The following call for random shuffle is always giving the same results for the vector v
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
int main()
{
std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::srand(time(0));
std::random_shuffle(v.begin(), v.end());
for (int i = 0; i < v.size(); ++i) {
printf("%d ", v[i]); printf("\n");
}
printf("%d\n", std::rand() % 100);
}
I've tried compiling using
g++ -std=c++0x
g++ -std=c++11
But both give the same results every time so I don't really understand what's going on.
$./a.out
7 1 4 6 8 9 5 2 3 10
26
$ ./a.out
7 1 4 6 8 9 5 2 3 10
41
$ ./a.out
7 1 4 6 8 9 5 2 3 10
39
Firstly,
-std=c++0xand-std=c++11mean exactly the same thing, so testing both is pointless.You didn't provide a complete program (please read https://stackoverflow.com/help/mcve next time) so I guessed at the rest of your code, and I tried this:
I get different results every second:
The times when it produces the same result are because the number of seconds returned by
time(0)is the same, and so the seed for therand()function is the same, and so the results are the same. If you wait a second so thattime(0)returns a different value you should get a different random shuffle of the elements.If the code you are running is not the same as mine you might get different results, but we can't possibly explain the results because you didn't show us your code.