[move] Move PoissonNextSend to src/random and update comment

PoissonNextSend is used by net and net_processing and is stateless, so
place it in the utility random.cpp translation unit.
This commit is contained in:
John Newbery
2021-12-14 10:15:10 +00:00
committed by Martin Zumsande
parent 801aaac2b3
commit 9e64d69bf7
4 changed files with 20 additions and 10 deletions

View File

@@ -19,6 +19,7 @@
#include <sync.h> // for Mutex
#include <util/time.h> // for GetTimeMicros()
#include <cmath>
#include <stdlib.h>
#include <thread>
@@ -714,3 +715,9 @@ void RandomInit()
ReportHardwareRand();
}
std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval)
{
double unscaled = -log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */);
return now + std::chrono::duration_cast<std::chrono::microseconds>(unscaled * average_interval + 0.5us);
}