Empirical
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
emp::Random Class Reference

A versatile and non-patterned pseudo-random-number generator (Mersenne Twister). More...

#include <ce_random.h>

Public Member Functions

constexpr Random (const int _seed=-1)
 
 ~Random ()=default
 
constexpr int GetSeed () const
 
constexpr int GetOriginalSeed () const
 
constexpr void ResetSeed (const int _seed)
 
constexpr double GetDouble ()
 
constexpr double GetDouble (const double max)
 
constexpr double GetDouble (const double min, const double max)
 
constexpr uint32_t GetUInt (const uint32_t max)
 
constexpr uint32_t GetUInt (const uint32_t min, const uint32_t max)
 
constexpr int32_t GetInt (const int max)
 
constexpr int32_t GetInt (const int min, const int max)
 
constexpr bool P (const double _p)
 
constexpr double GetRandNormal ()
 
constexpr double GetRandNormal (const double mean, const double std)
 
constexpr uint32_t GetRandPoisson (const double mean)
 
constexpr uint32_t GetRandPoisson (const double n, double p)
 
constexpr uint32_t GetFullRandBinomial (const double n, const double p)
 
constexpr uint32_t GetRandBinomial (const double n, const double p)
 
 Random (const int _seed=-1)
 
 ~Random ()
 
int GetSeed () const
 
int GetOriginalSeed () const
 
void ResetSeed (const int _seed)
 
double GetDouble ()
 
double GetDouble (const double max)
 
double GetDouble (const double min, const double max)
 
double GetDouble (const Range< double > range)
 
template<typename T >
uint32_t GetUInt (const T max)
 
template<typename T >
uint64_t GetUInt64 (const T max)
 
template<typename T1 , typename T2 >
uint32_t GetUInt (const T1 min, const T2 max)
 
template<typename T >
uint32_t GetUInt (const Range< T > range)
 
int GetInt (const int max)
 
int GetInt (const int min, const int max)
 
int GetInt (const Range< int > range)
 
bool P (const double p)
 
double GetRandNormal ()
 
double GetRandNormal (const double mean, const double std)
 
uint32_t GetRandPoisson (const double n, double p)
 
uint32_t GetRandPoisson (const double mean)
 
uint32_t GetFullRandBinomial (const double n, const double p)
 
uint32_t GetRandBinomial (const double n, const double p)
 

Protected Member Functions

constexpr void init ()
 
constexpr uint32_t Get ()
 
void init ()
 
int32_t Get ()
 

Protected Attributes

int seed
 Current random number seed. More...
 
int original_seed
 Orignal random number seed when object was first created. More...
 
int inext
 First position in use in internal state. More...
 
int inextp
 Second position in use in internal state. More...
 
int ma [56] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
 Internal state of RNG. More...
 
double expRV
 

Static Protected Attributes

static const uint32_t _BINOMIAL_TO_NORMAL = 50
 
static const uint32_t _BINOMIAL_TO_POISSON = 1000
 
static const uint32_t _RAND_MBIG = 1000000000
 
static const uint32_t _RAND_MSEED = 161803398
 
static const int32_t _BINOMIAL_TO_NORMAL = 50
 
static const int32_t _BINOMIAL_TO_POISSON = 1000
 
static const int32_t _RAND_MBIG = 1000000000
 
static const int32_t _RAND_MSEED = 161803398
 

Detailed Description

A versatile and non-patterned pseudo-random-number generator (Mersenne Twister).

Constructor & Destructor Documentation

constexpr emp::Random::Random ( const int  _seed = -1)
inline

Set up the random generator object.

Parameters
_seedThe seed of the random number generator. A negative seed means that the random number generator gets its seed from the actual system time.
emp::Random::~Random ( )
default
emp::Random::Random ( const int  _seed = -1)
inline

Set up the random generator object.

Parameters
_seedThe seed of the random number generator. A negative seed means that the random number generator gets its seed from a combination of the actual system time and the memory position of the random number generator.
emp::Random::~Random ( )
inline

Member Function Documentation

int32_t emp::Random::Get ( )
inlineprotected
constexpr uint32_t emp::Random::Get ( )
inlineprotected
double emp::Random::GetDouble ( )
inline

Generate a double between 0.0 and 1.0

Returns
The pseudo random number.
double emp::Random::GetDouble ( const double  max)
inline

Generate a double between 0 and a given number.

Returns
The pseudo random number.
Parameters
maxThe upper bound for the random numbers (will never be returned).
constexpr double emp::Random::GetDouble ( )
inline

Generate a double between 0.0 and 1.0

Returns
The pseudo random number.
double emp::Random::GetDouble ( const double  min,
const double  max 
)
inline

Generate a double out of a given interval.

Returns
The pseudo random number.
Parameters
minThe lower bound for the random numbers.
maxThe upper bound for the random numbers (will never be returned).
constexpr double emp::Random::GetDouble ( const double  max)
inline

Generate a double between 0 and a given number.

Returns
The pseudo random number.
Parameters
maxThe upper bound for the random numbers (will never be returned).
double emp::Random::GetDouble ( const Range< double >  range)
inline

Generate a double out of a given interval.

Returns
The pseudo random number.
Parameters
rangeThe upper and lower bounds for the random numbers [lower, upper)
constexpr double emp::Random::GetDouble ( const double  min,
const double  max 
)
inline

Generate a double out of a given interval.

Returns
The pseudo random number.
Parameters
minThe lower bound for the random numbers.
maxThe upper bound for the random numbers (will never be returned).
constexpr uint32_t emp::Random::GetFullRandBinomial ( const double  n,
const double  p 
)
inline

Generate a random variable drawn from a Binomial distribution.

This function is exact, but slow.

See also
Random::GetRandBinomial
uint32_t emp::Random::GetFullRandBinomial ( const double  n,
const double  p 
)
inline

Generate a random variable drawn from a Binomial distribution.

This function is exact, but slow.

See also
Random::GetRandBinomial
constexpr int32_t emp::Random::GetInt ( const int  max)
inline

Generate an int out of an interval.

Returns
The pseudo random number.
Parameters
minThe lower bound for the random numbers.
maxThe upper bound for the random numbers (will never be returned).
constexpr int32_t emp::Random::GetInt ( const int  min,
const int  max 
)
inline
int emp::Random::GetInt ( const int  max)
inline

Generate an int out of an interval.

Returns
The pseudo random number.
Parameters
minThe lower bound for the random numbers.
maxThe upper bound for the random numbers (will never be returned).
int emp::Random::GetInt ( const int  min,
const int  max 
)
inline
int emp::Random::GetInt ( const Range< int >  range)
inline
int emp::Random::GetOriginalSeed ( ) const
inline
Returns
The seed that was originally provided by the user.
constexpr int emp::Random::GetOriginalSeed ( ) const
inline
Returns
The seed that was originally provided by the user.
constexpr uint32_t emp::Random::GetRandBinomial ( const double  n,
const double  p 
)
inline

Generate a random variable drawn from a Binomial distribution.

This function is faster than Random::GetFullRandBinomial(), but uses some approximations.

See also
Random::GetFullRandBinomial
uint32_t emp::Random::GetRandBinomial ( const double  n,
const double  p 
)
inline

Generate a random variable drawn from a Binomial distribution.

This function is faster than Random::GetFullRandBinomial(), but uses some approximations.

See also
Random::GetFullRandBinomial
constexpr double emp::Random::GetRandNormal ( )
inline

Generate a random variable drawn from a unit normal distribution.

constexpr double emp::Random::GetRandNormal ( const double  mean,
const double  std 
)
inline

Generate a random variable drawn from a distribution with given mean and standard deviation.

double emp::Random::GetRandNormal ( )
inline

Generate a random variable drawn from a unit normal distribution.

double emp::Random::GetRandNormal ( const double  mean,
const double  std 
)
inline

Generate a random variable drawn from a distribution with given mean and standard deviation.

constexpr uint32_t emp::Random::GetRandPoisson ( const double  mean)
inline

Generate a random variable drawn from a Poisson distribution.

Parameters
meanThe mean of the distribution.
constexpr uint32_t emp::Random::GetRandPoisson ( const double  n,
double  p 
)
inline

Generate a random variable drawn from a Poisson distribution.

uint32_t emp::Random::GetRandPoisson ( const double  n,
double  p 
)
inline

Generate a random variable drawn from a Poisson distribution.

uint32_t emp::Random::GetRandPoisson ( const double  mean)
inline

Generate a random variable drawn from a Poisson distribution.

Parameters
meanThe mean of the distribution.
int emp::Random::GetSeed ( ) const
inline
Returns
The seed that was actually used to start the random sequence.
constexpr int emp::Random::GetSeed ( ) const
inline
Returns
The seed that was actually used to start the random sequence.
constexpr uint32_t emp::Random::GetUInt ( const uint32_t  max)
inline

Generate an uint32_t.

Returns
The pseudo random number.
Parameters
maxThe upper bound for the random numbers (will never be returned).
template<typename T >
uint32_t emp::Random::GetUInt ( const T  max)
inline

Generate an uint32_t.

Returns
The pseudo random number.
Parameters
maxThe upper bound for the random numbers (will never be returned).
constexpr uint32_t emp::Random::GetUInt ( const uint32_t  min,
const uint32_t  max 
)
inline

Generate an uint32_t out of an interval.

Returns
The pseudo random number.
Parameters
minThe lower bound for the random numbers.
maxThe upper bound for the random numbers (will never be returned).
template<typename T1 , typename T2 >
uint32_t emp::Random::GetUInt ( const T1  min,
const T2  max 
)
inline

Generate an uint32_t out of an interval.

Returns
The pseudo random number.
Parameters
minThe lower bound for the random numbers.
maxThe upper bound for the random numbers (will never be returned).
template<typename T >
uint32_t emp::Random::GetUInt ( const Range< T >  range)
inline

Generate a uint32_t out of a given interval.

Returns
The pseudo random number.
Parameters
rangeThe upper and lower bounds for the random numbers [lower, upper)
template<typename T >
uint64_t emp::Random::GetUInt64 ( const T  max)
inline

Generate an uint64_t.

Returns
The pseudo random number.
Parameters
maxThe upper bound for the random numbers (will never be returned).
Todo:
this function needs to be tested and refined.
void emp::Random::init ( )
inlineprotected
constexpr void emp::Random::init ( )
inlineprotected
constexpr bool emp::Random::P ( const double  _p)
inline
bool emp::Random::P ( const double  p)
inline

Tests a random value [0,1) against a given probability p, and returns true of false.

Parameters
pThe probability of the result being "true".
void emp::Random::ResetSeed ( const int  _seed)
inline

Starts a new sequence of pseudo random numbers.

Parameters
new_seedThe seed for the new sequence. A negative seed means that the random number generator gets its seed from the actual system time and the process ID.
constexpr void emp::Random::ResetSeed ( const int  _seed)
inline

Starts a new sequence of pseudo random numbers.

Parameters
new_seedThe seed for the new sequence. A negative seed means that the random number generator gets its seed from the actual system time and the process ID.

Member Data Documentation

const int32_t emp::Random::_BINOMIAL_TO_NORMAL = 50
staticprotected
const uint32_t emp::Random::_BINOMIAL_TO_NORMAL = 50
staticprotected
const int32_t emp::Random::_BINOMIAL_TO_POISSON = 1000
staticprotected
const uint32_t emp::Random::_BINOMIAL_TO_POISSON = 1000
staticprotected
const int32_t emp::Random::_RAND_MBIG = 1000000000
staticprotected
const uint32_t emp::Random::_RAND_MBIG = 1000000000
staticprotected
const int32_t emp::Random::_RAND_MSEED = 161803398
staticprotected
const uint32_t emp::Random::_RAND_MSEED = 161803398
staticprotected
double emp::Random::expRV
protected
int emp::Random::inext
protected

First position in use in internal state.

int emp::Random::inextp
protected

Second position in use in internal state.

int emp::Random::ma = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
protected

Internal state of RNG.

int emp::Random::original_seed
protected

Orignal random number seed when object was first created.

int emp::Random::seed
protected

Current random number seed.


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