6 #ifndef BITCOIN_RANDOM_H 7 #define BITCOIN_RANDOM_H 73 uint64_t
GetRand(uint64_t nMax) noexcept;
74 std::chrono::microseconds
75 GetRandMicros(std::chrono::microseconds duration_max) noexcept;
129 bytebuf_size =
sizeof(bytebuf);
138 explicit FastRandomContext(
bool fDeterministic =
false) noexcept;
141 explicit FastRandomContext(
const uint256 &seed) noexcept;
145 FastRandomContext(
const FastRandomContext &) =
delete;
146 FastRandomContext(FastRandomContext &&) =
delete;
147 FastRandomContext &operator=(
const FastRandomContext &) =
delete;
153 FastRandomContext &operator=(FastRandomContext &&from) noexcept;
157 if (bytebuf_size < 8) {
160 uint64_t ret =
ReadLE64(bytebuf + 64 - bytebuf_size);
169 }
else if (bits > 32) {
170 return rand64() >> (64 - bits);
172 if (bitbuf_size < bits) {
175 uint64_t ret = bitbuf & (~uint64_t(0) >> (64 - bits));
188 uint64_t ret = randbits(bits);
196 std::vector<uint8_t> randbytes(
size_t len);
199 uint32_t
rand32() noexcept {
return randbits(32); }
212 static constexpr uint64_t
min() {
return 0; }
213 static constexpr uint64_t
max() {
214 return std::numeric_limits<uint64_t>::max();
230 template <
typename I,
typename R>
void Shuffle(I first, I last, R &&rng) {
231 while (first != last) {
232 size_t j = rng.randrange(last - first);
235 swap(*first, *(first + j));
269 #endif // BITCOIN_RANDOM_H static const int NUM_OS_RANDOM_BYTES
Number of random bytes returned by GetOSRand.
void Keystream(uint8_t *c, size_t bytes)
outputs the keystream of size <bytes> into
int GetRandInt(int nMax) noexcept
void GetOSRand(uint8_t *ent32)
Get 32 bytes of system entropy.
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
void GetRandBytes(uint8_t *buf, int num) noexcept
Overall design of the RNG and entropy sources.
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 RandomInit()
Initialize global RNG state and log any CPU features that are used.
static constexpr uint64_t max()
std::chrono::microseconds GetRandMicros(std::chrono::microseconds duration_max) noexcept
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 Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
uint256 GetRandHash() noexcept
uint64_t operator()() noexcept
static constexpr uint64_t min()
bool randbool() noexcept
Generate a random boolean.
void GetStrongRandBytes(uint8_t *buf, int num) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
uint64_t rand64() noexcept
Generate a random 64-bit integer.
static uint64_t ReadLE64(const uint8_t *ptr)
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