6 #ifndef BITCOIN_RANDOM_H 7 #define BITCOIN_RANDOM_H 80 T GetRand(T nMax=std::numeric_limits<T>::max()) noexcept {
81 static_assert(std::is_integral<T>(),
"T must be integral");
82 static_assert(std::numeric_limits<T>::max() <= std::numeric_limits<uint64_t>::max(),
"GetRand only supports up to uint64_t");
96 constexpr
auto GetRandMicros = GetRandomDuration<std::chrono::microseconds>;
97 constexpr
auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;
108 std::chrono::microseconds
GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval);
175 uint64_t rand64() noexcept
177 if (requires_seed) RandomSeed();
178 std::array<std::byte, 8> buf;
188 }
else if (bits > 32) {
189 return rand64() >> (64 - bits);
191 if (bitbuf_size < bits) FillBitBuffer();
192 uint64_t
ret = bitbuf & (~uint64_t{0} >> (64 - bits));
208 uint64_t
ret = randbits(bits);
209 if (
ret <= range)
return ret;
214 template <
typename B =
unsigned char>
215 std::vector<B> randbytes(
size_t len);
221 uint32_t
rand32() noexcept {
return randbits(32); }
227 bool randbool() noexcept {
return randbits(1); }
230 template <
typename Tp>
233 return time + rand_uniform_duration<Tp>(range);
237 template <
typename Chrono>
240 using Dur =
typename Chrono::duration;
241 return range.count() > 0 ? Dur{randrange(range.count())} :
242 range.count() < 0 ? -Dur{randrange(-range.count())} :
247 typedef uint64_t result_type;
248 static constexpr uint64_t
min() {
return 0; }
249 static constexpr uint64_t
max() {
return std::numeric_limits<uint64_t>::max(); }
263 template <
typename I,
typename R>
266 while (first != last) {
267 size_t j = rng.randrange(last - first);
270 swap(*first, *(first + j));
301 #endif // BITCOIN_RANDOM_H static const int NUM_OS_RANDOM_BYTES
constexpr auto GetRandMillis
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
constexpr auto GetRandMicros
void Keystream(Span< std::byte > out) noexcept
outputs the keystream to out.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Unrestricted ChaCha20 cipher.
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
void GetRandBytes(Span< unsigned char > bytes) noexcept
Overall design of the RNG and entropy sources.
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
static constexpr uint64_t max()
static uint64_t CountBits(uint64_t x)
Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set...
void RandAddPeriodic() noexcept
Gather entropy from various expensive sources, and feed them to the PRNG state.
static uint64_t ReadLE64(const unsigned char *ptr)
uint32_t rand32() noexcept
Generate a random 32-bit integer.
void GetStrongRandBytes(Span< unsigned char > bytes) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
uint256 GetRandHash() noexcept
void GetOSRand(unsigned char *ent32)
Get 32 bytes of system entropy.
D GetRandomDuration(typename std::common_type< D >::type max) noexcept
Generate a uniform random duration in the range [0..max).
uint64_t operator()() noexcept
static constexpr uint64_t min()
unsigned char * UCharCast(char *c)
Tp rand_uniform_delay(const Tp &time, typename Tp::duration range)
Return the time point advanced by a uniform random duration.
uint64_t GetRandInternal(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
A Span is an object that can refer to a contiguous sequence of objects.
#define T(expected, seed, data)
void RandAddEvent(const uint32_t event_info) noexcept
Gathers entropy from the low bits of the time at which events occur.
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Chrono::duration rand_uniform_duration(typename Chrono::duration range) noexcept
Generate a uniform random duration in the range from 0 (inclusive) to range (exclusive).
std::chrono::microseconds GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval)
Return a timestamp in the future sampled from an exponential distribution (https://en.wikipedia.org/wiki/Exponential_distribution).