Skip to content

Commit

Permalink
DRY: Implement GetRand using FastRandomContext::randrange
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Jan 17, 2019
1 parent a1f252e commit 152146e
Showing 1 changed file with 1 addition and 11 deletions.
12 changes: 1 addition & 11 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,17 +503,7 @@ void RandAddSeedSleep() { ProcRand(nullptr, 0, RNGLevel::SLEEP); }

uint64_t GetRand(uint64_t nMax) noexcept
{
if (nMax == 0)
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);
return FastRandomContext().randrange(nMax);
}

int GetRandInt(int nMax) noexcept
Expand Down

0 comments on commit 152146e

Please sign in to comment.