6#ifndef BITCOIN_RANDOM_H
7#define BITCOIN_RANDOM_H
71uint64_t
GetRand(uint64_t nMax)
noexcept;
83constexpr auto GetRandMicros = GetRandomDuration<std::chrono::microseconds>;
84constexpr auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;
125 unsigned char bytebuf[64];
139 bytebuf_size =
sizeof(bytebuf);
163 uint64_t rand64() noexcept
165 if (bytebuf_size < 8) FillByteBuffer();
166 uint64_t ret =
ReadLE64(bytebuf + 64 - bytebuf_size);
176 }
else if (bits > 32) {
177 return rand64() >> (64 - bits);
179 if (bitbuf_size < bits) FillBitBuffer();
180 uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits));
196 uint64_t ret = randbits(bits);
197 if (ret <= range)
return ret;
202 std::vector<unsigned char> randbytes(
size_t len);
205 uint32_t
rand32() noexcept {
return randbits(32); }
211 bool randbool() noexcept {
return randbits(1); }
215 static constexpr uint64_t
min() {
return 0; }
216 static constexpr uint64_t
max() {
return std::numeric_limits<uint64_t>::max(); }
230template <
typename I,
typename R>
233 while (first != last) {
234 size_t j = rng.randrange(last - first);
237 swap(*first, *(first + j));
A class for ChaCha20 256-bit stream cipher developed by Daniel J.
void Keystream(unsigned char *c, size_t bytes)
outputs the keystream of size <bytes> into
uint32_t rand32() noexcept
Generate a random 32-bit integer.
static constexpr uint64_t max()
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
static constexpr uint64_t min()
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
uint64_t operator()() noexcept
static uint64_t ReadLE64(const unsigned char *ptr)
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.
D GetRandomDuration(typename std::common_type< D >::type max) noexcept
Generate a uniform random duration in the range [0..max).
constexpr auto GetRandMicros
void RandAddPeriodic() noexcept
Gather entropy from various expensive sources, and feed them to the PRNG state.
constexpr auto GetRandMillis
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
void GetStrongRandBytes(unsigned char *buf, int num) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
uint256 GetRandHash() noexcept
static const int NUM_OS_RANDOM_BYTES
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
void RandAddEvent(const uint32_t event_info) noexcept
Gathers entropy from the low bits of the time at which events occur.
void GetOSRand(unsigned char *ent32)
Get 32 bytes of system entropy.
int GetRandInt(int nMax) noexcept