mlpack  1.0.12
random.hpp
Go to the documentation of this file.
1 
13 #ifndef __MLPACK_CORE_MATH_RANDOM_HPP
14 #define __MLPACK_CORE_MATH_RANDOM_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 #include <boost/random.hpp>
18 
19 namespace mlpack {
20 namespace math {
21 
22 // Annoying Boost versioning issues.
23 #include <boost/version.hpp>
24 
25 #if BOOST_VERSION >= 104700
26  // Global random object.
27  extern boost::random::mt19937 randGen;
28  // Global uniform distribution.
29  extern boost::random::uniform_01<> randUniformDist;
30  // Global normal distribution.
31  extern boost::random::normal_distribution<> randNormalDist;
32 #else
33  // Global random object.
34  extern boost::mt19937 randGen;
35 
36  #if BOOST_VERSION >= 103900
37  // Global uniform distribution.
38  extern boost::uniform_01<> randUniformDist;
39  #else
40  // Pre-1.39 Boost.Random did not give default template parameter values.
41  extern boost::uniform_01<boost::mt19937, double> randUniformDist;
42  #endif
43 
44  // Global normal distribution.
45  extern boost::normal_distribution<> randNormalDist;
46 #endif
47 
55 inline void RandomSeed(const size_t seed)
56 {
57  randGen.seed((uint32_t) seed);
58  srand((unsigned int) seed);
59 #if ARMA_VERSION_MAJOR > 3 || \
60  (ARMA_VERSION_MAJOR == 3 && ARMA_VERSION_MINOR >= 930)
61  // Armadillo >= 3.930 has its own random number generator internally that we
62  // need to set the seed for also.
63  arma::arma_rng::set_seed(seed);
64 #endif
65 }
66 
70 inline double Random()
71 {
72 #if BOOST_VERSION >= 103900
73  return randUniformDist(randGen);
74 #else
75  // Before Boost 1.39, we did not give the random object when we wanted a
76  // random number; that gets given at construction time.
77  return randUniformDist();
78 #endif
79 }
80 
84 inline double Random(const double lo, const double hi)
85 {
86 #if BOOST_VERSION >= 103900
87  return lo + (hi - lo) * randUniformDist(randGen);
88 #else
89  // Before Boost 1.39, we did not give the random object when we wanted a
90  // random number; that gets given at construction time.
91  return lo + (hi - lo) * randUniformDist();
92 #endif
93 }
94 
98 inline int RandInt(const int hiExclusive)
99 {
100 #if BOOST_VERSION >= 103900
101  return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
102 #else
103  // Before Boost 1.39, we did not give the random object when we wanted a
104  // random number; that gets given at construction time.
105  return (int) std::floor((double) hiExclusive * randUniformDist());
106 #endif
107 }
108 
112 inline int RandInt(const int lo, const int hiExclusive)
113 {
114 #if BOOST_VERSION >= 103900
115  return lo + (int) std::floor((double) (hiExclusive - lo)
116  * randUniformDist(randGen));
117 #else
118  // Before Boost 1.39, we did not give the random object when we wanted a
119  // random number; that gets given at construction time.
120  return lo + (int) std::floor((double) (hiExclusive - lo)
121  * randUniformDist());
122 #endif
123 
124 }
125 
129 inline double RandNormal()
130 {
131  return randNormalDist(randGen);
132 }
133 
141 inline double RandNormal(const double mean, const double variance)
142 {
143  return variance * randNormalDist(randGen) + mean;
144 }
145 
146 }; // namespace math
147 }; // namespace mlpack
148 
149 #endif // __MLPACK_CORE_MATH_MATH_LIB_HPP
boost::uniform_01< boost::mt19937, double > randUniformDist
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
The core includes that mlpack expects; standard C++ includes and Armadillo.
void RandomSeed(const size_t seed)
Set the random seed used by the random functions (Random() and RandInt()).
Definition: random.hpp:55
double RandNormal()
Generates a normally distributed random number with mean 0 and variance 1.
Definition: random.hpp:129
double Random()
Generates a uniform random number between 0 and 1.
Definition: random.hpp:70
boost::normal_distribution randNormalDist
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:98
boost::mt19937 randGen