random: make GetRand() support entire range (incl. max)

The existing code uses GetRand(nMax), with a default value for nMax, where nMax is the
range of values (not the maximum!) that the output is allowed to take. This will always
miss the last possible value (e.g. GetRand<uint32_t>() will never return 0xffffffff).

Fix this, by moving the functionality largely in RandomMixin, and also adding a
separate RandomMixin::rand function, which returns a value in the entire (non-negative)
range of an integer.
This commit is contained in:
Pieter Wuille
2024-03-10 23:38:31 -04:00
parent 810cdf6b4e
commit e2d1f84858
3 changed files with 46 additions and 39 deletions

View File

@@ -679,11 +679,6 @@ void RandAddPeriodic() noexcept
void RandAddEvent(const uint32_t event_info) noexcept { GetRNGState().AddEvent(event_info); }
uint64_t GetRandInternal(uint64_t nMax) noexcept
{
return FastRandomContext().randrange(nMax);
}
uint256 GetRandHash() noexcept
{
uint256 hash;