MLPACK  1.0.11
random.hpp
Go to the documentation of this file.
1 
21 #ifndef __MLPACK_CORE_MATH_RANDOM_HPP
22 #define __MLPACK_CORE_MATH_RANDOM_HPP
23 
24 #include <mlpack/prereqs.hpp>
25 #include <boost/random.hpp>
26 
27 namespace mlpack {
28 namespace math {
29 
30 // Annoying Boost versioning issues.
31 #include <boost/version.hpp>
32 
33 #if BOOST_VERSION >= 104700
34  // Global random object.
35  extern boost::random::mt19937 randGen;
36  // Global uniform distribution.
37  extern boost::random::uniform_01<> randUniformDist;
38  // Global normal distribution.
39  extern boost::random::normal_distribution<> randNormalDist;
40 #else
41  // Global random object.
42  extern boost::mt19937 randGen;
43 
44  #if BOOST_VERSION >= 103900
45  // Global uniform distribution.
46  extern boost::uniform_01<> randUniformDist;
47  #else
48  // Pre-1.39 Boost.Random did not give default template parameter values.
49  extern boost::uniform_01<boost::mt19937, double> randUniformDist;
50  #endif
51 
52  // Global normal distribution.
53  extern boost::normal_distribution<> randNormalDist;
54 #endif
55 
63 inline void RandomSeed(const size_t seed)
64 {
65  randGen.seed((uint32_t) seed);
66  srand((unsigned int) seed);
67 #if ARMA_VERSION_MAJOR > 3 || \
68  (ARMA_VERSION_MAJOR == 3 && ARMA_VERSION_MINOR >= 930)
69  // Armadillo >= 3.930 has its own random number generator internally that we
70  // need to set the seed for also.
71  arma::arma_rng::set_seed(seed);
72 #endif
73 }
74 
78 inline double Random()
79 {
80 #if BOOST_VERSION >= 103900
81  return randUniformDist(randGen);
82 #else
83  // Before Boost 1.39, we did not give the random object when we wanted a
84  // random number; that gets given at construction time.
85  return randUniformDist();
86 #endif
87 }
88 
92 inline double Random(const double lo, const double hi)
93 {
94 #if BOOST_VERSION >= 103900
95  return lo + (hi - lo) * randUniformDist(randGen);
96 #else
97  // Before Boost 1.39, we did not give the random object when we wanted a
98  // random number; that gets given at construction time.
99  return lo + (hi - lo) * randUniformDist();
100 #endif
101 }
102 
106 inline int RandInt(const int hiExclusive)
107 {
108 #if BOOST_VERSION >= 103900
109  return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
110 #else
111  // Before Boost 1.39, we did not give the random object when we wanted a
112  // random number; that gets given at construction time.
113  return (int) std::floor((double) hiExclusive * randUniformDist());
114 #endif
115 }
116 
120 inline int RandInt(const int lo, const int hiExclusive)
121 {
122 #if BOOST_VERSION >= 103900
123  return lo + (int) std::floor((double) (hiExclusive - lo)
125 #else
126  // Before Boost 1.39, we did not give the random object when we wanted a
127  // random number; that gets given at construction time.
128  return lo + (int) std::floor((double) (hiExclusive - lo)
129  * randUniformDist());
130 #endif
131 
132 }
133 
137 inline double RandNormal()
138 {
139  return randNormalDist(randGen);
140 }
141 
149 inline double RandNormal(const double mean, const double variance)
150 {
151  return variance * randNormalDist(randGen) + mean;
152 }
153 
154 }; // namespace math
155 }; // namespace mlpack
156 
157 #endif // __MLPACK_CORE_MATH_MATH_LIB_HPP