-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRandomGenerator.hpp
341 lines (264 loc) · 9.45 KB
/
RandomGenerator.hpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#ifndef _RANDOMGENERATOR_HPP_
#define _RANDOMGENERATOR_HPP_
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <random>
namespace random_generators
{
// Basic CMWC Generator
// A complementary modulo with carry algorithm (proposed by George Marsaglia)
// Details can be found in:
// Marsaglia, G. (2003). "Random number generators". Journal of Modern Applied Statistical Methods 2
// See - http://digitalcommons.wayne.edu/cgi/viewcontent.cgi?article=1725&context=jmasm
// The memory requirement is 34 unsigned 32 bit integers (can be altered using cmwc_lag_size)
// The period length is currently circa 2^1054 - 1 which shold be more than adequate for most purposes
// N.B. cmwc_lag_size must be a power of two
// N.B. cmwc_a_value should be a suitable value according to cmwc_lag_size
class cmwc
{
static constexpr uint32_t cmwc_lag_size = 32;
static constexpr uint64_t cmwc_a_value = 987655670LL;
public:
inline uint32_t operator()()
{
uint32_t i = m_increment;
uint32_t c = m_carry;
uint32_t x;
uint64_t t;
i = (i + 1) & (cmwc_lag_size - 1);
t = cmwc_a_value * m_state[i] + c;
c = (t >> 32);
x = static_cast<uint32_t>((t + c) & 0xFFFFFFFF);
if (x < c)
{
x++;
c++;
}
m_state[i] = (0xFFFFFFFE - x);
m_increment = i;
m_carry = c;
return m_state[i];
}
// Seeding (specific / OS-specific random values)
void seed(uint32_t *init)
{
m_increment = (cmwc_lag_size - 1);
m_carry = 123;
for (uint32_t i = 0; i < cmwc_lag_size; i++)
m_state[i] = init[i];
}
void rand_seed()
{
uint32_t seeds[cmwc_lag_size];
std::random_device rd;
for (uint32_t i = 0; i < cmwc_lag_size; i++)
seeds[i] = rd();
seed(seeds);
}
// State
uint32_t m_increment;
uint32_t m_carry;
uint32_t m_state[cmwc_lag_size];
};
}
template <typename Generator = random_generators::cmwc>
class random_generator
{
public:
class windowed_gaussian_params
{
friend random_generator;
public:
windowed_gaussian_params(double mean, double dev) : m_mean(mean), m_dev(dev)
{
constexpr double inf = HUGE_VAL;
const double a = 1.0 / (dev * sqrt(2.0));
const double b = -mean * a;
m_lo = erf(b);
m_hi = erf(a + b);
// N.B. inf is fine as an input, but nan is not...
m_lo = std::isnan(m_lo) ? erf(-inf) : m_lo;
m_hi = std::isnan(m_hi) ? erf( inf) : m_hi;
};
double mean() const { return m_mean; }
double dev() const { return m_dev; }
private:
double m_mean;
double m_dev;
double m_lo;
double m_hi;
};
random_generator() { m_generator.rand_seed(); }
random_generator(uint32_t *init) { m_generator.seed(init); }
// Seeding (specific / random values)
void seed(uint32_t *init) { m_generator.seed(init); }
void rand_seed() { m_generator.rand_seed(); }
// Generate a Single Pseudo-random Unsigned Integer (full range / in the range [0, n] / in the range [lo, hi])
uint32_t rand_int()
{
return m_generator();
}
uint32_t rand_int(uint32_t n)
{
uint32_t used = n;
uint32_t i;
used |= used >> 1;
used |= used >> 2;
used |= used >> 4;
used |= used >> 8;
used |= used >> 16;
do
i = rand_int() & used; // toss unused bits shortens search
while (i > n);
return i;
}
int32_t rand_int(int32_t lo, int32_t hi)
{
return lo + rand_int(hi - lo);
}
// Generate a 32 bit Random Double (in the range [0,1] / in the range [0, n] / in the range [lo, hi])
double rand_double() { return rand_int() * 2.32830643653869628906e-10; }
double rand_double(double n) { return rand_double() * n; }
double rand_double(double lo, double hi) { return lo + rand_double() * (hi - lo); }
// Generate a 32 bit Random Double of Gaussian Distribution with given Mean / Deviation
double rand_gaussian(double mean, double dev)
{
double x, y, R;
rand_gaussians(x, y, R);
return (R * x) * dev + mean;
}
// Generate two independent gaussians (Mean 0 and Deviation 1)
void rand_gaussians(double& x, double& y)
{
double R;
rand_gaussians(x, y, R);
x *= R;
y *= R;
}
double rand_windowed_gaussian(const windowed_gaussian_params& params)
{
const double r = ltqnorm(0.5 + 0.5 * rand_double(params.m_lo, params.m_hi)) * params.dev() + params.mean();
return std::max(0.0, std::min(1.0, r));
}
double rand_windowed_gaussian(double mean, double dev)
{
const windowed_gaussian_params params(mean, dev);
return rand_windowed_gaussian(params);
}
private:
// Gaussian Helper
void rand_gaussians(double& x, double& y, double& R)
{
x = 0.0;
y = 0.0;
R = 0.0;
while (R >= 1.0 || R == 0.0)
{
x = rand_double(-1.0, 1.0);
y = rand_double(-1.0, 1.0);
R = (x * x) + (y * y);
}
R = sqrt((-2.0 * std::log(R)) / R);
}
// This is adapted from http://home.online.no/~pjacklam/notes/invnorm/impl/sprouse/ltqnorm.c
/*
* Lower tail quantile for standard normal distribution function.
*
* This function returns an approximation of the inverse cumulative
* standard normal distribution function. I.e., given P, it returns
* an approximation to the X satisfying P = Pr{Z <= X} where Z is a
* random variable from the standard normal distribution.
*
* The algorithm uses a minimax approximation by rational functions
* and the result has a relative error whose absolute value is less
* than 1.15e-9.
*
* Author: Peter J. Acklam
* Time-stamp: 2002-06-09 18:45:44 +0200
* E-mail: jacklam@math.uio.no
* WWW URL: http://www.math.uio.no/~jacklam
*
*/
double ltqnorm(double p)
{
/* Coefficients in rational approximations. */
constexpr double a[] =
{
-3.969683028665376e+01,
2.209460984245205e+02,
-2.759285104469687e+02,
1.383577518672690e+02,
-3.066479806614716e+01,
2.506628277459239e+00
};
constexpr double b[] =
{
-5.447609879822406e+01,
1.615858368580409e+02,
-1.556989798598866e+02,
6.680131188771972e+01,
-1.328068155288572e+01
};
constexpr double c[] =
{
-7.784894002430293e-03,
-3.223964580411365e-01,
-2.400758277161838e+00,
-2.549732539343734e+00,
4.374664141464968e+00,
2.938163982698783e+00
};
constexpr double d[] =
{
7.784695709041462e-03,
3.224671290700398e-01,
2.445134137142996e+00,
3.754408661907416e+00
};
constexpr double low = 0.02425;
constexpr double high = 0.97575;
double q, r;
errno = 0;
if (p < 0 || p > 1)
{
errno = EDOM;
return 0.0;
}
else if (p == 0)
{
errno = ERANGE;
return -HUGE_VAL /* minus "infinity" */;
}
else if (p == 1)
{
errno = ERANGE;
return HUGE_VAL /* "infinity" */;
}
else if (p < low)
{
/* Rational approximation for lower region */
q = sqrt(-2.0*std::log(p));
return (((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
}
else if (p > high)
{
/* Rational approximation for upper region */
q = sqrt(-2.0*std::log(1-p));
return -(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
}
else
{
/* Rational approximation for central region */
q = p - 0.5;
r = q*q;
return (((((a[0]*r+a[1])*r+a[2])*r+a[3])*r+a[4])*r+a[5])*q /
(((((b[0]*r+b[1])*r+b[2])*r+b[3])*r+b[4])*r+1);
}
}
// State
Generator m_generator;
};
#endif