Bitcoin ABC 0.32.5
P2P Digital Currency
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

===================== RANDOM NUMBER GENERATION CLASSES ===================== 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 = uint8_t>
std::vector< B > randbytes (size_t len) noexcept
 Generate 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...
 
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 >

===================== RANDOM NUMBER GENERATION CLASSES =====================

Mixin class that provides helper randomness functions.

In this section, 3 classes are defined:

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

Member Typedef Documentation

◆ result_type

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

Definition at line 397 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 281 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 201 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 196 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 399 of file random.h.

◆ min()

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

Definition at line 398 of file random.h.

◆ operator()()

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

Definition at line 402 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 299 of file random.h.

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

◆ rand160()

template<typename T >
uint160 RandomMixin< T >::rand160 ( )
inlinenoexcept

generate a random uint160.

Definition at line 321 of file random.h.

Here is the call graph for this function:

◆ rand256()

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

generate a random uint256.

Definition at line 328 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 318 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 389 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 339 of file random.h.

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 351 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 244 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 216 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 335 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 = uint8_t>
std::vector< B > RandomMixin< T >::randbytes ( size_t  len)
inlinenoexcept

Generate random bytes.

Definition at line 311 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 266 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 368 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 186 of file random.h.

◆ bitbuf_size

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

Definition at line 187 of file random.h.


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