70 uint64_t
GetRand(uint64_t nMax) noexcept;
109 bool requires_seed{
false};
112 unsigned char bytebuf[64];
126 bytebuf_size =
sizeof(bytebuf);
150 uint64_t rand64() noexcept
152 if (bytebuf_size < 8) FillByteBuffer();
153 uint64_t ret = ReadLE64(bytebuf + 64 - bytebuf_size);
162 }
else if (bits > 32) {
163 return rand64() >> (64 - bits);
165 if (bitbuf_size < bits) FillBitBuffer();
166 uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits));
177 int bits = CountBits(range);
179 uint64_t ret = randbits(bits);
180 if (ret <= range)
return ret;
185 std::vector<unsigned char> randbytes(
size_t len);
188 uint16_t
rand16() {
return randbits(16); }
191 uint32_t
rand32() noexcept {
return randbits(32); }
197 bool randbool() noexcept {
return randbits(1); }
201 static constexpr uint64_t
min() {
return 0; }
202 static constexpr uint64_t
max() {
return std::numeric_limits<uint64_t>::max(); }
216 template<
typename I,
typename R>
219 while (first != last) {
220 size_t j = rng.randrange(last - first);
223 swap(*first, *(first + j));
234 static const ssize_t NUM_OS_RANDOM_BYTES = 32;
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.
uint16_t rand16()
Generate a random 16-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
void RandAddPeriodic() noexcept
Gather entropy from various expensive sources, and feed them to the PRNG state.
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
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
bool GetRandBool(double rate) noexcept
uint64_t GetRand(uint64_t nMax) noexcept
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