views:

108

answers:

2

Hi, 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

+13  A: 

random_shuffle.

KennyTM
+1: I was about to write the same thing. :D
Partial
+1, also, Boost.Random has some good random number generators to go with it, if you want something higher-quality than the RNG used by default. :-P
Chris Jester-Young
Who says it doesn't use a high-quality RNG already? :P At least with your own RNG you can control it, which is immensely useful for things like unit tests.
Roger Pate
+5  A: 

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

std::generate_n(myarray, size, &std::rand);
Manuel
+1: I don't think this is what the questioner meant, but well done for noticing the ambiguity in "randomize" :-)
Steve Jessop