Bitcoin ABC 0.32.4
P2P Digital Currency
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
FastRandomContext Class Reference

Fast randomness source. More...

#include <random.h>

Collaboration diagram for FastRandomContext:
[legend]

Public Types

typedef uint64_t result_type
 

Public Member Functions

 FastRandomContext (bool fDeterministic=false) noexcept
 
 FastRandomContext (const uint256 &seed) noexcept
 Initialize with explicit seed (only for testing) More...
 
 FastRandomContext (const FastRandomContext &)=delete
 
 FastRandomContext (FastRandomContext &&)=delete
 
FastRandomContextoperator= (const FastRandomContext &)=delete
 
FastRandomContextoperator= (FastRandomContext &&from) noexcept
 Move a FastRandomContext. More...
 
uint64_t rand64 () noexcept
 Generate a random 64-bit integer. More...
 
uint64_t randbits (int bits) noexcept
 Generate a random (bits)-bit integer. More...
 
uint64_t randrange (uint64_t range) noexcept
 Generate a random integer in the range [0..range). More...
 
template<BasicByte B = uint8_t>
std::vector< B > randbytes (size_t len) noexcept
 Generate random bytes. More...
 
void fillrand (Span< std::byte > output) noexcept
 Fill a byte Span with random bytes. More...
 
uint32_t rand32 () noexcept
 Generate a random 32-bit integer. More...
 
uint160 rand160 () noexcept
 generate a random uint160. More...
 
uint256 rand256 () noexcept
 generate a random uint256. More...
 
bool randbool () noexcept
 Generate a random boolean. More...
 
template<typename Tp >
Tp rand_uniform_delay (const Tp &time, typename Tp::duration range) noexcept
 Return the time point advanced by a uniform random duration. More...
 
uint64_t operator() () noexcept
 

Static Public Member Functions

static constexpr uint64_t min () noexcept
 
static constexpr uint64_t max () noexcept
 

Private Member Functions

void RandomSeed () noexcept
 
void FillBitBuffer () noexcept
 

Private Attributes

bool requires_seed
 
ChaCha20 rng
 
uint64_t bitbuf
 
int bitbuf_size
 

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 156 of file random.h.

Member Typedef Documentation

◆ result_type

Definition at line 278 of file random.h.

Constructor & Destructor Documentation

◆ FastRandomContext() [1/4]

FastRandomContext::FastRandomContext ( bool  fDeterministic = false)
explicitnoexcept

Definition at line 742 of file random.cpp.

◆ FastRandomContext() [2/4]

FastRandomContext::FastRandomContext ( const uint256 seed)
explicitnoexcept

Initialize with explicit seed (only for testing)

Definition at line 681 of file random.cpp.

◆ FastRandomContext() [3/4]

FastRandomContext::FastRandomContext ( const FastRandomContext )
delete

◆ FastRandomContext() [4/4]

FastRandomContext::FastRandomContext ( FastRandomContext &&  )
delete

Member Function Documentation

◆ FillBitBuffer()

void FastRandomContext::FillBitBuffer ( )
inlineprivatenoexcept

Definition at line 166 of file random.h.

◆ fillrand()

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

Fill a byte Span with random bytes.

Definition at line 674 of file random.cpp.

◆ max()

static constexpr uint64_t FastRandomContext::max ( )
inlinestaticconstexprnoexcept

Definition at line 280 of file random.h.

◆ min()

static constexpr uint64_t FastRandomContext::min ( )
inlinestaticconstexprnoexcept

Definition at line 279 of file random.h.

◆ operator()()

uint64_t FastRandomContext::operator() ( )
inlinenoexcept

Definition at line 283 of file random.h.

◆ operator=() [1/2]

FastRandomContext & FastRandomContext::operator= ( const FastRandomContext )
delete

◆ operator=() [2/2]

FastRandomContext & FastRandomContext::operator= ( FastRandomContext &&  from)
noexcept

Move a FastRandomContext.

If the original one is used again, it will be reseeded.

Definition at line 750 of file random.cpp.

◆ rand160()

uint160 FastRandomContext::rand160 ( )
inlinenoexcept

generate a random uint160.

Definition at line 247 of file random.h.

Here is the call graph for this function:

◆ rand256()

uint256 FastRandomContext::rand256 ( )
inlinenoexcept

generate a random uint256.

Definition at line 254 of file random.h.

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

◆ rand32()

uint32_t FastRandomContext::rand32 ( )
inlinenoexcept

Generate a random 32-bit integer.

Definition at line 244 of file random.h.

Here is the caller graph for this function:

◆ rand64()

uint64_t FastRandomContext::rand64 ( )
inlinenoexcept

Generate a random 64-bit integer.

Definition at line 190 of file random.h.

Here is the call graph for this function:

◆ rand_uniform_delay()

template<typename Tp >
Tp FastRandomContext::rand_uniform_delay ( const Tp &  time,
typename Tp::duration  range 
)
inlinenoexcept

Return the time point advanced by a uniform random duration.

Definition at line 265 of file random.h.

Here is the caller graph for this function:

◆ randbits()

uint64_t FastRandomContext::randbits ( int  bits)
inlinenoexcept

Generate a random (bits)-bit integer.

Definition at line 200 of file random.h.

Here is the caller graph for this function:

◆ randbool()

bool FastRandomContext::randbool ( )
inlinenoexcept

Generate a random boolean.

Definition at line 261 of file random.h.

Here is the caller graph for this function:

◆ randbytes()

template<BasicByte B = uint8_t>
std::vector< B > FastRandomContext::randbytes ( size_t  len)
inlinenoexcept

Generate random bytes.

Definition at line 234 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 668 of file random.cpp.

Here is the call graph for this function:

◆ randrange()

uint64_t FastRandomContext::randrange ( uint64_t  range)
inlinenoexcept

Generate a random integer in the range [0..range).

Precondition: range > 0.

Definition at line 220 of file random.h.

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

Member Data Documentation

◆ bitbuf

uint64_t FastRandomContext::bitbuf
private

Definition at line 161 of file random.h.

◆ bitbuf_size

int FastRandomContext::bitbuf_size
private

Definition at line 162 of file random.h.

◆ requires_seed

bool FastRandomContext::requires_seed
private

Definition at line 158 of file random.h.

◆ rng

ChaCha20 FastRandomContext::rng
private

Definition at line 159 of file random.h.


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