[3] | 1 | #ifndef __BOOST_RNG__
|
---|
| 2 | #define __BOOST_RNG__
|
---|
| 3 |
|
---|
| 4 | #include <boost/random.hpp>
|
---|
| 5 | #include <boost/math/distributions.hpp>
|
---|
| 6 | #include <ctime>
|
---|
| 7 |
|
---|
| 8 | namespace math {
|
---|
| 9 |
|
---|
| 10 | namespace rng {
|
---|
| 11 |
|
---|
| 12 | /*!
|
---|
| 13 | * \class normal_generator
|
---|
| 14 | * \brief This class is basic normal random number generator \n
|
---|
| 15 | * template id RealType describe number precision (float double ....)\n
|
---|
| 16 | * template id PRNG describe pseudo random number generator ( see boost random)
|
---|
| 17 | */
|
---|
| 18 | template < class RealType , class PRNG > class normal_generator {
|
---|
| 19 | public :
|
---|
| 20 | /*!
|
---|
| 21 | * Default construtor
|
---|
| 22 | */
|
---|
| 23 | normal_generator():
|
---|
| 24 | engine(time(NULL)),
|
---|
| 25 | generator(engine,boost::normal_distribution<RealType>(0,1))
|
---|
| 26 | {}
|
---|
| 27 | /*!
|
---|
| 28 | * Construtor
|
---|
| 29 | * \param dist : a normal distribution (see boost distributions)
|
---|
| 30 | */
|
---|
| 31 | normal_generator(const boost::math::normal_distribution<RealType> & dist):
|
---|
| 32 | engine(time(NULL)),
|
---|
| 33 | generator(engine,boost::normal_distribution<RealType>(dist.mean(),dist.sigma()))
|
---|
| 34 | {}
|
---|
| 35 | /*!
|
---|
| 36 | * Construtor
|
---|
| 37 | * \param mean : mean of the normal distribution
|
---|
| 38 | * \param sigma : satndard deviation of the normal distribution
|
---|
| 39 | */
|
---|
| 40 | normal_generator(const RealType & mean ,const RealType & sigma):
|
---|
| 41 | engine(time(NULL)),
|
---|
| 42 | generator(engine,boost::normal_distribution<RealType>(mean(),sigma()))
|
---|
| 43 | {}
|
---|
| 44 |
|
---|
| 45 | /*!
|
---|
| 46 | * Get a random number
|
---|
| 47 | */
|
---|
| 48 | RealType operator()() {return generator();}
|
---|
| 49 |
|
---|
| 50 | /*!
|
---|
| 51 | * Set normal distribution
|
---|
| 52 | * \param dist : a normal distribution (see boost distributions)
|
---|
| 53 | */
|
---|
| 54 | void distribution( const boost::math::normal_distribution<RealType> & dist) {
|
---|
| 55 | generator.dist= boost::normal_distribution<RealType>(dist.mean(),dist.standard_deviation());
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /*!
|
---|
| 59 | * Set normal distrubution
|
---|
| 60 | * \param mean : mean of the normal distribution
|
---|
| 61 | * \param sigma : satndard deviation of the normal distribution
|
---|
| 62 | */
|
---|
| 63 | void distribution( const RealType & mean ,const RealType & sigma) {
|
---|
| 64 | generator.distribution()= boost::normal_distribution<RealType>(mean,sigma);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | private :
|
---|
| 68 |
|
---|
| 69 | /*!
|
---|
| 70 | * \brief pseudo random number generator
|
---|
| 71 | */
|
---|
| 72 | PRNG engine;
|
---|
| 73 |
|
---|
| 74 | /*!
|
---|
| 75 | * \brief the normal random number generator
|
---|
| 76 | */
|
---|
| 77 | boost::variate_generator<PRNG, boost::normal_distribution<RealType> > generator;
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | typedef class normal_generator<double,boost::mt19937> normal_rng;
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | /*!
|
---|
| 85 | * \class uniform_generator
|
---|
| 86 | * \brief This class is basic uniform random number generator \n
|
---|
| 87 | * template id RealType describe number precision (float double ....)\n
|
---|
| 88 | * template id PRNG describe pseudo random number generator ( see boost random)
|
---|
| 89 | */
|
---|
| 90 | template < class RealType , class PRNG > class uniform_generator {
|
---|
| 91 |
|
---|
| 92 | public :
|
---|
| 93 | /*!
|
---|
| 94 | * Default construtor
|
---|
| 95 | */
|
---|
| 96 | uniform_generator():
|
---|
| 97 | engine(time(NULL)),
|
---|
| 98 | generator(engine, boost::uniform_real<RealType>(0,1))
|
---|
| 99 | {}
|
---|
| 100 | /*!
|
---|
| 101 | * Construtor
|
---|
| 102 | * \param dist : a uniform distribution (see boost distributions)
|
---|
| 103 | */
|
---|
| 104 | uniform_generator(const boost::math::uniform_distribution<RealType> & dist):
|
---|
| 105 | engine(time(NULL)),
|
---|
| 106 | generator(engine, boost::uniform_real<RealType>(dist.lower(),dist.upper()))
|
---|
| 107 | {}
|
---|
| 108 | /*!
|
---|
| 109 | * Construtor
|
---|
| 110 | * \param lower : the lower value of the uniform distribution
|
---|
| 111 | * \param upper : the upper value of the uniform distribution
|
---|
| 112 | */
|
---|
| 113 | uniform_generator(const RealType & lower , const RealType & upper):
|
---|
| 114 | engine(time(NULL)),
|
---|
| 115 | generator(engine, boost::uniform_real<RealType>(lower,upper))
|
---|
| 116 | {}
|
---|
| 117 |
|
---|
| 118 | /*!
|
---|
| 119 | * Get a random number
|
---|
| 120 | */
|
---|
| 121 | RealType operator()() {return generator();}
|
---|
| 122 | /*!
|
---|
| 123 | * Set uniform distribution
|
---|
| 124 | * \param dist : a normal distribution (see boost distributions)
|
---|
| 125 | */
|
---|
| 126 | void distribution( const boost::math::uniform_distribution<RealType> & dist) {
|
---|
| 127 | generator.dist()= boost::uniform_real<RealType>(dist.lower(),dist.upper());
|
---|
| 128 | }
|
---|
| 129 | /*!
|
---|
| 130 | * Set uniform distribution
|
---|
| 131 | * \param lower : the lower value of the uniform distribution
|
---|
| 132 | * \param upper : the upper value of the uniform distribution
|
---|
| 133 | */
|
---|
| 134 | void distribution( const RealType & lower , const RealType & upper) {
|
---|
| 135 | generator.dist()= boost::uniform_real<RealType>(lower,upper);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private :
|
---|
| 139 | /*!
|
---|
| 140 | * \brief pseudo random number generator
|
---|
| 141 | */
|
---|
| 142 | PRNG engine;
|
---|
| 143 | /*!
|
---|
| 144 | * \brief the uniform random number generator
|
---|
| 145 | */
|
---|
| 146 | boost::variate_generator<PRNG, boost::uniform_real<RealType> > generator;
|
---|
| 147 | };
|
---|
| 148 |
|
---|
| 149 | typedef class uniform_generator<double,boost::mt19937> uniform_rng;
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | /*!
|
---|
| 153 | * \class traingle_generator
|
---|
| 154 | * \brief This class is basic triangular random number generator \n
|
---|
| 155 | * template id RealType describe number precision (float double ....)\n
|
---|
| 156 | * template id PRNG describe pseudo random number generator ( see boost random)
|
---|
| 157 | */
|
---|
| 158 | template < class RealType , class PRNG > class triangle_generator {
|
---|
| 159 |
|
---|
| 160 | public :
|
---|
| 161 | /*!
|
---|
| 162 | * Default construtor
|
---|
| 163 | */
|
---|
| 164 | triangle_generator():
|
---|
| 165 | engine(time(NULL)),
|
---|
| 166 | generator(engine, boost::triangle_distribution<RealType>(-1,0,1))
|
---|
| 167 | {}
|
---|
| 168 | /*!
|
---|
| 169 | * Construtor
|
---|
| 170 | * \param dist : a traingle distribution (see boost distributions)
|
---|
| 171 | */
|
---|
| 172 | triangle_generator(const boost::math::triangular_distribution<RealType> & dist):
|
---|
| 173 | engine(time(NULL)),
|
---|
| 174 | generator(engine, boost::triangle_distribution<RealType>(dist.lower(),dist.upper()))
|
---|
| 175 | {}
|
---|
| 176 | /*!
|
---|
| 177 | * Constructor :
|
---|
| 178 | * \param lower : the lower value of the uniform distribution
|
---|
| 179 | * \param mode : the mode value of the uniform distribution
|
---|
| 180 | * \param upper : the upper value of the uniform distribution
|
---|
| 181 | */
|
---|
| 182 | triangle_generator(const RealType & lower , const RealType & mode ,const RealType & upper):
|
---|
| 183 | engine(time(NULL)),
|
---|
| 184 | generator(engine, boost::triangle_distribution<RealType>(lower,mode,upper))
|
---|
| 185 | {}
|
---|
| 186 | /*!
|
---|
| 187 | * Get a random number
|
---|
| 188 | */
|
---|
| 189 | RealType operator()() {return generator();}
|
---|
| 190 | /*!
|
---|
| 191 | * Set triangle distribution
|
---|
| 192 | * \param dist : a normal distribution (see boost distributions)
|
---|
| 193 | */
|
---|
| 194 | void distribution( const boost::math::triangular_distribution<RealType> & dist) {
|
---|
| 195 | generator.dist()= boost::triangle_distribution<RealType>(dist.lower(),dist.mode(),dist.upper());
|
---|
| 196 | }
|
---|
| 197 | /*!
|
---|
| 198 | * Set triangle distribution
|
---|
| 199 | * \param lower : the lower value of the uniform distribution
|
---|
| 200 | * \param mode : the mode value of the uniform distribution
|
---|
| 201 | * \param upper : the upper value of the uniform distribution
|
---|
| 202 | */
|
---|
| 203 | void distribution( const RealType & lower ,const RealType & mode ,const RealType & upper) {
|
---|
| 204 | generator.dist()= boost::triangle_distribution<RealType>(lower,mode,upper);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | private :
|
---|
| 208 | /*!
|
---|
| 209 | * \brief pseudo random number generator
|
---|
| 210 | */
|
---|
| 211 | PRNG engine;
|
---|
| 212 | /*!
|
---|
| 213 | * \brief the triangle random number generator
|
---|
| 214 | */
|
---|
| 215 | boost::variate_generator<PRNG, boost::triangle_distribution<RealType> > generator;
|
---|
| 216 | };
|
---|
| 217 |
|
---|
| 218 | typedef class triangle_generator<double,boost::mt19937> triangle_rng;
|
---|
| 219 |
|
---|
| 220 | };
|
---|
| 221 | };
|
---|
| 222 | #endif
|
---|