Allolib  1.0
C++ Components For Interactive Multimedia
al_StdRandom.hpp
1 #ifndef INCLUDE_AL_STD_RANDOM_HPP
2 #define INCLUDE_AL_STD_RANDOM_HPP
3 
4 /*
5  For most use case,
6  inline functions in rnd:: namespace will do the work
7  ex) rnd::uniform(0, 100)
8 
9  Keehong Youn, 2017, younkeehong@gmail.com
10 */
11 
12 #include <random>
13 
14 unsigned int al_random_seed();
15 
16 namespace al {
17 
18 // give: interval [a, b]
19 // take: an int in the interval
20 // default range [0:100]
22  public:
23  std::mt19937 engine;
24  std::uniform_int_distribution<int> distribution;
25 
26  rand_uniformi();
27  rand_uniformi(unsigned int seed);
28  void seed(unsigned int seed);
29  int operator()();
30  int operator()(int b);
31  int operator()(int a, int b);
32 };
33 
34 // give: interval [a, b)
35 // take: a float in the interval
36 // default range [0:1)
38  public:
39  std::mt19937 engine;
40  std::uniform_real_distribution<float> distribution;
41 
42  rand_uniformf();
43  rand_uniformf(unsigned int seed);
44  void seed(unsigned int seed);
45  float operator()();
46  float operator()(float b);
47  float operator()(float a, float b);
48 };
49 
50 // give: mean of number of times an event happening in certain interval
51 // take: number of times that event happened in same interval
52 // default mean 100
53 class rand_poisson {
54  public:
55  std::mt19937 engine;
56  std::poisson_distribution<int> distribution;
57 
58  rand_poisson();
59  rand_poisson(unsigned int seed);
60  void seed(unsigned int seed);
61  int operator()();
62  int operator()(float m);
63 };
64 
65 // give: rate of an event happening in unit interval
66 // take: time/distance till next event happens
67 // default rate 1
69  public:
70  std::mt19937 engine;
71  std::exponential_distribution<float> distribution;
72 
74  rand_exponential(unsigned int seed);
75  void seed(unsigned int seed);
76  float operator()();
77  float operator()(float r);
78 };
79 
80 // give: mean and standard deviation
81 // take: a sample from normal distribution
82 // default mean=0, stdev=1
83 class rand_normal {
84  public:
85  std::mt19937 engine;
86  std::normal_distribution<float> distribution;
87 
88  rand_normal();
89  rand_normal(unsigned int seed);
90  void seed(unsigned int seed);
91  float operator()();
92  float operator()(float mean, float stdev);
93 };
94 
95 namespace rnd {
96 
99 rand_normal& global_normal();
100 rand_uniformi& global_ui();
101 
102 // [0, high]
103 inline int uniformi(int high) { return global_ui()(high); }
104 
105 // [low, high]
106 inline int uniformi(int low, int high) {
107  if (high < low) {
108  return global_ui()(high, low);
109  } else {
110  return global_ui()(low, high);
111  }
112 }
113 
115 inline float uniform() { return global()(); }
116 
118 template <class T>
119 inline T uniform(const T& hi) {
120  return global()(0.0f, float(hi));
121 }
122 
124 template <class T>
125 inline T uniform(const T& hi, const T& lo) {
126  if (hi < lo) {
127  return global()(float(hi), float(lo));
128  } else {
129  return global()(float(lo), float(hi));
130  }
131 }
132 
134 inline float uniformS() { return global()(-1.0f, 1.0f); }
135 
137 template <class T>
138 inline float uniformS(const T& lim) {
139  return global()(float(-lim), float(lim));
140 }
141 
143 
147 template <int N, class T>
148 inline void ball(T* point) {
149  T w;
150  do {
151  w = T(0);
152  for (int i = 0; i < N; ++i) {
153  float v = uniformS();
154  point[i] = v;
155  w += v * v;
156  }
157  } while (w >= T(1)); // if on or outside unit ball, try again
158 }
159 
161 template <template <int, class> class VecType, int N, class T>
162 inline void ball(VecType<N, T>& point) {
163  ball<N>(&point[0]);
164 }
165 
167 template <class VecType>
168 inline VecType ball() {
169  VecType v;
170  ball(v);
171  return v;
172 }
173 
175 inline float normal() { return global_normal()(); }
176 inline float gaussian() { return normal(); }
177 
179 inline float triangle() { return 0.5f * (uniformS() + uniformS()); }
180 
182 inline bool prob() { return (uniformS() < 0); }
183 
185 inline bool prob(float p) { return (uniform() < p); }
186 
188 inline float sign(float x = 1.f) {
189  static float arr[2] = {-1.0f, 1.0f};
190  return x * arr[global_ui()(0, 1)];
191 }
192 
193 } // namespace rnd
194 
195 } // namespace al
196 
197 #endif
T gaussian(const T &v)
T mean(const T &v1, const T &v2)
bool prob()
Returns true with probability 0.5.
float normal()
Returns standard normal variate.
float uniformS()
Returns signed uniform random in (-1, 1)
float triangle()
Returns triangle distribution variate, in (-1,1)
rand_uniformf & global()
Get global random number generator.
float sign(float x=1.f)
Returns argument with sign randomly flipped.
float uniform()
Returns uniform random in [0, 1)
void ball(T *point)
Returns point within a unit ball.
Definition: al_App.hpp:23