6#ifndef BITCOIN_RANDOM_H
7#define BITCOIN_RANDOM_H
85T
GetRand(T nMax = std::numeric_limits<T>::max()) noexcept {
86 static_assert(std::is_integral<T>(),
"T must be integral");
87 static_assert(std::numeric_limits<T>::max() <=
88 std::numeric_limits<uint64_t>::max(),
89 "GetRand only supports up to uint64_t");
104 return D{
GetRand(max.count())};
118std::chrono::microseconds
120 std::chrono::seconds average_interval);
164 void RandomSeed() noexcept;
166 void FillBitBuffer() noexcept {
190 uint64_t rand64() noexcept {
194 std::array<std::byte, 8> buf;
203 }
else if (bits > 32) {
204 return rand64() >> (64 - bits);
206 if (bitbuf_size < bits) {
209 uint64_t ret = bitbuf & (~uint64_t(0) >> (64 - bits));
225 uint64_t ret = randbits(bits);
233 template <BasicByte B = u
int8_t>
235 std::vector<B> ret(len);
244 uint32_t
rand32() noexcept {
return randbits(32); }
264 template <
typename Tp>
266 typename Tp::duration range)
noexcept {
267 using Dur =
typename Tp::duration;
268 Dur dur{range.count() > 0
269 ? Dur{randrange(range.count())}
271 ? -Dur{randrange(-range.count())}
279 static constexpr uint64_t
min() noexcept {
return 0; }
280 static constexpr uint64_t
max() noexcept {
281 return std::numeric_limits<uint64_t>::max();
297template <
typename I,
typename R>
void Shuffle(I first, I last, R &&rng) {
298 while (first != last) {
299 size_t j = rng.randrange(last - first);
302 swap(*first, *(first + j));
Unrestricted ChaCha20 cipher.
void Keystream(Span< std::byte > out) noexcept
outputs the keystream to out.
Tp rand_uniform_delay(const Tp &time, typename Tp::duration range) noexcept
Return the time point advanced by a uniform random duration.
static constexpr uint64_t max() noexcept
uint160 rand160() noexcept
generate a random uint160.
uint32_t rand32() noexcept
Generate a random 32-bit integer.
uint256 rand256() noexcept
generate a random uint256.
bool randbool() noexcept
Generate a random boolean.
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
static constexpr uint64_t min() noexcept
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
uint64_t operator()() noexcept
A Span is an object that can refer to a contiguous sequence of objects.
static uint64_t ReadLE64(const uint8_t *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.
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....
D GetRandomDuration(typename std::common_type< D >::type max) noexcept
Generate a uniform random duration in the range [0..max).
constexpr auto GetRandMicros
void GetRandBytes(Span< uint8_t > bytes) noexcept
Overall design of the RNG and entropy sources.
void RandAddPeriodic() noexcept
Gather entropy from various expensive sources, and feed them to the PRNG state.
constexpr auto GetRandMillis
uint64_t GetRandInternal(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
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.
uint256 GetRandHash() noexcept
static const int NUM_OS_RANDOM_BYTES
Number of random bytes returned by GetOSRand.
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
void RandAddEvent(const uint32_t event_info) noexcept
Gathers entropy from the low bits of the time at which events occur.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
void GetStrongRandBytes(Span< uint8_t > bytes) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
uint8_t * UCharCast(char *c)
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept