lsst.afw  13.0-110-ge674a0930
Public Types | Public Member Functions | Static Public Member Functions | List of all members
lsst::afw::math::Random Class Reference

A class that can be used to generate sequences of random numbers according to a number of different algorithms. More...

#include <Random.h>

Public Types

enum  Algorithm {
  MT19937 = 0, RANLXS0, RANLXS1, RANLXS2,
  RANLXD1, RANLXD2, RANLUX, RANLUX389,
  CMRG, MRG, TAUS, TAUS2,
  GFSR4, NUM_ALGORITHMS
}
 Identifiers for the list of supported algorithms. More...
 

Public Member Functions

 Random (Algorithm algorithm=MT19937, unsigned long seed=1)
 Creates a random number generator that uses the given algorithm to produce random numbers, and seeds it with the specified value. More...
 
 Random (std::string const &algorithm, unsigned long seed=1)
 Creates a random number generator that uses the algorithm with the given name to produce random numbers, and seeds it with the specified value. More...
 
 Random (std::shared_ptr< pex::policy::Policy > const policy)
 Creates a random number generator using the algorithm and seed specified in the given policy. More...
 
Random deepCopy () const
 Creates a deep copy of this random number generator. More...
 
Algorithm getAlgorithm () const
 
std::string getAlgorithmName () const
 
unsigned long getSeed () const
 
double uniform ()
 Returns a uniformly distributed random double precision floating point number from the generator. More...
 
double uniformPos ()
 Returns a uniformly distributed random double precision floating point number from the generator. More...
 
unsigned long uniformInt (unsigned long n)
 Returns a uniformly distributed random integer from 0 to n-1. More...
 
double flat (double const a, double const b)
 Returns a random variate from the flat (uniform) distribution on [a, b). More...
 
double gaussian ()
 Returns a gaussian random variate with mean 0 and standard deviation 1 More...
 
double chisq (double const nu)
 Returns a random variate from the chi-squared distribution with nu degrees of freedom. More...
 
double poisson (double const mu)
 Returns a random variate from the poisson distribution with mean mu. More...
 

Static Public Member Functions

static std::vector< std::string > const & getAlgorithmNames ()
 
typedef std::string State
 Accessors for the opaque state of the random number generator. More...
 
State getState () const
 Accessors for the opaque state of the random number generator. More...
 
void setState (State const &state)
 Accessors for the opaque state of the random number generator. More...
 
std::size_t getStateSize () const
 Accessors for the opaque state of the random number generator. More...
 

Detailed Description

A class that can be used to generate sequences of random numbers according to a number of different algorithms.

Support for generating random variates from the uniform, Gaussian, Poisson, and chi-squared distributions is provided.

This class is a thin wrapper for the random number generation facilities of GSL, which supports many additional distributions that can easily be added to this class as the need arises.

To enable reproducibility, factory functions which determine the algorithm type and seed value to used based on the the LSST_RNG_ALGORITHM and LSST_RNG_SEED environment variables (or the "rngAlgorithm" and "rngSeed" keys in a policy) are provided.

See also
Random number generation in GSL
Random number distributions in GSL

Definition at line 62 of file Random.h.

Member Typedef Documentation

◆ State

typedef std::string lsst::afw::math::Random::State

Accessors for the opaque state of the random number generator.

These may be used to save the state and restore it later, possibly after persisting. The state is algorithm-dependent, and possibly platform/architecture dependent; it should only be used for debugging perposes.

We use string here because it's a format Python and afw::table understand; the actual value is a binary blob that is not expected to be human-readable.

Definition at line 176 of file Random.h.

Member Enumeration Documentation

◆ Algorithm

Identifiers for the list of supported algorithms.

Enumerator
MT19937 

The MT19937 "Mersenne Twister" generator of Makoto Matsumoto and Takuji Nishimura.

RANLXS0 

Second-generation version of the RANLUX algorithm of Lüscher, 24-bit output, luxury level 0 (weakest)

RANLXS1 

Second-generation version of the RANLUX algorithm of Lüscher, 24-bit output, luxury level 1 (stronger)

RANLXS2 

Second-generation version of the RANLUX algorithm of Lüscher, 24-bit output, luxury level 2 (strongest)

RANLXD1 

Double precision (48-bit) output using the RANLXS algorithm, luxury level 1 (weakest).

RANLXD2 

Double precision (48-bit) output using the RANLXS algorithm, luxury level 2 (strongest).

RANLUX 

Original version of the RANLUX algorithm, 24-bit output.

