LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
Random.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
32 #ifndef LSST_AFW_MATH_RANDOM_H
33 #define LSST_AFW_MATH_RANDOM_H
34 
35 #include <memory>
36 
37 #include "gsl/gsl_rng.h"
38 
39 #include "lsst/pex/exceptions.h"
40 #include "lsst/pex/policy/Policy.h"
41 
42 
43 namespace lsst { namespace afw { namespace math {
44 
62 class Random {
63 public:
64 
66  enum Algorithm {
68  MT19937 = 0,
86  MRG,
95  };
96 
97  // -- Constructor --------
98  explicit Random(Algorithm algorithm = MT19937, unsigned long seed = 1);
99  explicit Random(std::string const & algorithm, unsigned long seed = 1);
100  explicit Random(lsst::pex::policy::Policy::Ptr const policy);
101  // Use compiler generated destructor and shallow copy constructor/assignment operator
102 
103  Random deepCopy() const;
104 
106 
116  typedef std::string State;
117  State getState() const;
118  void setState(State const & state);
119  std::size_t getStateSize() const;
121 
122  // -- Accessors --------
123  Algorithm getAlgorithm() const;
124  std::string getAlgorithmName() const;
125  static std::vector<std::string> const & getAlgorithmNames();
126  unsigned long getSeed() const;
127 
128  // -- Modifiers: generating random numbers --------
129  double uniform();
130  double uniformPos();
131  unsigned long uniformInt(unsigned long n);
132 
133  // -- Modifiers: computing random variates for various distributions --------
134  double flat(double const a, double const b);
135  double gaussian();
136  double chisq(double const nu);
137  double poisson(double const nu);
138 
139 private:
140  std::shared_ptr< ::gsl_rng> _rng;
141  unsigned long _seed;
143 
144  static ::gsl_rng_type const * const _gslRngTypes[NUM_ALGORITHMS];
145  static char const * const _algorithmNames[NUM_ALGORITHMS];
146  static char const * const _algorithmEnvVarName;
147  static char const * const _seedEnvVarName;
148 
149  void initialize();
150  void initialize(std::string const &);
151 };
152 
153 /************************************************************************************************************/
154 /*
155  * Create Images containing random numbers
156  */
157 template<typename ImageT>
158 void randomUniformImage(ImageT *image, Random &rand);
159 
160 template<typename ImageT>
161 void randomUniformPosImage(ImageT *image, Random &rand);
162 
163 template<typename ImageT>
164 void randomUniformIntImage(ImageT *image, Random &rand, unsigned long n);
165 
166 template<typename ImageT>
167 void randomFlatImage(ImageT *image, Random &rand, double const a, double const b);
168 
169 template<typename ImageT>
170 void randomGaussianImage(ImageT *image, Random &rand);
171 
172 template<typename ImageT>
173 void randomChisqImage(ImageT *image, Random &rand, double const nu);
174 
175 template<typename ImageT>
176 void randomPoissonImage(ImageT *image, Random &rand, double const mu);
177 
178 
179 }}} // end of namespace lsst::afw::math
180 
181 #endif // LSST_AFW_MATH_RANDOM_H
182 
void randomUniformImage(ImageT *image, Random &rand)
Set image to random numbers uniformly distributed in the range [0, 1)
Definition: RandomImage.cc:112
std::shared_ptr< Policy > Ptr
Definition: Policy.h:172
std::string State
Accessors for the opaque state of the random number generator.
Definition: Random.h:116
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:150
double poisson(double const nu)
Returns a random variate from the poisson distribution with mean mu.
Definition: Random.cc:392
Algorithm _algorithm
Definition: Random.h:142
Include files required for standard LSST Exception handling.
Double precision (48-bit) output using the RANLXS algorithm, luxury level 2 (strongest).
Definition: Random.h:78
unsigned long _seed
Definition: Random.h:141
unsigned long uniformInt(unsigned long n)
Returns a uniformly distributed random integer from 0 to n-1.
Definition: Random.cc:343
unsigned long getSeed() const
Definition: Random.cc:287
static char const *const _seedEnvVarName
Definition: Random.h:147
void randomFlatImage(ImageT *image, Random &rand, double const a, double const b)
Set image to random numbers uniformly distributed in the range [a, b)
Definition: RandomImage.cc:143
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
Second-generation version of the RANLUX algorithm of Lüscher, 24-bit output, luxury level 0 (weakest)...
Definition: Random.h:70
std::size_t getStateSize() const
Accessors for the opaque state of the random number generator.
Definition: Random.cc:249
metadata import lsst afw display as afwDisplay n
Fifth-order multiple recursive generator by L&#39;Ecuyer, Blouin, and Coutre.
Definition: Random.h:86
void randomUniformPosImage(ImageT *image, Random &rand)
Set image to random numbers uniformly distributed in the range (0, 1)
Definition: RandomImage.cc:122
Number of supported algorithms.
Definition: Random.h:94
static char const *const _algorithmEnvVarName
Definition: Random.h:146
void setState(State const &state)
Accessors for the opaque state of the random number generator.
Definition: Random.cc:238
Original version of the RANLUX algorithm, 24-bit output (all bits are decorrelated).
Definition: Random.h:82
void randomChisqImage(ImageT *image, Random &rand, double const nu)
Set image to random numbers with a chi^2_{nu} distribution.
Definition: RandomImage.cc:165
Algorithm getAlgorithm() const
Definition: Random.cc:258
void randomGaussianImage(ImageT *image, Random &rand)
Set image to random numbers with a gaussian N(0, 1) distribution.
Definition: RandomImage.cc:155
double chisq(double const nu)
Returns a random variate from the chi-squared distribution with nu degrees of freedom.
Definition: Random.cc:382
std::string getAlgorithmName() const
Definition: Random.cc:265
double flat(double const a, double const b)
Returns a random variate from the flat (uniform) distribution on [a, b).
Definition: Random.cc:360
static char const *const _algorithmNames[NUM_ALGORITHMS]
Definition: Random.h:145
std::shared_ptr< ::gsl_rng > _rng
Definition: Random.h:140
double gaussian()
Returns a gaussian random variate with mean 0 and standard deviation 1.
Definition: Random.cc:372
double uniformPos()
Returns a uniformly distributed random double precision floating point number from the generator...
Definition: Random.cc:321
Algorithm
Identifiers for the list of supported algorithms.
Definition: Random.h:66
Second-generation version of the RANLUX algorithm of Lüscher, 24-bit output, luxury level 1 (stronger...
Definition: Random.h:72
Combined multiple recursive generator by L&#39;Ecuyer.
Definition: Random.h:84
void randomUniformIntImage(ImageT *image, Random &rand, unsigned long n)
Set image to random integers uniformly distributed in the range 0 ...
Definition: RandomImage.cc:132
void randomPoissonImage(ImageT *image, Random &rand, double const mu)
Set image to random numbers with a Poisson distribution with mean mu (n.b.
Definition: RandomImage.cc:177
A fifth-order multiple recursive generator by L&#39;Ecuyer, Blouin, and Coutre.
Definition: Random.h:92
afw::table::Key< double > b
Original version of the RANLUX algorithm, 24-bit output.
Definition: Random.h:80
A maximally equidistributed combined Tausworthe generator by L&#39;Ecuyer with improved seeding relative ...
Definition: Random.h:90
Random deepCopy() const
Creates a deep copy of this random number generator.
Definition: Random.cc:225
Second-generation version of the RANLUX algorithm of Lüscher, 24-bit output, luxury level 2 (stronges...
Definition: Random.h:74
double uniform()
Returns a uniformly distributed random double precision floating point number from the generator...
Definition: Random.cc:306
A class that can be used to generate sequences of random numbers according to a number of different a...
Definition: Random.h:62
State getState() const
Accessors for the opaque state of the random number generator.
Definition: Random.cc:234
::gsl_rng_type const *const _gslRngTypes[NUM_ALGORITHMS]
Definition: Random.h:144
A maximally equidistributed combined Tausworthe generator by L&#39;Ecuyer.
Definition: Random.h:88
The MT19937 &quot;Mersenne Twister&quot; generator of Makoto Matsumoto and Takuji Nishimura.
Definition: Random.h:68
static std::vector< std::string > const & getAlgorithmNames()
Definition: Random.cc:272
Double precision (48-bit) output using the RANLXS algorithm, luxury level 1 (weakest).
Definition: Random.h:76