LSST Applications g0265f82a02+0e5473021a,g02d81e74bb+f5613e8b4f,g1470d8bcf6+190ad2ba91,g14a832a312+311607e4ab,g2079a07aa2+86d27d4dc4,g2305ad1205+a8e3196225,g295015adf3+b67ee847e5,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g3ddfee87b4+a761f810f3,g487adcacf7+17c8fdbcbd,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+65b5bd823e,g5a732f18d5+53520f316c,g64a986408d+f5613e8b4f,g6c1bc301e9+51106c2951,g858d7b2824+f5613e8b4f,g8a8a8dda67+585e252eca,g99cad8db69+6729933424,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+ef4e3a5875,gb0e22166c9+60f28cb32d,gb6a65358fc+0e5473021a,gba4ed39666+c2a2e4ac27,gbb8dafda3b+e9bba80f27,gc120e1dc64+eee469a5e5,gc28159a63d+0e5473021a,gcf0d15dbbd+a761f810f3,gdaeeff99f8+f9a426f77a,ge6526c86ff+d4c1d4bfef,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gf1cff7945b+f5613e8b4f,w.2024.16
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 {
134 if (names.size() == 0) {
135 for (auto _algorithmName : _algorithmNames) {
136 names.emplace_back(_algorithmName);
137 }
138 }
139 return names;
140}

◆ 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: