Don't use the old random_shuffle and don't set a global seed.

This commit is contained in:
aljar 2020-11-13 17:49:14 +01:00
parent 4570496547
commit 11f1eaf2c3
1 changed files with 7 additions and 10 deletions

View File

@ -10,7 +10,7 @@
/** /**
* Dependencies * Dependencies
*/ */
#include <sys/time.h> #include <random>
/** /**
* Include guard * Include guard
@ -76,17 +76,14 @@ public:
// which may break loadbalancing.. // which may break loadbalancing..
if (randomOrder) if (randomOrder)
{ {
// We need to seed the random number generator. Normally time is taken // create a random device for the seed of the random number generator
// for this. Since we want to have randomness within a second we use std::random_device rd;
// more precision via timeval
struct timeval time;
gettimeofday(&time, nullptr);
// We seed with the precision of miliseconds. // Create the generator
srand((time.tv_sec * 1000) + (time.tv_usec / 1000)); std::mt19937 gen(rd());
// shuffle the vector. // shuffle the vector.
std::random_shuffle(_v.begin(), _v.end()); std::shuffle(_v.begin(), _v.end(), gen);
} }
} }