6 #ifndef BITCOIN_RANDOM_H 7 #define BITCOIN_RANDOM_H 71 uint64_t
GetRand(uint64_t nMax) noexcept;
83 constexpr
auto GetRandMicros = GetRandomDuration<std::chrono::microseconds>;
84 constexpr
auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;
125 unsigned char bytebuf[64];
139 bytebuf_size =
sizeof(bytebuf);
149 explicit FastRandomContext(
bool fDeterministic =
false) noexcept;
152 explicit FastRandomContext(
const uint256& seed) noexcept;
155 FastRandomContext(
const FastRandomContext&) =
delete;
156 FastRandomContext(FastRandomContext&&) =
delete;
157 FastRandomContext& operator=(
const FastRandomContext&) =
delete;
160 FastRandomContext& operator=(FastRandomContext&& from) 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); }
215 static constexpr uint64_t
min() {
return 0; }
216 static constexpr uint64_t
max() {
return std::numeric_limits<uint64_t>::max(); }
230 template <
typename I,
typename R>
233 while (first != last) {
234 size_t j = rng.randrange(last - first);
237 swap(*first, *(first + j));
268 #endif // BITCOIN_RANDOM_H static const int NUM_OS_RANDOM_BYTES
int GetRandInt(int nMax) noexcept
constexpr auto GetRandMillis
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
constexpr auto GetRandMicros
A class for ChaCha20 256-bit stream cipher developed by Daniel J.
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
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...
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
static constexpr uint64_t max()
void RandAddPeriodic() noexcept
Gather entropy from various expensive sources, and feed them to the PRNG state.
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...
uint32_t rand32() noexcept
Generate a random 32-bit integer.
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
void Keystream(unsigned char *c, size_t bytes)
outputs the keystream of size <bytes> into
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 uint64_t ReadLE64(const unsigned char *ptr)
static constexpr uint64_t min()
bool randbool() noexcept
Generate a random boolean.
uint64_t rand64() noexcept
Generate a random 64-bit integer.
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).
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).