C++ vector randomised the same way after each random shuffle

219 views Asked by At

I've tried a number of ways to randomise the items in a vector container in C++. Though more than that the items have to be different each time they are randomised. At the moment it is spitting out the same randomly shuffled vector each time.

The methods I've used are as follows:

/////////////////////////////////////////////////
METHOD 1
srand(time(0));
random_shuffle(mapkeys.begin(), mapkeys.end());

METHOD 2
int myrandom (int i) { return std::rand()%i;}
random_shuffle(mapkeys.begin(), mapkeys.end(), myrandom);

METHOD 3
auto engine = default_random_engine{};
shuffle(begin(mapkeys), end(mapkeys), engine);

METHOD 4
default_random_engine gen(unsigned(time (NULL)));
shuffle(begin(mapkeys), end(mapkeys), gen);
/////////////////////////////////////////////////

I found these from various questions here on stack. While I understand the concept of needing to pass the seed into random_shuffle, it does not seem to work for me. If someone could not only point out but explain to me the error that would be greatly appreciated!

0

There are 0 answers