mlpack  master
random.hpp
Go to the documentation of this file.
1 
11 #ifndef MLPACK_CORE_MATH_RANDOM_HPP
12 #define MLPACK_CORE_MATH_RANDOM_HPP
13 
14 #include <mlpack/prereqs.hpp>
15 #include <mlpack/mlpack_export.hpp>
16 #include <random>
17 
18 namespace mlpack {
19 namespace math {
20 
26 // Global random object.
27 extern MLPACK_EXPORT std::mt19937 randGen;
28 // Global uniform distribution.
29 extern MLPACK_EXPORT std::uniform_real_distribution<> randUniformDist;
30 // Global normal distribution.
31 extern MLPACK_EXPORT std::normal_distribution<> randNormalDist;
32 
40 inline void RandomSeed(const size_t seed)
41 {
42  randGen.seed((uint32_t) seed);
43  srand((unsigned int) seed);
44 #if ARMA_VERSION_MAJOR > 3 || \
45  (ARMA_VERSION_MAJOR == 3 && ARMA_VERSION_MINOR >= 930)
46  // Armadillo >= 3.930 has its own random number generator internally that we
47  // need to set the seed for also.
48  arma::arma_rng::set_seed(seed);
49 #endif
50 }
51 
55 inline double Random()
56 {
57  return randUniformDist(randGen);
58 }
59 
63 inline double Random(const double lo, const double hi)
64 {
65  return lo + (hi - lo) * randUniformDist(randGen);
66 }
67 
71 inline int RandInt(const int hiExclusive)
72 {
73  return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
74 }
75 
79 inline int RandInt(const int lo, const int hiExclusive)
80 {
81  return lo + (int) std::floor((double) (hiExclusive - lo)
82  * randUniformDist(randGen));
83 }
84 
88 inline double RandNormal()
89 {
90  return randNormalDist(randGen);
91 }
92 
100 inline double RandNormal(const double mean, const double variance)
101 {
102  return variance * randNormalDist(randGen) + mean;
103 }
104 
114 inline void ObtainDistinctSamples(const size_t loInclusive,
115  const size_t hiExclusive,
116  const size_t maxNumSamples,
117  arma::uvec& distinctSamples)
118 {
119  const size_t samplesRangeSize = hiExclusive - loInclusive;
120 
121  if (samplesRangeSize > maxNumSamples)
122  {
123  arma::Col<size_t> samples;
124 
125  samples.zeros(samplesRangeSize);
126 
127  for (size_t i = 0; i < maxNumSamples; i++)
128  samples [ (size_t) math::RandInt(samplesRangeSize) ]++;
129 
130  distinctSamples = arma::find(samples > 0);
131 
132  if (loInclusive > 0)
133  distinctSamples += loInclusive;
134  }
135  else
136  {
137  distinctSamples.set_size(samplesRangeSize);
138  for (size_t i = 0; i < samplesRangeSize; i++)
139  distinctSamples[i] = loInclusive + i;
140  }
141 }
142 
143 } // namespace math
144 } // namespace mlpack
145 
146 #endif // MLPACK_CORE_MATH_MATH_LIB_HPP
void ObtainDistinctSamples(const size_t loInclusive, const size_t hiExclusive, const size_t maxNumSamples, arma::uvec &distinctSamples)
Obtains no more than maxNumSamples distinct samples.
Definition: random.hpp:114
MLPACK_EXPORT std::uniform_real_distribution randUniformDist
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
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:40
double RandNormal()
Generates a normally distributed random number with mean 0 and variance 1.
Definition: random.hpp:88
double Random()
Generates a uniform random number between 0 and 1.
Definition: random.hpp:55
MLPACK_EXPORT std::normal_distribution randNormalDist
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:71
MLPACK_EXPORT std::mt19937 randGen
MLPACK_EXPORT is required for global variables; it exports the symbols correctly on Windows...