Bitcoin ABC 0.32.5
P2P Digital Currency
Public Types | Public Member Functions | Static Public 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

 RandomMixin () noexcept=default
 
 RandomMixin (const RandomMixin &)=delete
 
RandomMixinoperator= (const RandomMixin &)=delete
 
 RandomMixin (RandomMixin &&other) noexcept
 
RandomMixinoperator= (RandomMixin &&other) noexcept
 
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...
 
uint64_t randrange (uint64_t range) noexcept
 Generate a random integer in the range [0..range). More...
 
void fillrand (Span< std::byte > span) noexcept
 Fill a Span with random bytes. 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...
 
uint64_t operator() () noexcept
 

Static Public Member Functions

static constexpr uint64_t min () noexcept
 
static constexpr uint64_t max () 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 186 of file random.h.

Member Typedef Documentation

◆ result_type

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

Definition at line 351 of file random.h.

Constructor & Destructor Documentation

◆ RandomMixin() [1/3]

template<typename T >
RandomMixin< T >::RandomMixin ( )
defaultnoexcept

◆ RandomMixin() [2/3]

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

◆ RandomMixin() [3/3]

template<typename T >
RandomMixin< T >::RandomMixin ( RandomMixin< T > &&  other)
inlinenoexcept

Definition at line 209 of file random.h.

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

Here is the call graph for this function:

◆ 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 198 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 353 of file random.h.

◆ min()

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

Definition at line 352 of file random.h.

◆ operator()()

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

Definition at line 356 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 > &&  other)
inlinenoexcept

Definition at line 215 of file random.h.

◆ rand160()

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

generate a random uint160.

Definition at line 318 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 325 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 315 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 336 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 252 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 224 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 332 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 308 of file random.h.

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

◆ randrange()

template<typename T >
uint64_t RandomMixin< T >::randrange ( uint64_t  range)
inlinenoexcept

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

Precondition: range > 0.

Definition at line 276 of file random.h.

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

Member Data Documentation

◆ bitbuf

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

Definition at line 188 of file random.h.

◆ bitbuf_size

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

Definition at line 189 of file random.h.


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