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  #if (BINDING_TYPE != BINDING_TYPE_TEST)
43  randGen.seed((uint32_t) seed);
44  srand((unsigned int) seed);
45  arma::arma_rng::set_seed(seed);
46  #else
47  (void) seed;
48  #endif
49 }
50 
58 #if (BINDING_TYPE == BINDING_TYPE_TEST)
59 inline void FixedRandomSeed()
60 {
61  const static size_t seed = rand();
62  randGen.seed((uint32_t) seed);
63  srand((unsigned int) seed);
64  arma::arma_rng::set_seed(seed);
65 }
66 #endif
67 
71 inline double Random()
72 {
73  return randUniformDist(randGen);
74 }
75 
79 inline double Random(const double lo, const double hi)
80 {
81  return lo + (hi - lo) * randUniformDist(randGen);
82 }
83 
87 inline int RandInt(const int hiExclusive)
88 {
89  return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
90 }
91 
95 inline int RandInt(const int lo, const int hiExclusive)
96 {
97  return lo + (int) std::floor((double) (hiExclusive - lo)
98  * randUniformDist(randGen));
99 }
100 
104 inline double RandNormal()
105 {
106  return randNormalDist(randGen);
107 }
108 
116 inline double RandNormal(const double mean, const double variance)
117 {
118  return variance * randNormalDist(randGen) + mean;
119 }
120 
130 inline void ObtainDistinctSamples(const size_t loInclusive,
131  const size_t hiExclusive,
132  const size_t maxNumSamples,
133  arma::uvec& distinctSamples)
134 {
135  const size_t samplesRangeSize = hiExclusive - loInclusive;
136 
137  if (samplesRangeSize > maxNumSamples)
138  {
139  arma::Col<size_t> samples;
140 
141  samples.zeros(samplesRangeSize);
142 
143  for (size_t i = 0; i < maxNumSamples; i++)
144  samples [ (size_t) math::RandInt(samplesRangeSize) ]++;
145 
146  distinctSamples = arma::find(samples > 0);
147 
148  if (loInclusive > 0)
149  distinctSamples += loInclusive;
150  }
151  else
152  {
153  distinctSamples.set_size(samplesRangeSize);
154  for (size_t i = 0; i < samplesRangeSize; i++)
155  distinctSamples[i] = loInclusive + i;
156  }
157 }
158 
159 } // namespace math
160 } // namespace mlpack
161 
162 #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:130
.hpp
Definition: add_to_po.hpp:21
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
MLPACK_EXPORT std::uniform_real_distribution randUniformDist
double RandNormal()
Generates a normally distributed random number with mean 0 and variance 1.
Definition: random.hpp:104
MLPACK_EXPORT std::normal_distribution randNormalDist
void FixedRandomSeed()
Set the random seed to a fixed number.
Definition: random.hpp:59
MLPACK_EXPORT std::mt19937 randGen
MLPACK_EXPORT is required for global variables; it exports the symbols correctly on Windows...
double Random()
Generates a uniform random number between 0 and 1.
Definition: random.hpp:71
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:87