Possible Duplicate:
How to make sure that std::random_shuffle always produces a different result?
I have an array and I wish to shuffle it, I use:
answerPositionArray[0] = 100;
answerPositionArray[1] = 400;
answerPositionArray[2] = 800;
std::random_shuffle(answerPositionArray, answerPositionArray + 2);
But every time I run my program the same shuffle comes out, 400, 800, 100. Is there a way to get the shuffle to be different every time? Eg. first time 100, 800, 400 then 800, 400, 100 etc.
Thanks
C++ random numbers aren’t truly random – they are generated from initial value called seed. If you don’t set the seed, it will always be the same, so generated sequence won’t change.
std::random_shuffledepends on random number generation, so it will behave this way as well.So how to set the seed? Use:
before any calls to functions using random numbers. It will set the seed to current time in seconds. Don’t forget to add appropritate header files.