geomc 1.0
A c++ linear algebra template library
Public Member Functions | Protected Attributes | List of all members
Random Class Referenceabstract

Abstraction for a source of random or pseudorandom bits. More...

#include <geomc/random/Random.h>

Inheritance diagram for Random:
LCRand MTRand XoshiroRand

Public Member Functions

 Random (void)
 Construct a new random number generator with an implementation-selected seed.
 
 Random (uint64_t seed)
 
virtual uint32_t rand32 ()=0
 
virtual void rseed (uint64_t seed)=0
 Reset the generator's state with the bits of seed as its source of entropy. More...
 
template<typename T >
rand ()
 
template<typename T >
rand (T max)
 
template<typename T >
rand (T lo, T hi)
 
template<>
bool rand ()
 
template<>
unsigned int rand ()
 
template<>
int rand ()
 
template<>
unsigned long rand ()
 
template<>
long rand ()
 
template<>
unsigned long long rand ()
 
template<>
long long rand ()
 
template<>
float rand ()
 
template<>
double rand ()
 
template<>
unsigned int rand (unsigned int hi)
 
template<>
int rand (int hi)
 
template<>
unsigned long rand (unsigned long hi)
 
template<>
long rand (long hi)
 
template<>
unsigned long long rand (unsigned long long hi)
 
template<>
long long rand (long long hi)
 
template<>
float rand (float hi)
 
template<>
double rand (double hi)
 
template<>
int rand (int lo, int hi)
 
template<>
long long rand (long long lo, long long hi)
 
template<>
float rand (float lo, float hi)
 
template<>
double rand (double lo, double hi)
 
template<>
bool rand ()
 
template<>
unsigned int rand ()
 
template<>
int rand ()
 
template<>
unsigned long rand ()
 
template<>
long rand ()
 
template<>
unsigned long long rand ()
 
template<>
long long rand ()
 
template<>
float rand ()
 
template<>
double rand ()
 
template<>
unsigned int rand (unsigned int hi)
 
template<>
int rand (int hi)
 
template<>
unsigned long rand (unsigned long hi)
 
template<>
long rand (long hi)
 
template<>
unsigned long long rand (unsigned long long hi)
 
template<>
long long rand (long long hi)
 
template<>
float rand (float hi)
 
template<>
double rand (double hi)
 
template<>
int rand (int lo, int hi)
 
template<>
long long rand (long long lo, long long hi)
 
template<>
float rand (float lo, float hi)
 
template<>
double rand (double lo, double hi)
 

Protected Attributes

uint32_t _bitpool
 
unsigned int _bitsleft
 

Detailed Description

Abstraction for a source of random or pseudorandom bits.

Implementations provide random bits by overriding rand32(), which should return no fewer than 32 random bits.

Client-friendly random numbers are exposed via Random::rand<T>(). Specializations are provided for a selection of primitive T. Specializations of rand() for other T may be written, which will automatically be inherited by all subclasses of Random.

The general contract of rand() which should be followed by all specializations:

rand(void)

rand(T max)

rand(T lo, T hi)

Constructor & Destructor Documentation

◆ Random()

Random ( uint64_t  seed)

Construct a new random number generator with the bits of seed as a source of entropy.

Member Function Documentation

◆ rand() [1/24]

T rand ( void  )
inline

Produces a (pseudo-) random T with uniform distribution over the unit interval, if the unit interval is populated by T; or with uniform distribution over the entire space of possible T otherwise.

For example, rand<float>() returns a random float between 0 and 1.0, while rand<unsigned int>() returns a random uint between 0 and UINT_MAX.

◆ rand() [2/24]

bool rand ( )
Returns
A random boolean value.

◆ rand() [3/24]

unsigned int rand ( )
Returns
An unsigned int between 0 and UINT_MAX.

◆ rand() [4/24]

int rand ( )
Returns
An int between INT_MIN and INT_MAX.

◆ rand() [5/24]

unsigned long rand ( )
Returns
An unsigned long between 0 and ULONG_MAX.

◆ rand() [6/24]

long rand ( )
Returns
A long between LONG_MIN and LONG_MAX.

