random: convert GetExponentialRand into rand_exp_duration

This commit is contained in:
Pieter Wuille
2024-03-11 10:44:35 -04:00
parent 4eaa239dc3
commit cfb0dfe2cf
4 changed files with 33 additions and 25 deletions

View File

@@ -773,8 +773,7 @@ void RandomInit()
ReportHardwareRand();
}
std::chrono::microseconds GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval)
double MakeExponentiallyDistributed(uint64_t uniform) noexcept
{
double unscaled = -std::log1p(FastRandomContext().randbits<48>() * -0.0000000000000035527136788 /* -1/2^48 */);
return now + std::chrono::duration_cast<std::chrono::microseconds>(unscaled * average_interval + 0.5us);
return -std::log1p((uniform >> 16) * -0.0000000000000035527136788 /* -1/2^48 */);
}