LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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)
Definition: RandomImage.cc:112
std::string State
Definition: Random.h:116
Random(Algorithm algorithm=MT19937, unsigned long seed=1)
Definition: Random.cc:150
double poisson(double const nu)
Definition: Random.cc:392
Algorithm _algorithm
Definition: Random.h:142
std::shared_ptr< Policy > Ptr
Definition: Policy.h:172
unsigned long _seed
Definition: Random.h:141
unsigned long uniformInt(unsigned long n)
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)
Definition: RandomImage.cc:143
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
std::size_t getStateSize() const
Definition: Random.cc:249
void randomUniformPosImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:122
static char const *const _algorithmEnvVarName
Definition: Random.h:146
void setState(State const &state)
Definition: Random.cc:238
void randomChisqImage(ImageT *image, Random &rand, double const nu)
Definition: RandomImage.cc:165
Algorithm getAlgorithm() const
Definition: Random.cc:258
void randomGaussianImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:155
double chisq(double const nu)
Definition: Random.cc:382
std::string getAlgorithmName() const
Definition: Random.cc:265
double flat(double const a, double const 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
void randomUniformIntImage(ImageT *image, Random &rand, unsigned long n)
Definition: RandomImage.cc:132
void randomPoissonImage(ImageT *image, Random &rand, double const mu)
Definition: RandomImage.cc:177
afw::table::Key< double > b
Include files required for standard LSST Exception handling.
Random deepCopy() const
Definition: Random.cc:225
State getState() const
Definition: Random.cc:234
::gsl_rng_type const *const _gslRngTypes[NUM_ALGORITHMS]
Definition: Random.h:144
static std::vector< std::string > const & getAlgorithmNames()
Definition: Random.cc:272