◆ rand() [7/24]

unsigned long long rand ( )
Returns
An unsigned long long betwen 0 and ULLONG_MAX.

◆ rand() [8/24]

long long rand ( )
Returns
A long long between LLONG_MIN and LLONG_MAX.

◆ rand() [9/24]

float rand ( )

Generate a floating-point value with uniform distribution between 0 and 1.0.

This method is preferable to use over the common practice of rand<int>() / (float)INT_MAX, as the latter excludes a large fraction of the representable floating point numbers between 0.0 and 1.0 (particularly those near zero), resulting in reduced entropy.

This method produces a number by carefully picking the bits of the exponent and mantissa explicitly, in such a way that the distibution remains uniform.

◆ rand() [10/24]

double rand ( )

Generate a floating-point value with uniform distribution between 0 and 1.0.

This method is preferable to use over the common practice of rand<int>() / (float)INT_MAX, as the latter excludes a large fraction of the representable floating point numbers between 0.0 and 1.0 (particularly those near zero), resulting in reduced entropy.

This method produces a number by carefully picking the bits of the exponent and mantissa explicitly, in such a way that the distibution remains uniform.

◆ rand() [11/24]

double rand ( double  hi)
Parameters
hiUpper or lower bound of random number.
Returns
A double between 0 and hi, regardless of the sign of hi.

◆ rand() [12/24]

double rand ( double  lo,
double  hi 
)
Parameters
loLower bound of random number.
hiUpper bound of random number.
Returns
An double between lo and hi.

◆ rand() [13/24]

float rand ( float  hi)
Parameters
hiUpper or lower bound of random number.
Returns
A float between 0 and hi, regardless of the sign of hi.

◆ rand() [14/24]

float rand ( float  lo,
float  hi 
)
Parameters
loLower bound of random number.
hiUpper bound of random number.
Returns
An float between lo and hi.

◆ rand() [15/24]

int rand ( int  hi)
Parameters
hiUpper or lower bound of random number.
Returns
A number between 0 and hi (regardless of the sign of hi).

◆ rand() [16/24]

int rand ( int  lo,
int  hi 
)
Parameters
loLower bound of random number.
hiUpper bound of random number.
Returns
An int between lo and hi.

◆ rand() [17/24]

long rand ( long  hi)
Parameters
hiUpper or lower bound of random number.
Returns
A long between 0 and hi, regardless of the sign of hi.

◆ rand() [18/24]

long long rand ( long long  hi)
Parameters
hiUpper or lower bound of random number.
Returns
A long long between 0 and hi, regardless of the sign of hi.

◆ rand() [19/24]

long long rand ( long long  lo,
long long  hi 
)
Parameters
loLower bound of random number.
hiUpper bound of random number.
Returns
A long long between lo and hi.

◆ rand() [20/24]

T rand ( lo,
hi 
)
inline

Produces a (pseudo-) random T with uniform distribution between lo and hi.

Parameters
loLower bound for possible T samples.
hiUpper bound for possible T samples.

◆ rand() [21/24]

T rand ( max)
inline

Produces a (pseudo-) random T with uniform distribution between the T analogue of 0 and max.

Parameters
maxUpper bound of possible T samples.

◆ rand() [22/24]

unsigned int rand ( unsigned int  hi)
Parameters
hiUpper bound of random number.
Returns
An unsigned int between 0 and hi.

◆ rand() [23/24]

unsigned long rand ( unsigned long  hi)
Parameters
hiUpper bound of random number.
Returns
An unsigned long between 0 and hi.

◆ rand() [24/24]

unsigned long long rand ( unsigned long long  hi)
Parameters
hiUpper bound of random number.
Returns
An unsigned long long between 0 and hi.

◆ rand32()

virtual uint32_t rand32 ( )
pure virtual
Returns
No fewer than 32 (pseudo-) random bits.

Implemented in LCRand, MTRand, and XoshiroRand.

◆ rseed()

virtual void rseed ( uint64_t  seed)
pure virtual

Reset the generator's state with the bits of seed as its source of entropy.

Implemented in LCRand, MTRand, and XoshiroRand.


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