MLPACK  1.0.7
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 <stdlib.h>
25 #include <math.h>
26 #include <float.h>
27 
28 #include <boost/random.hpp>
29 
30 namespace mlpack {
31 namespace math {
32 
33 // Annoying Boost versioning issues.
34 #include <boost/version.hpp>
35 
36 #if BOOST_VERSION >= 104700
37  // Global random object.
38  extern boost::random::mt19937 randGen;
39  // Global uniform distribution.
40  extern boost::random::uniform_01<> randUniformDist;
41  // Global normal distribution.
42  extern boost::random::normal_distribution<> randNormalDist;
43 #else
44  // Global random object.
45  extern boost::mt19937 randGen;
46 
47  #if BOOST_VERSION >= 103900
48  // Global uniform distribution.
49  extern boost::uniform_01<> randUniformDist;
50  #else
51  // Pre-1.39 Boost.Random did not give default template parameter values.
52  extern boost::uniform_01<boost::mt19937, double> randUniformDist;
53  #endif
54 
55  // Global normal distribution.
56  extern boost::normal_distribution<> randNormalDist;
57 #endif
58 
66 inline void RandomSeed(const size_t seed)
67 {
68  randGen.seed((uint32_t) seed);
69  srand((unsigned int) seed);
70 }
71 
75 inline double Random()
76 {
77 #if BOOST_VERSION >= 103900
78  return randUniformDist(randGen);
79 #else
80  // Before Boost 1.39, we did not give the random object when we wanted a
81  // random number; that gets given at construction time.
82  return randUniformDist();
83 #endif
84 }
85 
89 inline double Random(const double lo, const double hi)
90 {
91 #if BOOST_VERSION >= 103900
92  return lo + (hi - lo) * randUniformDist(randGen);
93 #else
94  // Before Boost 1.39, we did not give the random object when we wanted a
95  // random number; that gets given at construction time.
96  return lo + (hi - lo) * randUniformDist();
97 #endif
98 }
99 
103 inline int RandInt(const int hiExclusive)
104 {
105 #if BOOST_VERSION >= 103900
106  return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
107 #else
108  // Before Boost 1.39, we did not give the random object when we wanted a
109  // random number; that gets given at construction time.
110  return (int) std::floor((double) hiExclusive * randUniformDist());
111 #endif
112 }
113 
117 inline int RandInt(const int lo, const int hiExclusive)
118 {
119 #if BOOST_VERSION >= 103900
120  return lo + (int) std::floor((double) (hiExclusive - lo)
122 #else
123  // Before Boost 1.39, we did not give the random object when we wanted a
124  // random number; that gets given at construction time.
125  return lo + (int) std::floor((double) (hiExclusive - lo)
126  * randUniformDist());
127 #endif
128 
129 }
130 
134 inline double RandNormal()
135 {
136  return randNormalDist(randGen);
137 }
138 
146 inline double RandNormal(const double mean, const double variance)
147 {
148  return variance * randNormalDist(randGen) + mean;
149 }
150 
151 }; // namespace math
152 }; // namespace mlpack
153 
154 #endif // __MLPACK_CORE_MATH_MATH_LIB_HPP