Bitcoin Core 28.99.0
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages Concepts
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
RandomMixin< T > Class Template Reference

Mixin class that provides helper randomness functions. More...

#include <random.h>

Public Types

typedef uint64_t result_type
 

Public Member Functions

constexpr RandomMixin () noexcept=default
 
 RandomMixin (const RandomMixin &)=delete
 
RandomMixinoperator= (const RandomMixin &)=delete
 
 RandomMixin (RandomMixin &&)=delete
 
RandomMixinoperator= (RandomMixin &&)=delete
 
uint64_t randbits (int bits) noexcept
 Generate a random (bits)-bit integer. More...
 
template<int Bits>
uint64_t randbits () noexcept
 Same as above, but with compile-time fixed bits count. More...
 
template<std::integral I>
randrange (I range) noexcept
 Generate a random integer in the range [0..range), with range > 0. More...
 
void fillrand (Span< std::byte > span) noexcept
 Fill a Span with random bytes. More...
 
template<std::integral I>
rand () noexcept
 Generate a random integer in its entire (non-negative) range. More...
 
template<BasicByte B = unsigned char>
std::vector< Brandbytes (size_t len) noexcept
 Generate random bytes. More...
 
uint32_t rand32 () noexcept
 Generate a random 32-bit integer. 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...
 
template<typename Chrono >
requires StdChronoDuration<typename Chrono::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). More...
 
template<StdChronoDuration Dur>
Dur randrange (typename std::common_type_t< Dur > range) noexcept
 Generate a uniform random duration in the range [0..max). More...
 
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). More...
 
uint64_t operator() () noexcept
 

Static Public Member Functions

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

Protected Member Functions

constexpr void FlushCache () noexcept
 

Private Member Functions

RandomNumberGenerator auto & Impl () noexcept
 Access the underlying generator. More...
 

Private Attributes

uint64_t bitbuf {0}
 
int bitbuf_size {0}
 

Detailed Description

template<typename T>
class RandomMixin< T >

Mixin class that provides helper randomness functions.

Intended to be used through CRTP: https://en.cppreference.com/w/cpp/language/crtp. An RNG class FunkyRNG would derive publicly from RandomMixin<FunkyRNG>. This permits RandomMixin from accessing the derived class's rand64() function, while also allowing the derived class to provide more.

The derived class must satisfy the RandomNumberGenerator concept.

Definition at line 174 of file random.h.

Member Typedef Documentation

◆ result_type

template<typename T >
typedef uint64_t RandomMixin< T >::result_type

Definition at line 364 of file random.h.

Constructor & Destructor Documentation

◆ RandomMixin() [1/3]

template<typename T >
constexpr RandomMixin< T >::RandomMixin ( )
constexprdefaultnoexcept

◆ RandomMixin() [2/3]

template<typename T >
RandomMixin< T >::RandomMixin ( const RandomMixin< T > &  )
delete

◆ RandomMixin() [3/3]

template<typename T >
RandomMixin< T >::RandomMixin ( RandomMixin< T > &&  )
delete

Member Function Documentation

◆ fillrand()

template<typename T >
void RandomMixin< T >::fillrand ( Span< std::byte >  span)
inlinenoexcept

Fill a Span with random bytes.

Definition at line 267 of file random.h.

Here is the call graph for this function:

◆ FlushCache()

template<typename T >
constexpr void RandomMixin< T >::FlushCache ( )
inlineconstexprprotectednoexcept

Definition at line 188 of file random.h.

◆ Impl()

template<typename T >
RandomNumberGenerator auto & RandomMixin< T >::Impl ( )
inlineprivatenoexcept

Access the underlying generator.

This also enforces the RandomNumberGenerator concept. We cannot declare that in the template (no template<RandomNumberGenerator T>) because the type isn't fully instantiated yet there.

Definition at line 185 of file random.h.

Here is the caller graph for this function:

◆ max()

template<typename T >
static constexpr uint64_t RandomMixin< T >::max ( )
inlinestaticconstexprnoexcept

Definition at line 366 of file random.h.

◆ min()

