-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathRandom.cpp
529 lines (454 loc) · 14.6 KB
/
Random.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Random.cpp is based on Random.h
// Mersenne Twister random number generator -- a C++ class Random
// Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
// Richard J. Wagner v1.0 15 May 2003 [email protected]
// The Mersenne Twister is an algorithm for generating random numbers. It
// was designed with consideration of the flaws in various other generators.
// The period, 2^19937-1, and the order of equidistribution, 623 dimensions,
// are far greater. The generator is also fast; it avoids multiplication and
// division, and it benefits from caches and pipelines. For more information
// see the inventors' web page at http://www.math.keio.ac.jp/~matumoto/emt.html
// Reference
// M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
// Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on
// Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
// Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
// Copyright (C) 2000 - 2003, Richard J. Wagner
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. The names of its contributors may not be used to endorse or promote
// products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// The original code included the following notice:
//
// When you use this, send an email to: [email protected]
// with an appropriate reference to your work.
//
// It would be nice to CC: [email protected] and [email protected]
// when you write.
// Parts of this file are modified beginning in 29.10.09 for adaption in PXL.
// Parts of this file are modified beginning in 10.02.12 for adaption in CRPropa.
#include "crpropa/Random.h"
#include "crpropa/base64.h"
#include <cstdio>
namespace crpropa {
Random::Random(const uint32_t& oneSeed) {
seed(oneSeed);
}
Random::Random(uint32_t * const bigSeed, const uint32_t seedLength) {
seed(bigSeed, seedLength);
}
Random::Random() {
seed();
}
double Random::rand() {
return double(randInt()) * (1.0 / 4294967295.0);
}
double Random::rand(const double& n) {
return rand() * n;
}
double Random::randExc() {
return double(randInt()) * (1.0 / 4294967296.0);
}
double Random::randExc(const double& n) {
return randExc() * n;
}
double Random::randDblExc() {
return (double(randInt()) + 0.5) * (1.0 / 4294967296.0);
}
double Random::randDblExc(const double& n) {
return randDblExc() * n;
}
double Random::rand53() {
uint32_t a = randInt() >> 5, b = randInt() >> 6;
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0); // by Isaku Wada
}
// Return a real number from a normal (Gaussian) distribution with given
// mean and variance by Box-Muller method
double Random::randNorm(const double& mean, const double& variance) {
double r = sqrt(-2.0 * log(1.0 - randDblExc())) * variance;
double phi = 2.0 * 3.14159265358979323846264338328 * randExc();
return mean + r * cos(phi);
}
double Random::randUniform(double min, double max) {
return min + (max - min) * rand();
}
double Random::randRayleigh(double sigma) {
return sigma * sqrt(-2.0 * log(1 - rand()));
}
double Random::randFisher(double kappa) {
return acos(1. + 1. / kappa * log(1 - rand() * (1 - exp(-2 * kappa))));
}
size_t Random::randBin(const std::vector<float> &cdf) {
std::vector<float>::const_iterator it = std::lower_bound(cdf.begin(),
cdf.end(), rand() * cdf.back());
return it - cdf.begin();
}
size_t Random::randBin(const std::vector<double> &cdf) {
std::vector<double>::const_iterator it = std::lower_bound(cdf.begin(),
cdf.end(), rand() * cdf.back());
return it - cdf.begin();
}
Vector3d Random::randVector() {
double z = randUniform(-1.0, 1.0);
double t = randUniform(-1.0 * M_PI, M_PI);
double r = sqrt(1 - z * z);
return Vector3d(r * cos(t), r * sin(t), z);
}
Vector3d Random::randVectorAroundMean(const Vector3d &meanDirection,
double angle) {
Vector3d axis = meanDirection.cross(randVector());
Vector3d v = meanDirection;
return v.getRotated(axis, angle);
}
Vector3d Random::randFisherVector(const Vector3d &meanDirection, double kappa) {
return randVectorAroundMean(meanDirection, randFisher(kappa));
}
Vector3d Random::randConeVector(const Vector3d &meanDirection, double angularRadius) {
const double theta = acos(randUniform(1, cos(angularRadius)));
return randVectorAroundMean(meanDirection, theta);
}
Vector3d Random::randVectorLamberts() {
// random vector following Lamberts cosine law (https://en.wikipedia.org/wiki/Lambert%27s_cosine_law)
// for a surface element with normal vector pointing in positive z-axis (0, 0, 1)
double phi = randUniform(-1.0 * M_PI, M_PI);
double theta = M_PI / 2.0 - acos(sqrt(randUniform(0, 1)));
return Vector3d(cos(phi) * cos(theta), sin(phi) * cos(theta), sin(theta));
}
Vector3d Random::randVectorLamberts(const Vector3d &normalVector) {
// random vector following Lamberts cosine law for a surface element described by normalVector
Vector3d vLambertz = randVectorLamberts();
// find rotation axis that rotates the z-axis to the normalVector of the surface element
Vector3d axis = normalVector.cross(Vector3d(0, 0, 1));
if (axis.getR() < std::numeric_limits<double>::epsilon()) {
axis = Vector3d(0, 0, 1);
}
double angle = normalVector.getAngleTo(Vector3d(0, 0, 1));
// rotate the random Lamberts vector from z-axis to respective surface element
return vLambertz.getRotated(axis / axis.getR(), -angle);
}
Vector3d Random::randomInterpolatedPosition(const Vector3d &a, const Vector3d &b) {
return a + rand() * (b - a);
}
double Random::randPowerLaw(double index, double min, double max) {
if ((min < 0) || (max < min)) {
throw std::runtime_error(
"Power law distribution only possible for 0 <= min <= max");
}
//check for index -1!
if ((std::abs(index + 1.0)) < std::numeric_limits<double>::epsilon()) {
double part1 = log(max);
double part2 = log(min);
return exp((part1 - part2) * rand() + part2);
} else {
double part1 = pow(max, index + 1);
double part2 = pow(min, index + 1);
double ex = 1 / (index + 1);
return pow((part1 - part2) * rand() + part2, ex);
}
}
double Random::randBrokenPowerLaw(double index1, double index2,
double breakpoint, double min, double max) {
if ((min <= 0) || (max < min)) {
throw std::runtime_error(
"Power law distribution only possible for 0 < min <= max");
}
if (min >= breakpoint) {
return this->randPowerLaw(index2, min, max);
} else if (max <= breakpoint) {
return this->randPowerLaw(index2, min, max);
} else {
double intPL1;
// check if index1 = -1
if ((std::abs(index1 + 1.0)) < std::numeric_limits<double>::epsilon()) {
intPL1 = log(breakpoint / min);
} else {
intPL1 = (pow(breakpoint, index1 + 1) - pow(min, index1 + 1))
/ (index1 + 1);
}
double intPL2;
// check if index2 = -1
if ((std::abs(index2 + 1.0)) < std::numeric_limits<double>::epsilon()) {
intPL2 = log(max / breakpoint) * pow(breakpoint, index1 - index2);
} else {
intPL2 = (pow(max, index2 + 1) - pow(breakpoint, index2 + 1))
* pow(breakpoint, index1 - index2) / (index2 + 1);
}
if (rand() > intPL1 / (intPL1 + intPL2))
return randPowerLaw(index2, breakpoint, max);
else
return randPowerLaw(index1, min, breakpoint);
}
}
double Random::randExponential() {
double dum;
do {
dum = rand();
} while (dum < std::numeric_limits<double>::epsilon());
return -1.0 * log(dum);
}
uint32_t Random::randInt() {
if (left == 0)
reload();
--left;
uint32_t s1;
s1 = *pNext++;
s1 ^= (s1 >> 11);
s1 ^= (s1 << 7) & 0x9d2c5680UL;
s1 ^= (s1 << 15) & 0xefc60000UL;
return (s1 ^ (s1 >> 18));
}
uint32_t Random::randInt(const uint32_t& n) {
// Find which bits are used in n
// Optimized by Magnus Jonsson ([email protected])
uint32_t used = n;
used |= used >> 1;
used |= used >> 2;
used |= used >> 4;
used |= used >> 8;
used |= used >> 16;
// Draw numbers until one is found in [0,n]
uint32_t i;
do
i = randInt() & used; // toss unused bits to shorten search
while (i > n);
return i;
}
uint64_t Random::randInt64()
{
int64_t a = randInt();
int64_t b = randInt();
return (b + a << 32);
}
uint64_t Random::randInt64(const uint64_t &n)
{
uint64_t used = n;
used |= used >> 1;
used |= used >> 2;
used |= used >> 4;
used |= used >> 8;
used |= used >> 16;
used |= used >> 32;
// Draw numbers until one is found in [0,n]
uint64_t i;
do
i = randInt64() & used; // toss unused bits to shorten search
while (i > n);
return i;
}
void Random::seed(const uint32_t oneSeed) {
initial_seed.resize(1);
initial_seed[0] = oneSeed;
initialize(oneSeed);
reload();
}
void Random::seed(uint32_t * const bigSeed, const uint32_t seedLength) {
initial_seed.resize(seedLength);
for (size_t i =0; i< seedLength; i++)
{
initial_seed[i] = bigSeed[i];
}
initialize(19650218UL);
int i = 1;
uint32_t j = 0;
int k = (N > seedLength ? N : seedLength);
for (; k; --k) {
state[i] = state[i]
^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL);
state[i] += (bigSeed[j] & 0xffffffffUL) + j;
state[i] &= 0xffffffffUL;
++i;
++j;
if (i >= N) {
state[0] = state[N - 1];
i = 1;
}
if (j >= seedLength)
j = 0;
}
for (k = N - 1; k; --k) {
state[i] = state[i]
^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL);
state[i] -= i;
state[i] &= 0xffffffffUL;
++i;
if (i >= N) {
state[0] = state[N - 1];
i = 1;
}
}
state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array
reload();
}
void Random::seed() {
// First try getting an array from /dev/urandom
FILE* urandom = std::fopen("/dev/urandom", "rb");
if (urandom) {
uint32_t bigSeed[N];
uint32_t *s = bigSeed;
int i = N;
bool success = true;
while (success && i--)
success = std::fread(s++, sizeof(uint32_t), 1, urandom) != 0;
std::fclose(urandom);
if (success) {
seed(bigSeed, N);
return;
}
}
// Was not successful, so use time() and clock() instead
seed(hash(time(NULL), clock()));
}
void Random::initialize(const uint32_t seed) {
uint32_t *s = state;
uint32_t *r = state;
int i = 1;
*s++ = seed & 0xffffffffUL;
for (; i < N; ++i) {
*s++ = (1812433253UL * (*r ^ (*r >> 30)) + i) & 0xffffffffUL;
r++;
}
}
void Random::reload() {
uint32_t *p = state;
int i;
for (i = N - M; i--; ++p)
*p = twist(p[M], p[0], p[1]);
for (i = M; --i; ++p)
*p = twist(p[M - N], p[0], p[1]);
*p = twist(p[M - N], p[0], state[0]);
left = N, pNext = state;
}
uint32_t Random::hash(time_t t, clock_t c) {
static uint32_t differ = 0; // guarantee time-based seeds will change
uint32_t h1 = 0;
unsigned char *p = (unsigned char *) &t;
for (size_t i = 0; i < sizeof(t); ++i) {
h1 *= std::numeric_limits<unsigned char>::max() + 2U;
h1 += p[i];
}
uint32_t h2 = 0;
p = (unsigned char *) &c;
for (size_t j = 0; j < sizeof(c); ++j) {
h2 *= std::numeric_limits<unsigned char>::max() + 2U;
h2 += p[j];
}
return (h1 + differ++) ^ h2;
}
void Random::save(uint32_t* saveArray) const {
uint32_t *sa = saveArray;
const uint32_t *s = state;
int i = N;
for (; i--; *sa++ = *s++) {
}
*sa = left;
}
const std::vector<uint32_t> &Random::getSeed() const
{
return initial_seed;
}
void Random::load(uint32_t * const loadArray) {
uint32_t *s = state;
uint32_t *la = loadArray;
int i = N;
for (; i--; *s++ = *la++) {
}
left = *la;
pNext = &state[N - left];
}
std::ostream& operator<<(std::ostream& os, const Random& mtrand) {
const uint32_t *s = mtrand.state;
int i = mtrand.N;
for (; i--; os << *s++ << "\t") {
}
return os << mtrand.left;
}
std::istream& operator>>(std::istream& is, Random& mtrand) {
uint32_t *s = mtrand.state;
int i = mtrand.N;
for (; i--; is >> *s++) {
}
is >> mtrand.left;
mtrand.pNext = &mtrand.state[mtrand.N - mtrand.left];
return is;
}
#ifdef _OPENMP
#include <omp.h>
#include <stdexcept>
// see http://stackoverflow.com/questions/8051108/using-the-openmp-threadprivate-directive-on-static-instances-of-c-stl-types
const static int MAX_THREAD = 256;
struct RANDOM_TLS_ITEM {
Random r;
char padding[(sizeof(Random) / 64 + 1) * 64 - sizeof(Random)];
};
#ifdef _MSC_VER
__declspec(align(64)) static RANDOM_TLS_ITEM _tls[MAX_THREAD];
#else
__attribute__ ((aligned(64))) static RANDOM_TLS_ITEM _tls[MAX_THREAD];
#endif
Random &Random::instance() {
int i = omp_get_thread_num();
if (i >= MAX_THREAD)
throw std::runtime_error("crpropa::Random: more than MAX_THREAD threads!");
return _tls[i].r;
}
void Random::seedThreads(const uint32_t oneSeed) {
for(size_t i = 0; i < MAX_THREAD; ++i)
_tls[i].r.seed(oneSeed + i);
}
std::vector< std::vector<uint32_t> > Random::getSeedThreads()
{
std::vector< std::vector<uint32_t> > seeds;
for(size_t i = 0; i < omp_get_num_threads(); ++i)
seeds.push_back(_tls[i].r.getSeed() );
return seeds;
}
#else
static Random _random;
Random &Random::instance() {
return _random;
}
void Random::seedThreads(const uint32_t oneSeed) {
_random.seed(oneSeed);
}
std::vector< std::vector<uint32_t> > Random::getSeedThreads()
{
std::vector< std::vector<uint32_t> > seeds;
seeds.push_back(_random.getSeed() );
return seeds;
}
#endif
const std::string Random::getSeed_base64() const
{
return Base64::encode((unsigned char*) &initial_seed[0], sizeof(initial_seed[0]) * initial_seed.size() / sizeof(unsigned char));
}
void Random::seed(const std::string &b64Seed)
{
std::string decoded_data = Base64::decode(b64Seed);
size_t seedSize = decoded_data.size() * sizeof(decoded_data[0]) / sizeof(uint32_t);
seed((uint32_t*)decoded_data.c_str(), seedSize );
}
} // namespace crpropa