Skip to content

Commit

Permalink
Merge pull request #1624 from Expensify/tyler-non-static-randoms
Browse files Browse the repository at this point in the history
Allocate RNG when called, not at startup
  • Loading branch information
coleaeason authored Dec 8, 2023
2 parents 17af25a + 61a6532 commit 41e204a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
19 changes: 12 additions & 7 deletions libstuff/SRandom.cpp
Original file line number Diff line number Diff line change
@@ -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<uint64_t> SRandom::_distribution64 = uniform_int_distribution<uint64_t>();

uint64_t SRandom::limitedRand64(uint64_t minNum, uint64_t maxNum) {
uniform_int_distribution<uint64_t> 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);
}

Expand Down
1 change: 0 additions & 1 deletion libstuff/SRandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ class SRandom {
static string randStr(uint& length);

private:
static mt19937_64 _generator;
static uniform_int_distribution<uint64_t> _distribution64;
};

0 comments on commit 41e204a

Please sign in to comment.