From 11f1eaf2c34f1e26648fe1be5b192017338ca86b Mon Sep 17 00:00:00 2001 From: aljar Date: Fri, 13 Nov 2020 17:49:14 +0100 Subject: [PATCH] Don't use the old random_shuffle and don't set a global seed. --- src/linux_tcp/addressinfo.h | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/linux_tcp/addressinfo.h b/src/linux_tcp/addressinfo.h index b9df839..550bf96 100644 --- a/src/linux_tcp/addressinfo.h +++ b/src/linux_tcp/addressinfo.h @@ -10,7 +10,7 @@ /** * Dependencies */ -#include +#include /** * Include guard @@ -76,17 +76,14 @@ public: // which may break loadbalancing.. if (randomOrder) { - // We need to seed the random number generator. Normally time is taken - // for this. Since we want to have randomness within a second we use - // more precision via timeval - struct timeval time; - gettimeofday(&time, nullptr); + // create a random device for the seed of the random number generator + std::random_device rd; - // We seed with the precision of miliseconds. - srand((time.tv_sec * 1000) + (time.tv_usec / 1000)); + // Create the generator + std::mt19937 gen(rd()); - // shuffle the vector. - std::random_shuffle(_v.begin(), _v.end()); + // shuffle the vector. + std::shuffle(_v.begin(), _v.end(), gen); } }