Skip to content

Commit 152146e

Browse files
committed
DRY: Implement GetRand using FastRandomContext::randrange
1 parent a1f252e commit 152146e

File tree

1 file changed

+1
-11
lines changed

1 file changed

+1
-11
lines changed

src/random.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -503,17 +503,7 @@ void RandAddSeedSleep() { ProcRand(nullptr, 0, RNGLevel::SLEEP); }
503503

504504
uint64_t GetRand(uint64_t nMax) noexcept
505505
{
506-
if (nMax == 0)
507-
return 0;
508-
509-
// The range of the random source must be a multiple of the modulus
510-
// to give every possible output value an equal possibility
511-
uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
512-
uint64_t nRand = 0;
513-
do {
514-
GetRandBytes((unsigned char*)&nRand, sizeof(nRand));
515-
} while (nRand >= nRange);
516-
return (nRand % nMax);
506+
return FastRandomContext().randrange(nMax);
517507
}
518508

519509
int GetRandInt(int nMax) noexcept

0 commit comments

Comments
 (0)