aboutsummaryrefslogtreecommitdiff
path: root/src/random.cpp
diff options
context:
space:
mode:
authorJohn Newbery <john@johnnewbery.com>2021-12-14 10:15:10 +0000
committerMartin Zumsande <mzumsande@gmail.com>2022-01-13 15:54:59 +0100
commit9e64d69bf74c8a381fb59841519cc3736bce14d4 (patch)
tree9b2bffa0f8dfd562a90234023783c5d14c1c65e4 /src/random.cpp
parent801aaac2b39564aa14009785146ba26d2506fb53 (diff)
[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.
Diffstat (limited to 'src/random.cpp')
-rw-r--r--src/random.cpp7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/random.cpp b/src/random.cpp
index 6eb06c5d47..919061e61d 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -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);
+}