Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

easy way to randomize the entries of an array using stl?

I can sort a int* array using stl, plain and simple like

std::sort(myarray, myarray + size);

Is there any equal simple way to randomize it?

thanks

like image 772
monkeyking Avatar asked Nov 28 '22 04:11

monkeyking


2 Answers

std::random_shuffle(myarray, myarray + size);

like image 135
kennytm Avatar answered Nov 30 '22 18:11

kennytm


If you want to generate new random content instead of shuffling the elements that are already there:

std::generate_n(myarray, size, &std::rand);
like image 41
Manuel Avatar answered Nov 30 '22 17:11

Manuel