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