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 inline std::mt19937& RandGen()
28 {
29  static thread_local std::mt19937 randGen;
30  return randGen;
31 }
32 
34 inline std::uniform_real_distribution<>& RandUniformDist()
35 {
36  static thread_local std::uniform_real_distribution<> randUniformDist(0.0, 1.0);
37  return randUniformDist;
38 }
39 
41 inline std::normal_distribution<>& RandNormalDist()
42 {
43  static thread_local std::normal_distribution<> randNormalDist(0.0, 1.0);
44  return randNormalDist;
45 }
46 
54 inline void RandomSeed(const size_t seed)
55 {
56  #if (!defined(BINDING_TYPE) || BINDING_TYPE != BINDING_TYPE_TEST)
57  RandGen().seed((uint32_t) seed);
58  #if (BINDING_TYPE == BINDING_TYPE_R)
59  // To suppress Found 'srand', possibly from 'srand' (C).
60  (void) seed;
61  #else
62  srand((unsigned int) seed);
63  #endif
64  arma::arma_rng::set_seed(seed);
65  #else
66  (void) seed;
67  #endif
68 }
69 
77 #if (BINDING_TYPE == BINDING_TYPE_TEST)
78 inline void FixedRandomSeed()
79 {
80  const static size_t seed = rand();
81  RandGen().seed((uint32_t) seed);
82  srand((unsigned int) seed);
83  arma::arma_rng::set_seed(seed);
84 }
85 
86 inline void CustomRandomSeed(const size_t seed)
87 {
88  RandGen().seed((uint32_t) seed);
89  srand((unsigned int) seed);
90  arma::arma_rng::set_seed(seed);
91 }
92 #endif
93 
97 inline double Random()
98 {
99  return RandUniformDist()(RandGen());
100 }
101 
105 inline double Random(const double lo, const double hi)
106 {
107  return lo + (hi - lo) * RandUniformDist()(RandGen());
108 }
109 
113 inline double RandBernoulli(const double input)
114 {
115  if (Random() < input)
116  return 1;
117  else
118  return 0;
119 }
120 
124 inline int RandInt(const int hiExclusive)
125 {
126  return (int) std::floor((double) hiExclusive * RandUniformDist()(RandGen()));
127 }
128 
132 inline int RandInt(const int lo, const int hiExclusive)
133 {
134  return lo + (int) std::floor((double) (hiExclusive - lo)
135  * RandUniformDist()(RandGen()));
136 }
137 
141 inline double RandNormal()
142 {
143  return RandNormalDist()(RandGen());
144 }
145 
153 inline double RandNormal(const double mean, const double variance)
154 {
155  return variance * RandNormalDist()(RandGen()) + mean;
156 }
157 
167 inline void ObtainDistinctSamples(const size_t loInclusive,
168  const size_t hiExclusive,
169  const size_t maxNumSamples,
170  arma::uvec& distinctSamples)
171 {
172  const size_t samplesRangeSize = hiExclusive - loInclusive;
173 
174  if (samplesRangeSize > maxNumSamples)
175  {
176  arma::Col<size_t> samples;
177 
178  samples.zeros(samplesRangeSize);
179 
180  for (size_t i = 0; i < maxNumSamples; ++i)
181  samples [ (size_t) math::RandInt(samplesRangeSize) ]++;
182 
183  distinctSamples = arma::find(samples > 0);
184 
185  if (loInclusive > 0)
186  distinctSamples += loInclusive;
187  }
188  else
189  {
190  distinctSamples.set_size(samplesRangeSize);
191  for (size_t i = 0; i < samplesRangeSize; ++i)
192  distinctSamples[i] = loInclusive + i;
193  }
194 }
195 
196 } // namespace math
197 } // namespace mlpack
198 
199 #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:167
double RandBernoulli(const double input)
Generates a 0/1 specified by the input.
Definition: random.hpp:113
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
typename impl::find< T, U >::type find
Definition: meta.hpp:238
void RandomSeed(const size_t seed)
Set the random seed used by the random functions (Random() and RandInt()).
Definition: random.hpp:54
double RandNormal()
Generates a normally distributed random number with mean 0 and variance 1.
Definition: random.hpp:141
std::mt19937 & RandGen()
MLPACK_EXPORT is required for global variables; it exports the symbols correctly on Windows...
Definition: random.hpp:27
std::uniform_real_distribution & RandUniformDist()
Global uniform distribution.
Definition: random.hpp:34
void FixedRandomSeed()
Set the random seed to a fixed number.
Definition: random.hpp:78
double Random()
Generates a uniform random number between 0 and 1.
Definition: random.hpp:97
std::normal_distribution & RandNormalDist()
Global normal distribution.
Definition: random.hpp:41
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:124
void CustomRandomSeed(const size_t seed)
Definition: random.hpp:86