mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
random: Add FastRandomContext::rand_uniform_delay
This commit is contained in:
11
src/random.h
11
src/random.h
@@ -223,6 +223,17 @@ public:
|
||||
/** Generate a random boolean. */
|
||||
bool randbool() noexcept { return randbits(1); }
|
||||
|
||||
/** Return the time point advanced by a uniform random duration. */
|
||||
template <typename Tp>
|
||||
Tp rand_uniform_delay(const Tp& time, typename Tp::duration range)
|
||||
{
|
||||
using Dur = typename Tp::duration;
|
||||
Dur dur{range.count() > 0 ? /* interval [0..range) */ Dur{randrange(range.count())} :
|
||||
range.count() < 0 ? /* interval (range..0] */ -Dur{randrange(-range.count())} :
|
||||
/* interval [0..0] */ Dur{0}};
|
||||
return time + dur;
|
||||
}
|
||||
|
||||
// Compatibility with the C++11 UniformRandomBitGenerator concept
|
||||
typedef uint64_t result_type;
|
||||
static constexpr uint64_t min() { return 0; }
|
||||
|
||||
Reference in New Issue
Block a user