mlpack  2.0.1
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 <random>
18 
19 namespace mlpack {
20 namespace math {
21 
22 // Global random object.
23 extern std::mt19937 randGen;
24 // Global uniform distribution.
25 extern std::uniform_real_distribution<> randUniformDist;
26 // Global normal distribution.
27 extern std::normal_distribution<> randNormalDist;
28 
36 inline void RandomSeed(const size_t seed)
37 {
38  randGen.seed((uint32_t) seed);
39  srand((unsigned int) seed);
40 #if ARMA_VERSION_MAJOR > 3 || \
41  (ARMA_VERSION_MAJOR == 3 && ARMA_VERSION_MINOR >= 930)
42  // Armadillo >= 3.930 has its own random number generator internally that we
43  // need to set the seed for also.
44  arma::arma_rng::set_seed(seed);
45 #endif
46 }
47 
51 inline double Random()
52 {
53  return randUniformDist(randGen);
54 }
55 
59 inline double Random(const double lo, const double hi)
60 {
61  return lo + (hi - lo) * randUniformDist(randGen);
62 }
63 
67 inline int RandInt(const int hiExclusive)
68 {
69  return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
70 }
71 
75 inline int RandInt(const int lo, const int hiExclusive)
76 {
77  return lo + (int) std::floor((double) (hiExclusive - lo)
78  * randUniformDist(randGen));
79 }
80 
84 inline double RandNormal()
85 {
86  return randNormalDist(randGen);
87 }
88 
96 inline double RandNormal(const double mean, const double variance)
97 {
98  return variance * randNormalDist(randGen) + mean;
99 }
100 
101 } // namespace math
102 } // namespace mlpack
103 
104 #endif // __MLPACK_CORE_MATH_MATH_LIB_HPP
std::uniform_real_distribution randUniformDist
Linear algebra utility functions, generally performed on matrices or vectors.
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:36
double RandNormal()
Generates a normally distributed random number with mean 0 and variance 1.
Definition: random.hpp:84
std::mt19937 randGen
double Random()
Generates a uniform random number between 0 and 1.
Definition: random.hpp:51
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:67
std::normal_distribution randNormalDist