RANLUX389 

Original version of the RANLUX algorithm, 24-bit output (all bits are decorrelated).

CMRG 

Combined multiple recursive generator by L'Ecuyer.

MRG 

Fifth-order multiple recursive generator by L'Ecuyer, Blouin, and Coutre.

TAUS 

A maximally equidistributed combined Tausworthe generator by L'Ecuyer.

TAUS2 

A maximally equidistributed combined Tausworthe generator by L'Ecuyer with improved seeding relative to TAUS.

GFSR4 

A fifth-order multiple recursive generator by L'Ecuyer, Blouin, and Coutre.

NUM_ALGORITHMS 

Number of supported algorithms.

Definition at line 65 of file Random.h.

Constructor & Destructor Documentation

◆ Random() [1/3]

lsst::afw::math::Random::Random ( Algorithm  algorithm = MT19937,
unsigned long  seed = 1 
)
explicit

Creates a random number generator that uses the given algorithm to produce random numbers, and seeds it with the specified value.

Passing a seed-value of zero will cause the generator to be seeded with an algorithm specific default value. The default value for algorithm is MT19937, corresponding to the "Mersenne Twister" algorithm by Makoto Matsumoto and Takuji Nishimura.

Parameters
[in]algorithmthe algorithm to use for random number generation
[in]seedthe seed value to initialize the generator with
Exceptions
lsst::pex::exceptions::InvalidParameterErrorThrown if the requested algorithm is not supported or a seed value of zero (corresponding to an algorithm specific seed) is chosen.
lsst::pex::exceptions::MemoryErrorThrown if memory allocation for internal generator state fails.

Definition at line 90 of file Random.cc.

◆ Random() [2/3]

lsst::afw::math::Random::Random ( std::string const &  algorithm,
unsigned long  seed = 1 
)
explicit

Creates a random number generator that uses the algorithm with the given name to produce random numbers, and seeds it with the specified value.

Passing a seed-value of zero will cause the generator to be seeded with an algorithm specific default value.

Parameters
[in]algorithmthe name of the algorithm to use for random number generation
[in]seedthe seed value to initialize the generator with
Exceptions
lsst::pex::exceptions::InvalidParameterErrorThrown if the requested algorithm is not supported or a seed value of zero (corresponding to an algorithm specific seed) is chosen.
lsst::pex::exceptions::MemoryErrorThrown if memory allocation for internal generator state fails.

Definition at line 97 of file Random.cc.

◆ Random() [3/3]

lsst::afw::math::Random::Random ( std::shared_ptr< pex::policy::Policy > const  policy)
explicit

Creates a random number generator using the algorithm and seed specified in the given policy.

The algorithm name and seed are expected to be specified in string-valued keys named "rngAlgorithm" and "rngSeed" respectively. The "rngSeed" value is expected to be convertible to an unsigned long integer and must not be positive.

Parameters
[in]policypolicy which contains the algorithm and seed to to use for random number generation
Returns
a newly created random number generator
Exceptions
lsst::pex::exceptions::InvalidParameterErrorThrown if the requested algorithm is not supported.
lsst::pex::exceptions::MemoryErrorThrown if memory allocation for internal generator state fails.
lsst::pex::exceptions::RuntimeErrorThrown if the "rngSeed" policy value cannot be converted to an unsigned long int.

Definition at line 101 of file Random.cc.

Member Function Documentation

◆ chisq()

double lsst::afw::math::Random::chisq ( double const  nu)

Returns a random variate from the chi-squared distribution with nu degrees of freedom.

Parameters
[in]nuthe number of degrees of freedom in the chi-squared distribution
Returns
a random variate from the chi-squared distribution

Definition at line 179 of file Random.cc.

◆ deepCopy()

Random lsst::afw::math::Random::deepCopy ( ) const

Creates a deep copy of this random number generator.

Both this random number and its copy will subsequently produce an identical stream of random numbers.

Returns
a deep copy of this random number generator
Exceptions
lsst::pex::exceptions::MemoryErrorThrown if memory allocation for internal generator state fails.

Definition at line 116 of file Random.cc.

◆ flat()

double lsst::afw::math::Random::flat ( double const  a,
double const  b 
)

Returns a random variate from the flat (uniform) distribution on [a, b).

Parameters
[in]alower endpoint of uniform distribution range (inclusive)
[in]bupper endpoint of uniform distribution range (exclusive)
Returns
a uniform random variate.

Definition at line 175 of file Random.cc.

◆ gaussian()

double lsst::afw::math::Random::gaussian ( )

