Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
FastRandomContext Class Reference

Fast randomness source. More...

#include <random.h>

Inheritance diagram for FastRandomContext:
[legend]
Collaboration diagram for FastRandomContext:
[legend]

Public Member Functions

 FastRandomContext (bool fDeterministic=false) noexcept
 Construct a FastRandomContext with GetRandHash()-based entropy (or zero key if fDeterministic).
 FastRandomContext (const uint256 &seed) noexcept
 Initialize with explicit seed (only for testing).
void Reseed (const uint256 &seed) noexcept
 Reseed with explicit seed (only for testing).
uint64_t rand64 () noexcept
 Generate a random 64-bit integer.
void fillrand (std::span< std::byte > output) noexcept
 Fill a byte span with random bytes.
Public Member Functions inherited from RandomMixin< FastRandomContext >
constexpr RandomMixin () noexcept=default
RandomMixinoperator= (const RandomMixin &)=delete
uint64_t randbits (int bits) noexcept
 Generate a random (bits)-bit integer.
randrange (I range) noexcept
 Generate a random integer in the range [0..range), with range > 0.
void fillrand (std::span< std::byte > span) noexcept
 Fill a span with random bytes.
rand () noexcept
 Generate a random integer in its entire (non-negative) range.
std::vector< Brandbytes (size_t len) noexcept
 Generate random bytes.
uint32_t rand32 () noexcept
 Generate a random 32-bit integer.
uint256 rand256 () noexcept
 generate a random uint256.
bool randbool () noexcept
 Generate a random boolean.
Tp rand_uniform_delay (const Tp &time, typename Tp::duration range) noexcept
 Return the time point advanced by a uniform random duration.
Chrono::duration rand_uniform_duration (typename Chrono::duration range) noexcept
 Generate a uniform random duration in the range from 0 (inclusive) to range (exclusive).
std::chrono::microseconds rand_exp_duration (std::chrono::microseconds mean) noexcept
 Return a duration sampled from an exponential distribution (https://en.wikipedia.org/wiki/Exponential_distribution).
uint64_t operator() () noexcept

Private Member Functions

void RandomSeed () noexcept

Private Attributes

bool requires_seed
ChaCha20 rng

Additional Inherited Members

Public Types inherited from RandomMixin< FastRandomContext >
typedef uint64_t result_type
Static Public Member Functions inherited from RandomMixin< FastRandomContext >
static constexpr uint64_t min () noexcept
static constexpr uint64_t max () noexcept
Protected Member Functions inherited from RandomMixin< FastRandomContext >
constexpr void FlushCache () noexcept

Detailed Description

Fast randomness source.

This is seeded once with secure random data, but is completely deterministic and does not gather more entropy after that.

This class is not thread-safe.

Definition at line 385 of file random.h.

Constructor & Destructor Documentation

◆ FastRandomContext() [1/2]

FastRandomContext::FastRandomContext ( bool fDeterministic = false)
explicitnoexcept

Construct a FastRandomContext with GetRandHash()-based entropy (or zero key if fDeterministic).

Definition at line 689 of file random.cpp.

◆ FastRandomContext() [2/2]

FastRandomContext::FastRandomContext ( const uint256 & seed)
explicitnoexcept

Initialize with explicit seed (only for testing).

Definition at line 632 of file random.cpp.

Member Function Documentation

◆ fillrand()

void FastRandomContext::fillrand ( std::span< std::byte > output)
noexcept

Fill a byte span with random bytes.

This overrides the RandomMixin version.

Definition at line 626 of file random.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rand64()

uint64_t FastRandomContext::rand64 ( )
inlinenoexcept

Generate a random 64-bit integer.

Definition at line 404 of file random.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RandomSeed()

void FastRandomContext::RandomSeed ( )
privatenoexcept

Definition at line 619 of file random.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Reseed()

void FastRandomContext::Reseed ( const uint256 & seed)
noexcept

Reseed with explicit seed (only for testing).

Definition at line 634 of file random.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ requires_seed

bool FastRandomContext::requires_seed
private

Definition at line 388 of file random.h.

◆ rng

ChaCha20 FastRandomContext::rng
private

Definition at line 389 of file random.h.


The documentation for this class was generated from the following files: