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 (!defined(BINDING_TYPE) || 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 
67 inline void CustomRandomSeed(const size_t seed)
68 {
69  randGen.seed((uint32_t) seed);
70  srand((unsigned int) seed);
71  arma::arma_rng::set_seed(seed);
72 }
73 #endif
74 
78 inline double Random()
79 {
80  return randUniformDist(randGen);
81 }
82 
86 inline double Random(const double lo, const double hi)
87 {
88  return lo + (hi - lo) * randUniformDist(randGen);
89 }
90 
94 inline double RandBernoulli(const double input)
95 {
96  if (Random() < input)
97  return 1;
98  else
99  return 0;
100 }
101 
105 inline int RandInt(const int hiExclusive)
106 {
107  return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
108 }
109 
113 inline int RandInt(const int lo, const int hiExclusive)
114 {
115  return lo + (int) std::floor((double) (hiExclusive - lo)
116  * randUniformDist(randGen));
117 }
118 
122 inline double RandNormal()
123 {
124  return randNormalDist(randGen);
125 }
126 
134 inline double RandNormal(const double mean, const double variance)
135 {
136  return variance * randNormalDist(randGen) + mean;
137 }
138 
148 inline void ObtainDistinctSamples(const size_t loInclusive,
149  const size_t hiExclusive,
150  const size_t maxNumSamples,
151  arma::uvec& distinctSamples)
152 {
153  const size_t samplesRangeSize = hiExclusive - loInclusive;
154 
155  if (samplesRangeSize > maxNumSamples)
156  {
157  arma::Col<size_t> samples;
158 
159  samples.zeros(samplesRangeSize);
160 
161  for (size_t i = 0; i < maxNumSamples; i++)
162  samples [ (size_t) math::RandInt(samplesRangeSize) ]++;
163 
164  distinctSamples = arma::find(samples > 0);
165 
166  if (loInclusive > 0)
167  distinctSamples += loInclusive;
168  }
169  else
170  {
171  distinctSamples.set_size(samplesRangeSize);
172  for (size_t i = 0; i < samplesRangeSize; i++)
173  distinctSamples[i] = loInclusive + i;
174  }
175 }
176 
177 } // namespace math
178 } // namespace mlpack
179 
180 #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:148
double RandBernoulli(const double input)
Generates a 0/1 specified by the input.
Definition: random.hpp:94
.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:122
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:78
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:105
void CustomRandomSeed(const size_t seed)
Definition: random.hpp:67