Returns a gaussian random variate with mean 0 and standard deviation 1

Returns
a gaussian random variate
Note
The implementation uses the Ziggurat algorithm.

Definition at line 177 of file Random.cc.

◆ getAlgorithm()

Random::Algorithm lsst::afw::math::Random::getAlgorithm ( ) const
Returns
The algorithm in use by this random number generator.

Definition at line 144 of file Random.cc.

◆ getAlgorithmName()

std::string lsst::afw::math::Random::getAlgorithmName ( ) const
Returns
The name of the algorithm in use by this random number generator.

Definition at line 146 of file Random.cc.

◆ getAlgorithmNames()

std::vector< std::string > const & lsst::afw::math::Random::getAlgorithmNames ( )
static
Returns
The list of names of supported random number generation algorithms.

Definition at line 148 of file Random.cc.

◆ getSeed()

unsigned long lsst::afw::math::Random::getSeed ( ) const
Returns
The integer this random number generator was seeded with.
Note
A seed value of 0 indicates that the random number generator was seeded with an algorithm specific default value.

Definition at line 158 of file Random.cc.

◆ getState()

Random::State lsst::afw::math::Random::getState ( ) const

Accessors for the opaque state of the random number generator.

These may be used to save the state and restore it later, possibly after persisting. The state is algorithm-dependent, and possibly platform/architecture dependent; it should only be used for debugging perposes.

We use string here because it's a format Python and afw::table understand; the actual value is a binary blob that is not expected to be human-readable.

Definition at line 125 of file Random.cc.

◆ getStateSize()

std::size_t lsst::afw::math::Random::getStateSize ( ) const

Accessors for the opaque state of the random number generator.

These may be used to save the state and restore it later, possibly after persisting. The state is algorithm-dependent, and possibly platform/architecture dependent; it should only be used for debugging perposes.

We use string here because it's a format Python and afw::table understand; the actual value is a binary blob that is not expected to be human-readable.

Definition at line 140 of file Random.cc.

◆ poisson()

double lsst::afw::math::Random::poisson ( double const  mu)

Returns a random variate from the poisson distribution with mean mu.

Parameters
mudesired mean (and variance)
Returns
a random variate from the Poission distribution

Definition at line 181 of file Random.cc.

◆ setState()

void lsst::afw::math::Random::setState ( State const &  state)

Accessors for the opaque state of the random number generator.

These may be used to save the state and restore it later, possibly after persisting. The state is algorithm-dependent, and possibly platform/architecture dependent; it should only be used for debugging perposes.

We use string here because it's a format Python and afw::table understand; the actual value is a binary blob that is not expected to be human-readable.

Definition at line 129 of file Random.cc.

◆ uniform()

double lsst::afw::math::Random::uniform ( )

Returns a uniformly distributed random double precision floating point number from the generator.

The random number will be in the range [0, 1); the range includes 0.0 but excludes 1.0. Note that some algorithms will not produce randomness across all mantissa bits - choose an algorithm that produces double precisions results (such as Random::RANLXD1, Random::TAUS, or Random::MT19937) if this is important.

Returns
a uniformly distributed random double precision floating point number in the range [0, 1).
See also
uniformPositiveDouble()

Definition at line 162 of file Random.cc.

◆ uniformInt()

unsigned long lsst::afw::math::Random::uniformInt ( unsigned long  n)

Returns a uniformly distributed random integer from 0 to n-1.

This function is not intended to generate values across the full range of unsigned integer values [0, 2^32 - 1]. If this is necessary, use a high precision algorithm like Random::RANLXD1, Random::TAUS, or Random::MT19937 with a minimum value of zero and call get() directly.

Parameters
[in]nspecifies the range of allowable return values (0 to n-1)
Returns
a uniformly distributed random integer
Exceptions
lsst::pex::exceptions::RangeErrorThrown if n is larger than the algorithm specific range of the generator.
See also
get()
getMin()
getMax()

Definition at line 166 of file Random.cc.

◆ uniformPos()

double lsst::afw::math::Random::uniformPos ( )

Returns a uniformly distributed random double precision floating point number from the generator.

The random number will be in the range (0, 1); the range excludes both 0.0 and 1.0. Note that some algorithms will not produce randomness across all mantissa bits - choose an algorithm that produces double precisions results (such as Random::RANLXD1, Random::TAUS, or Random::MT19937) if this is important.

Returns
a uniformly distributed random double precision floating point number in the range (0, 1).

Definition at line 164 of file Random.cc.


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