diff options
author | Norbert Thiebaud <nthiebaud@gmail.com> | 2015-05-01 22:58:26 -0500 |
---|---|---|
committer | Michael Stahl <mstahl@redhat.com> | 2015-05-05 10:41:56 +0000 |
commit | 011563a083da45b7c6805ec42778672d4a0fb0dc (patch) | |
tree | c89e20f00a9042e546a5175dd281325bd2ab66f3 /comphelper/source | |
parent | 9503fe93e08a11393188099a272e7fb8d6623157 (diff) |
seed mt19937 with random data
time(NULL) is a poor seed. It is quite predictable and
multiple instance starting in the same second will get the same seed
and therefore the same pseudo random number sequence
Use std::random_device, witch is meant to provide 'true' random
data.. mix time(NULL) just in case the std implementation is crappy.
PS: sadly std::random_device.entropy() cannot be relied on
as clang and gcc are known to return 0 despite their random_device
being non-deterministic, hence the prophylactic systematic
mixing with time(null)
Change-Id: I44dab9970f8f0388dc1c99e9816d49d1afbecf18
Reviewed-on: https://gerrit.libreoffice.org/15591
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Michael Stahl <mstahl@redhat.com>
Diffstat (limited to 'comphelper/source')
-rw-r--r-- | comphelper/source/misc/random.cxx | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/comphelper/source/misc/random.cxx b/comphelper/source/misc/random.cxx index ea6c6a1d603a..84e31760a379 100644 --- a/comphelper/source/misc/random.cxx +++ b/comphelper/source/misc/random.cxx @@ -37,12 +37,13 @@ struct RandomNumberGenerator STD_RNG_ALGO global_rng; RandomNumberGenerator() { + std::random_device rd; // initialises the state of the global random number generator // should only be called once. // (note, a few std::variate_generator<> (like normal) have their // own state which would need a reset as well to guarantee identical // sequence of numbers, e.g. via myrand.distribution().reset()) - global_rng.seed(time(NULL)); + global_rng.seed(rd() ^ time(nullptr)); } }; |