DRY: Implement GetRand using FastRandomContext::randrange
This commit is contained in:
parent
a1f252eda8
commit
152146e782
1 changed files with 1 additions and 11 deletions
|
@ -503,17 +503,7 @@ void RandAddSeedSleep() { ProcRand(nullptr, 0, RNGLevel::SLEEP); }
|
||||||
|
|
||||||
uint64_t GetRand(uint64_t nMax) noexcept
|
uint64_t GetRand(uint64_t nMax) noexcept
|
||||||
{
|
{
|
||||||
if (nMax == 0)
|
return FastRandomContext().randrange(nMax);
|
||||||
return 0;
|
|
||||||
|
|
||||||
// The range of the random source must be a multiple of the modulus
|
|
||||||
// to give every possible output value an equal possibility
|
|
||||||
uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
|
|
||||||
uint64_t nRand = 0;
|
|
||||||
do {
|
|
||||||
GetRandBytes((unsigned char*)&nRand, sizeof(nRand));
|
|
||||||
} while (nRand >= nRange);
|
|
||||||
return (nRand % nMax);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetRandInt(int nMax) noexcept
|
int GetRandInt(int nMax) noexcept
|
||||||
|
|
Loading…
Reference in a new issue