LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
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. 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 (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. More...
 
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. 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 ()
 

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,
79  RANLUX389,
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
table::Key< int > a

◆ 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)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174

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