LSST Applications g0f08755f38+9c285cab97,g1635faa6d4+13f3999e92,g1653933729+a8ce1bb630,g1a0ca8cf93+bf6eb00ceb,g28da252d5a+0829b12dee,g29321ee8c0+5700dc9eac,g2bbee38e9b+9634bc57db,g2bc492864f+9634bc57db,g2cdde0e794+c2c89b37c4,g3156d2b45e+41e33cbcdc,g347aa1857d+9634bc57db,g35bb328faa+a8ce1bb630,g3a166c0a6a+9634bc57db,g3e281a1b8c+9f2c4e2fc3,g414038480c+077ccc18e7,g41af890bb2+fde0dd39b6,g5fbc88fb19+17cd334064,g781aacb6e4+a8ce1bb630,g80478fca09+55a9465950,g82479be7b0+d730eedb7d,g858d7b2824+9c285cab97,g9125e01d80+a8ce1bb630,g9726552aa6+10f999ec6a,ga5288a1d22+2a84bb7594,gacf8899fa4+c69c5206e8,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+9634bc57db,gcf0d15dbbd+4b7d09cae4,gda3e153d99+9c285cab97,gda6a2b7d83+4b7d09cae4,gdaeeff99f8+1711a396fd,ge2409df99d+5e831397f4,ge79ae78c31+9634bc57db,gf0baf85859+147a0692ba,gf3967379c6+41c94011de,gf3fb38a9a8+8f07a9901b,gfb92a5be7c+9c285cab97,w.2024.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
lsst::afw::math::Random Class Referencefinal

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...
 
using State = std::string
 Accessors for the opaque state of the random number generator.
 

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.
 
 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.
 
 Random (Random const &)=default
 
 Random (Random &&)=default
 
Randomoperator= (Random const &)=default
 
Randomoperator= (Random &&)=default
 
 ~Random ()=default
 
Random deepCopy () const
 Creates a deep copy of this random number generator.
 
State getState () const
 
void setState (State const &state)
 
std::size_t getStateSize () const
 
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.
 
double uniformPos ()
 Returns a uniformly distributed random double precision floating point number from the generator.
 
unsigned long uniformInt (unsigned long n)
 Returns a uniformly distributed random integer from 0 to n-1.
 
double flat (double const a, double const b)
 Returns a random variate from the flat (uniform) distribution on [a, b).
 
double gaussian ()
 Returns a gaussian random variate with mean 0 and standard deviation 1
 
double chisq (double const nu)
 Returns a random variate from the chi-squared distribution with nu degrees of freedom.
 
double poisson (double const mu)
 Returns a random variate from the poisson distribution with mean mu.
 

Static Public Member Functions

static std::vector< std::string > const & getAlgorithmNames ()
 

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.

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

Definition at line 57 of file Random.h.

Member Typedef Documentation

◆ 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 154 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 60 of file Random.h.

60 {
62 MT19937 = 0,
65 RANLXS0,
68 RANLXS1,
71 RANLXS2,
73 RANLXD1,
75 RANLXD2,
77 RANLUX,
81 CMRG,
83 MRG,
85 TAUS,
88 TAUS2,
90 GFSR4,
93 };
@ MT19937
The MT19937 "Mersenne Twister" generator of Makoto Matsumoto and Takuji Nishimura.
Definition Random.h:62
@ TAUS
A maximally equidistributed combined Tausworthe generator by L'Ecuyer.
Definition Random.h:85
@ RANLUX
Original version of the RANLUX algorithm, 24-bit output.
Definition Random.h:77
@ RANLXS2
Second-generation version of the RANLUX algorithm of Lüscher, 24-bit output, luxury level 2 (stronges...
Definition Random.h:71
@ RANLXD1
Double precision (48-bit) output using the RANLXS algorithm, luxury level 1 (weakest).
Definition Random.h:73
@ NUM_ALGORITHMS
Number of supported algorithms.
Definition Random.h:92
@ MRG
Fifth-order multiple recursive generator by L'Ecuyer, Blouin, and Coutre.
Definition Random.h:83
@ GFSR4
A fifth-order multiple recursive generator by L'Ecuyer, Blouin, and Coutre.
Definition Random.h:90
@ RANLXS1
Second-generation version of the RANLUX algorithm of Lüscher, 24-bit output, luxury level 1 (stronger...
Definition Random.h:68
@ RANLXD2
Double precision (48-bit) output using the RANLXS algorithm, luxury level 2 (strongest).
Definition Random.h:75
@ RANLXS0
Second-generation version of the RANLUX algorithm of Lüscher, 24-bit output, luxury level 0 (weakest)
Definition Random.h:65
@ CMRG
Combined multiple recursive generator by L'Ecuyer.
Definition Random.h:81
@ TAUS2
A maximally equidistributed combined Tausworthe generator by L'Ecuyer with improved seeding relative ...
Definition Random.h:88
@ RANLUX389
Original version of the RANLUX algorithm, 24-bit output (all bits are decorrelated).
Definition Random.h:79

Constructor & Destructor Documentation

◆ Random() [1/4]

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.

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.
std::bad_allocThrown if memory allocation for internal generator state fails.

Definition at line 89 of file Random.cc.

89 : _rng(), _seed(seed), _algorithm(algorithm) {
90 if (_algorithm < 0 || _algorithm >= NUM_ALGORITHMS) {
91 throw LSST_EXCEPT(ex::InvalidParameterError, "Invalid RNG algorithm");
92 }
93 initialize();
94}
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
Reports invalid arguments.
Definition Runtime.h:66

◆ Random() [2/4]

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.

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.
std::bad_allocThrown if memory allocation for internal generator state fails.

Definition at line 96 of file Random.cc.

96 : _rng(), _seed(seed) {
97 initialize(algorithm);
98}

◆ Random() [3/4]

lsst::afw::math::Random::Random ( Random const & )
default

◆ Random() [4/4]

lsst::afw::math::Random::Random ( Random && )
default

◆ ~Random()

lsst::afw::math::Random::~Random ( )
default

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 163 of file Random.cc.

163{ return ::gsl_ran_chisq(_rng.get(), nu); }
T get(T... args)

◆ 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
std::bad_allocThrown if memory allocation for internal generator state fails.

Definition at line 100 of file Random.cc.

100 {
101 Random rng = *this;
102 rng._rng.reset(::gsl_rng_clone(_rng.get()), ::gsl_rng_free);
103 if (!rng._rng) {
104 throw std::bad_alloc();
105 }
106 return rng;
107}
Random(Algorithm algorithm=MT19937, unsigned long seed=1)
Creates a random number generator that uses the given algorithm to produce random numbers,...
Definition Random.cc:89

◆ 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 159 of file Random.cc.

159{ return ::gsl_ran_flat(_rng.get(), a, b); }
table::Key< int > b

◆ 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 161 of file Random.cc.

161{ return ::gsl_ran_gaussian_ziggurat(_rng.get(), 1.0); }

◆ getAlgorithm()

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

Definition at line 128 of file Random.cc.

128{ return _algorithm; }

◆ 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 130 of file Random.cc.

130{ return std::string(_algorithmNames[_algorithm]); }

◆ 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 132 of file Random.cc.

132 {
133 static std::vector<std::string> names;
134 if (names.size() == 0) {
135 for (auto _algorithmName : _algorithmNames) {
136 names.emplace_back(_algorithmName);
137 }
138 }
139 return names;
140}
T emplace_back(T... args)
T size(T... args)

◆ getSeed()

unsigned long lsst::afw::math::Random::getSeed ( ) const
Returns
The integer this random number generator was seeded with.
Note
The seed is guaranteed not to be zero.

Definition at line 142 of file Random.cc.

142{ return _seed; }

◆ getState()

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

Definition at line 109 of file Random.cc.

109 {
110 return State(static_cast<char *>(::gsl_rng_state(_rng.get())), getStateSize());
111}
std::string State
Accessors for the opaque state of the random number generator.
Definition Random.h:154
std::size_t getStateSize() const
Definition Random.cc:124

◆ getStateSize()

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

Definition at line 124 of file Random.cc.

124{ return ::gsl_rng_size(_rng.get()); }

◆ operator=() [1/2]

Random & lsst::afw::math::Random::operator= ( Random && )
default

◆ operator=() [2/2]

Random & lsst::afw::math::Random::operator= ( Random const & )
default

◆ 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 165 of file Random.cc.

165{ return ::gsl_ran_poisson(_rng.get(), mu); }

◆ setState()

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

Definition at line 113 of file Random.cc.

113 {
114 if (state.size() != getStateSize()) {
115 throw LSST_EXCEPT(
116 pex::exceptions::LengthError,
117 (boost::format("Size of given state vector (%d) does not match expected size (%d)") %
118 state.size() % getStateSize())
119 .str());
120 }
121 std::copy(state.begin(), state.end(), static_cast<char *>(::gsl_rng_state(_rng.get())));
122}
T copy(T... args)

◆ 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 146 of file Random.cc.

146{ return ::gsl_rng_uniform(_rng.get()); }

◆ 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 150 of file Random.cc.

150 {
151 if (n > ::gsl_rng_max(_rng.get()) - ::gsl_rng_min(_rng.get())) {
152 throw LSST_EXCEPT(ex::RangeError, "Desired random number range exceeds generator range");
153 }
154 return ::gsl_rng_uniform_int(_rng.get(), n);
155}
Reports when the result of an operation cannot be represented by the destination type.
Definition Runtime.h:115

◆ 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 148 of file Random.cc.

148{ return ::gsl_rng_uniform_pos(_rng.get()); }

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