template<typename T >
static constexpr uint64_t RandomMixin< T >::min ( )
inlinestaticconstexprnoexcept

Definition at line 365 of file random.h.

◆ operator()()

template<typename T >
uint64_t RandomMixin< T >::operator() ( )
inlinenoexcept

Definition at line 367 of file random.h.

Here is the call graph for this function:

◆ operator=() [1/2]

template<typename T >
RandomMixin & RandomMixin< T >::operator= ( const RandomMixin< T > &  )
delete

◆ operator=() [2/2]

template<typename T >
RandomMixin & RandomMixin< T >::operator= ( RandomMixin< T > &&  )
delete

◆ rand()

template<typename T >
template<std::integral I>
I RandomMixin< T >::rand ( )
inlinenoexcept

Generate a random integer in its entire (non-negative) range.

Definition at line 287 of file random.h.

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

◆ rand256()

template<typename T >
uint256 RandomMixin< T >::rand256 ( )
inlinenoexcept

generate a random uint256.

Definition at line 308 of file random.h.

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

◆ rand32()

template<typename T >
uint32_t RandomMixin< T >::rand32 ( )
inlinenoexcept

Generate a random 32-bit integer.

Definition at line 305 of file random.h.

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

◆ rand_exp_duration()

template<typename T >
std::chrono::microseconds RandomMixin< T >::rand_exp_duration ( std::chrono::microseconds  mean)
inlinenoexcept

Return a duration sampled from an exponential distribution (https://en.wikipedia.org/wiki/Exponential_distribution).

Successive events whose intervals are distributed according to this form a memoryless Poisson process. This should be used for repeated network events (e.g. sending a certain type of message) to minimize leaking information to observers.

The probability of an event occurring before time x is 1 - e^-(x/a) where a is the average interval between events.

Definition at line 356 of file random.h.

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

◆ rand_uniform_delay()

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

Return the time point advanced by a uniform random duration.

Definition at line 320 of file random.h.

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

◆ rand_uniform_duration()

template<typename T >
template<typename Chrono >
requires StdChronoDuration<typename Chrono::duration>
Chrono::duration RandomMixin< T >::rand_uniform_duration ( typename Chrono::duration  range)
inlinenoexcept

Generate a uniform random duration in the range from 0 (inclusive) to range (exclusive).

Definition at line 327 of file random.h.

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

◆ randbits() [1/2]

template<typename T >
template<int Bits>
uint64_t RandomMixin< T >::randbits ( )
inlinenoexcept

Same as above, but with compile-time fixed bits count.

Definition at line 230 of file random.h.

Here is the call graph for this function:

◆ randbits() [2/2]

template<typename T >
uint64_t RandomMixin< T >::randbits ( int  bits)
inlinenoexcept

Generate a random (bits)-bit integer.

Definition at line 204 of file random.h.

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

◆ randbool()

template<typename T >
bool RandomMixin< T >::randbool ( )
inlinenoexcept

Generate a random boolean.

Definition at line 316 of file random.h.

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

◆ randbytes()

template<typename T >
template<BasicByte B = unsigned char>
std::vector< B > RandomMixin< T >::randbytes ( size_t  len)
inlinenoexcept

Generate random bytes.

Definition at line 297 of file random.h.

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

◆ randrange() [1/2]

template<typename T >
template<std::integral I>
I RandomMixin< T >::randrange ( range)
inlinenoexcept

Generate a random integer in the range [0..range), with range > 0.

Definition at line 254 of file random.h.

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

◆ randrange() [2/2]

template<typename T >
template<StdChronoDuration Dur>
Dur RandomMixin< T >::randrange ( typename std::common_type_t< Dur >  range)
inlinenoexcept

Generate a uniform random duration in the range [0..max).

Precondition: max.count() > 0

Definition at line 337 of file random.h.

Here is the call graph for this function:

Member Data Documentation

◆ bitbuf

template<typename T >
uint64_t RandomMixin< T >::bitbuf {0}
private

Definition at line 177 of file random.h.

◆ bitbuf_size

template<typename T >
int RandomMixin< T >::bitbuf_size {0}
private

Definition at line 178 of file random.h.


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