mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 06:28:31 +01:00
refactor: Make FEELER_SLEEP_WINDOW type safe (std::chrono)
This commit is contained in:
17
src/random.h
17
src/random.h
@@ -11,6 +11,7 @@
|
||||
#include <span.h>
|
||||
#include <uint256.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
@@ -236,13 +237,19 @@ public:
|
||||
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;
|
||||
return time + rand_uniform_duration<Tp>(range);
|
||||
}
|
||||
|
||||
/** Generate a uniform random duration in the range from 0 (inclusive) to range (exclusive). */
|
||||
template <typename Chrono>
|
||||
typename Chrono::duration rand_uniform_duration(typename Chrono::duration range) noexcept
|
||||
{
|
||||
using Dur = typename Chrono::duration;
|
||||
return 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};
|
||||
};
|
||||
|
||||
// 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