-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrandom.cpp
307 lines (242 loc) · 6.05 KB
/
Prandom.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// FILE: Prandom.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.6
// PURPOSE: Arduino library for random number generation with Python random interface
// URL: https://github.com/RobTillaart/Prandom
//
// code based upon Python implementation although some small
// optimizations and tweaks were needed to get it working.
#include "Prandom.h"
Prandom::Prandom()
{
seed();
}
Prandom::Prandom(uint32_t s)
{
seed(s);
}
void Prandom::seed()
{
// no argument ==> time based.
seed(_rndTime());
}
void Prandom::seed(uint32_t s, uint32_t t)
{
// set Marsaglia constants, prevent 0 as value
if (s == 0) s = 1;
if (t == 0) t = 2;
_m_w = s;
_m_z = t;
}
uint32_t Prandom::getrandbits(uint8_t n)
{
uint8_t shift = min(31, n - 1);
return _rnd(1UL << shift);
}
uint32_t Prandom::randrange(uint32_t stop)
{
return _rnd(stop);
}
uint32_t Prandom::randrange(uint32_t start, uint32_t stop, uint32_t step)
{
if (step == 1) return start + _rnd(stop - start);
return start + step * _rnd((stop - start + step - 1) / step);
}
// returns value between 0 and top which defaults to 1.0
// the parameter does not exist in Python
// note: not all possible (0xFFFFFFFF) values are used
// function has an uniform distribution.
float Prandom::random(const float top)
{
if (top == 0) return 0;
float f = (top * __random()) / 0xFFFFFFFF;
return f;
}
float Prandom::uniform(float lo, float hi)
{
if (lo == hi) return lo;
return lo + random(hi - lo);
}
float Prandom::triangular(float lo, float hi, float mid)
{
if (lo == hi) return lo;
float val = random();
if (val > mid)
{
val = 1 - val;
mid = 1 - mid;
float t = hi;
hi = lo;
lo = t;
}
return lo + (hi - lo) * sqrt(val * mid);
}
// minor optimization.
float Prandom::normalvariate(float mu, float sigma)
{
// const float NV_MAGICCONST = 4 * exp(-0.5)/sqrt(2.0);
const float NV_MAGICCONST = 2 * exp(-0.5) / sqrt(2.0);
float u1, u2, z;
while (true)
{
u1 = random();
u2 = 1 - random();
z = NV_MAGICCONST * (u1 - 0.5) / u2 ;
// if ((z * z / 4) <= -log(u2)) break;
if ((z * z) <= -log(u2)) break;
}
return z * sigma + mu;
}
float Prandom::lognormvariate(float mu, float sigma)
{
return exp(normalvariate(mu, sigma));
}
// implemented slightly differently
float Prandom::gauss(float mu, float sigma)
{
static bool generate = false;
static float next = 0;
float z = 0;
generate = !generate;
if (generate == false)
{
z = next;
}
else
{
float x2pi = random(TWO_PI);
float g2rad = sqrt( -2.0 * log(1.0 - random()));
z = cos(x2pi) * g2rad;
next = sin(x2pi) * g2rad;
}
return z * sigma + mu;
};
float Prandom::expovariate(float lambda)
{
return -log(1.0 - random()) / lambda;
}
// alpha & beta > 0
float Prandom::gammavariate(float alpha, float beta)
{
const float LOG4 = log(4);
const float SG_MAGICCONST = 1.0 + log(4.5);
if (alpha > 1.0)
{
// # Uses R.C.H. Cheng, "The generation of Gamma
// # variables with non-integral shape parameters",
// # Applied Statistics, (1977), 26, No. 1, p71-74
float ainv = sqrt(2.0 * alpha - 1.0);
float bbb = alpha - LOG4;
float ccc = alpha + ainv;
float u1, u2, v, x, z, r;
while (true)
{
u1 = random();
if (u1 < 1e-7) continue;
if (u1 > 0.9999999) continue; // needed?
u2 = 1.0 - random();
v = log(u1 / (1.0 - u1)) / ainv;
x = alpha * exp(v);
z = u1 * u1 * u2;
r = bbb + ccc * v - x;
if ( ( (r + SG_MAGICCONST - 4.5 * z) >= 0.0) ||
(r >= log(z)) )
{
return x * beta;
}
}
}
else if (alpha == 1.0)
{
return -log(1.0 - random()) * beta;
}
else // alpha in 0..1
{
// # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
float u, b, p, x, u1;
while (true)
{
u = random();
b = (EULER + alpha) / EULER;
p = b * u;
if ( p <= 1.0) x = pow(p, (1.0 / alpha));
else x = -log((b - p) / alpha);
u1 = random();
if (p > 1.0)
{
if (u1 <= pow(x, (alpha - 1.0))) break;
}
else
{
if (u1 <= exp(-x)) break;
}
}
return x * beta;
}
}
float Prandom::betavariate(float alpha, float beta)
{
float y = gammavariate(alpha, 1.0);
if (y == 0) return 0.0;
return y / (y + gammavariate(beta, 1.0));
};
float Prandom::paretovariate(float alpha)
{
float u = 1 - random();
return pow(u, (-1.0 / alpha));
}
float Prandom::weibullvariate(float alpha, float beta)
{
float u = 1 - random();
return alpha * pow(-log(u), 1.0 / beta);
}
float Prandom::vonmisesvariate(float mu, float kappa)
{
if (kappa <= 1e-6) return TWO_PI * random();
float s = 0.5 / kappa;
float r = s + sqrt(1.0 + s * s);
float u1, u2, u3, z, d, q, f, theta;
do
{
u1 = random();
z = cos(PI * u1);
d = z / (r + z);
u2 = random();
} while ( ( u2 >= 1.0 - d * d ) && (u2 > (1.0 - d) * exp(d)) );
q = 1.0 / r;
f = (q + z) / (1.0 + q * z);
u3 = random();
if (u3 > 0.5) theta = mu + acos(f);
else theta = mu - acos(f);
while (theta < 0) theta += TWO_PI;
while (theta > TWO_PI) theta -= TWO_PI;
return theta;
}
////////////////////////////////////////////////////////////////////////////
//
// PRIVATE
//
uint32_t Prandom::_rndTime()
{
return (micros() + (micros() >> 2) ) ^ (millis());
}
// TODO how to guarantee it uniform between 0 .. n-1
uint32_t Prandom::_rnd(uint32_t n)
{
// float formula works fastest but it looses precision for large values of n
// as floats have only 23 bit mantissa
uint32_t val = __random();
if (n > 0x003FFFFF) return val % n; // distribution will fail here
return (n * 1.0 * val) / 0xFFFFFFFF;
}
// An example of a simple pseudo-random number generator is the
// Multiply-with-carry method invented by George Marsaglia.
// two initializers (not null)
uint32_t Prandom::__random()
{
_m_z = 36969L * (_m_z & 65535L) + (_m_z >> 16);
_m_w = 18000L * (_m_w & 65535L) + (_m_w >> 16);
return (_m_z << 16) + _m_w; /* 32-bit result */
}
// -- END OF FILE --