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 #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 double RandBernoulli(const double input)
88 {
89  if (Random() < input)
90  return 1;
91  else
92  return 0;
93 }
94 
98 inline int RandInt(const int hiExclusive)
99 {
100  return (int) std::floor((double) hiExclusive * randUniformDist(randGen));
101 }
102 
106 inline int RandInt(const int lo, const int hiExclusive)
107 {
108  return lo + (int) std::floor((double) (hiExclusive - lo)
109  * randUniformDist(randGen));
110 }
111 
115 inline double RandNormal()
116 {
117  return randNormalDist(randGen);
118 }
119 
127 inline double RandNormal(const double mean, const double variance)
128 {
129  return variance * randNormalDist(randGen) + mean;
130 }
131 
141 inline void ObtainDistinctSamples(const size_t loInclusive,
142  const size_t hiExclusive,
143  const size_t maxNumSamples,
144  arma::uvec& distinctSamples)
145 {
146  const size_t samplesRangeSize = hiExclusive - loInclusive;
147 
148  if (samplesRangeSize > maxNumSamples)
149  {
150  arma::Col<size_t> samples;
151 
152  samples.zeros(samplesRangeSize);
153 
154  for (size_t i = 0; i < maxNumSamples; i++)
155  samples [ (size_t) math::RandInt(samplesRangeSize) ]++;
156 
157  distinctSamples = arma::find(samples > 0);
158 
159  if (loInclusive > 0)
160  distinctSamples += loInclusive;
161  }
162  else
163  {
164  distinctSamples.set_size(samplesRangeSize);
165  for (size_t i = 0; i < samplesRangeSize; i++)
166  distinctSamples[i] = loInclusive + i;
167  }
168 }
169 
170 } // namespace math
171 } // namespace mlpack
172 
173 #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:141
double RandBernoulli(const double input)
Generates a 0/1 specified by the input.
Definition: random.hpp:87
.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:115
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:98