From 94dc0387683b4a1fb6967c67403d9d1608ac3165 Mon Sep 17 00:00:00 2001 From: Tyler Karaszewski Date: Thu, 7 Dec 2023 16:40:39 -0700 Subject: [PATCH 1/3] Allocate when called, not at startup --- libstuff/SRandom.cpp | 19 ++++++++++++------- libstuff/SRandom.h | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/libstuff/SRandom.cpp b/libstuff/SRandom.cpp index b6900dac6..baa2421d7 100644 --- a/libstuff/SRandom.cpp +++ b/libstuff/SRandom.cpp @@ -1,20 +1,25 @@ #include "SRandom.h" -#ifdef VALGRIND -// random_device breaks valgrind. -mt19937_64 SRandom::_generator = mt19937_64(); -#else -mt19937_64 SRandom::_generator = mt19937_64(random_device()()); -#endif - uniform_int_distribution SRandom::_distribution64 = uniform_int_distribution(); uint64_t SRandom::limitedRand64(uint64_t minNum, uint64_t maxNum) { uniform_int_distribution limitedRandom(minNum, maxNum); +#ifdef VALGRIND + // random_device breaks valgrind. + mt19937_64 _generator = mt19937_64(); +#else + mt19937_64 _generator = mt19937_64(random_device()()); +#endif return limitedRandom(_generator); } uint64_t SRandom::rand64() { +#ifdef VALGRIND + // random_device breaks valgrind. + mt19937_64 _generator = mt19937_64(); +#else + mt19937_64 _generator = mt19937_64(random_device()()); +#endif return _distribution64(_generator); } diff --git a/libstuff/SRandom.h b/libstuff/SRandom.h index a718a04cd..c23f0674c 100644 --- a/libstuff/SRandom.h +++ b/libstuff/SRandom.h @@ -11,8 +11,8 @@ class SRandom { static uint64_t rand64(); static uint64_t limitedRand64(uint64_t min, uint64_t max); static string randStr(uint& length); + void init(); private: - static mt19937_64 _generator; static uniform_int_distribution _distribution64; }; From 61a65325127b1d8c91413876c3282d443ea6be5a Mon Sep 17 00:00:00 2001 From: Tyler Karaszewski Date: Thu, 7 Dec 2023 16:48:02 -0700 Subject: [PATCH 2/3] oops, was testing with this --- libstuff/SRandom.h | 1 - 1 file changed, 1 deletion(-) diff --git a/libstuff/SRandom.h b/libstuff/SRandom.h index c23f0674c..1f1cba16e 100644 --- a/libstuff/SRandom.h +++ b/libstuff/SRandom.h @@ -11,7 +11,6 @@ class SRandom { static uint64_t rand64(); static uint64_t limitedRand64(uint64_t min, uint64_t max); static string randStr(uint& length); - void init(); private: static uniform_int_distribution _distribution64; From d51fa1f26a22f1666a4f349e2cef338b90b6a9bf Mon Sep 17 00:00:00 2001 From: Tyler Karaszewski Date: Fri, 8 Dec 2023 07:05:47 -0800 Subject: [PATCH 3/3] Revert "Allocate RNG when called, not at startup" --- libstuff/SRandom.cpp | 19 +++++++------------ libstuff/SRandom.h | 1 + 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/libstuff/SRandom.cpp b/libstuff/SRandom.cpp index baa2421d7..b6900dac6 100644 --- a/libstuff/SRandom.cpp +++ b/libstuff/SRandom.cpp @@ -1,25 +1,20 @@ #include "SRandom.h" +#ifdef VALGRIND +// random_device breaks valgrind. +mt19937_64 SRandom::_generator = mt19937_64(); +#else +mt19937_64 SRandom::_generator = mt19937_64(random_device()()); +#endif + uniform_int_distribution SRandom::_distribution64 = uniform_int_distribution(); uint64_t SRandom::limitedRand64(uint64_t minNum, uint64_t maxNum) { uniform_int_distribution limitedRandom(minNum, maxNum); -#ifdef VALGRIND - // random_device breaks valgrind. - mt19937_64 _generator = mt19937_64(); -#else - mt19937_64 _generator = mt19937_64(random_device()()); -#endif return limitedRandom(_generator); } uint64_t SRandom::rand64() { -#ifdef VALGRIND - // random_device breaks valgrind. - mt19937_64 _generator = mt19937_64(); -#else - mt19937_64 _generator = mt19937_64(random_device()()); -#endif return _distribution64(_generator); } diff --git a/libstuff/SRandom.h b/libstuff/SRandom.h index 1f1cba16e..a718a04cd 100644 --- a/libstuff/SRandom.h +++ b/libstuff/SRandom.h @@ -13,5 +13,6 @@ class SRandom { static string randStr(uint& length); private: + static mt19937_64 _generator; static uniform_int_distribution _distribution64; };