LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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 "boost/shared_ptr.hpp"
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  boost::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:151
double poisson(double const nu)
Definition: Random.cc:390
Include files required for standard LSST Exception handling.
Algorithm _algorithm
Definition: Random.h:142
boost::shared_ptr< Policy > Ptr
Definition: Policy.h:172
unsigned long _seed
Definition: Random.h:141
unsigned long uniformInt(unsigned long n)
Definition: Random.cc:341
unsigned long getSeed() const
Definition: Random.cc:285
static char const *const _seedEnvVarName
Definition: Random.h:147
boost::shared_ptr< ::gsl_rng > _rng
Definition: Random.h:140
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:247
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:236
void randomChisqImage(ImageT *image, Random &rand, double const nu)
Definition: RandomImage.cc:165
Algorithm getAlgorithm() const
Definition: Random.cc:256
void randomGaussianImage(ImageT *image, Random &rand)
Definition: RandomImage.cc:155
double chisq(double const nu)
Definition: Random.cc:380
std::string getAlgorithmName() const
Definition: Random.cc:263
double flat(double const a, double const b)
Definition: Random.cc:358
static char const *const _algorithmNames[NUM_ALGORITHMS]
Definition: Random.h:145
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
Random deepCopy() const
Definition: Random.cc:223
State getState() const
Definition: Random.cc:232
::gsl_rng_type const *const _gslRngTypes[NUM_ALGORITHMS]
Definition: Random.h:144
static std::vector< std::string > const & getAlgorithmNames()
Definition: Random.cc:270