 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
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>;
95 std::chrono::microseconds
GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval);
137 unsigned char bytebuf[64];
151 bytebuf_size =
sizeof(bytebuf);
175 uint64_t rand64() noexcept
177 if (bytebuf_size < 8) FillByteBuffer();
178 uint64_t ret =
ReadLE64(bytebuf + 64 - bytebuf_size);
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 std::vector<unsigned char> randbytes(
size_t len);
217 uint32_t
rand32() noexcept {
return randbits(32); }
223 bool randbool() noexcept {
return randbits(1); }
227 static constexpr uint64_t
min() {
return 0; }
228 static constexpr uint64_t
max() {
return std::numeric_limits<uint64_t>::max(); }
242 template <
typename I,
typename R>
245 while (first != last) {
246 size_t j = rng.randrange(last - first);
249 swap(*first, *(first + j));
280 #endif // BITCOIN_RANDOM_H
static uint64_t ReadLE64(const unsigned char *ptr)
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....
static const int NUM_OS_RANDOM_BYTES
bool Random_SanityCheck()
Check that OS randomness is available and returning the requested number of bytes.
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
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...
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
void Keystream(unsigned char *c, size_t bytes)
outputs the keystream of size <bytes> into
constexpr auto GetRandMicros
uint32_t rand32() noexcept
Generate a random 32-bit integer.
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 GetOSRand(unsigned char *ent32)
Get 32 bytes of system entropy.
void RandAddEvent(const uint32_t event_info) noexcept
Gathers entropy from the low bits of the time at which events occur.
static constexpr uint64_t max()
int GetRandInt(int nMax) noexcept
uint256 GetRandHash() noexcept
D GetRandomDuration(typename std::common_type< D >::type max) noexcept
Generate a uniform random duration in the range [0..max).
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
static constexpr uint64_t min()
uint64_t operator()() noexcept
A class for ChaCha20 256-bit stream cipher developed by Daniel J.
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
constexpr auto GetRandMillis
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.