MLPACK  1.0.10
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 }
68 
72 inline double Random()
73 {
74 #if BOOST_VERSION >= 103900
75  return randUniformDist(randGen);
76 #else
77  // Before Boost 1.39, we did not give the random object when we wanted a
78  // random number; that gets given at construction time.
79  return randUniformDist();
80 #endif
81 }
82 
86 inline double Random(const double lo, const double hi)
87 {
88 #if BOOST_VERSION >= 103900
89  return lo + (hi - lo) * randUniformDist(randGen);
90 #else
91  // Before Boost 1.39, we did not give the random object when we wanted a
92  // random number; that gets given at construction time.
93  return lo + (hi - lo) * randUniformDist();
94 #endif
95 }
96 
100 inline int RandInt(const int hiExclusive)
101 {
102 #if BOOST_VERSION >= 103900
103  return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
104 #else
105  // Before Boost 1.39, we did not give the random object when we wanted a
106  // random number; that gets given at construction time.
107  return (int) std::floor((double) hiExclusive * randUniformDist());
108 #endif
109 }
110 
114 inline int RandInt(const int lo, const int hiExclusive)
115 {
116 #if BOOST_VERSION >= 103900
117  return lo + (int) std::floor((double) (hiExclusive - lo)
119 #else
120  // Before Boost 1.39, we did not give the random object when we wanted a
121  // random number; that gets given at construction time.
122  return lo + (int) std::floor((double) (hiExclusive - lo)
123  * randUniformDist());
124 #endif
125 
126 }
127 
131 inline double RandNormal()
132 {
133  return randNormalDist(randGen);
134 }
135 
143 inline double RandNormal(const double mean, const double variance)
144 {
145  return variance * randNormalDist(randGen) + mean;
146 }
147 
148 }; // namespace math
149 }; // namespace mlpack
150 
151 #endif // __MLPACK_CORE_MATH_MATH_LIB_HPP
boost::uniform_01< boost::mt19937, double > randUniformDist
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:63
double RandNormal()
Generates a normally distributed random number with mean 0 and variance 1.
Definition: random.hpp:131
double Random()
Generates a uniform random number between 0 and 1.
Definition: random.hpp:72
boost::normal_distribution randNormalDist
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:100
boost::mt19937 randGen