sisi4s
Loading...
Searching...
No Matches
RandomGenerator.hpp
Go to the documentation of this file.
1#ifndef RANDOM_GENERATOR_DEFINED
2#define RANDOM_GENERATOR_DEFINED
3
4#include <cstdint>
5
12namespace sisi4s {
14public:
15 RandomGenerator(unsigned int seed = 1, int shaking = 32)
16 : x(seed ^ 521288629)
17 , y(seed ^ 362436069)
18 , z(seed ^ 16163801)
19 , n(seed ^ 1131199209)
20 , c(y > x) {
21 for (int i(0); i < shaking; ++i) nextInteger();
22 }
23
24 double nextUniform() {
25 uint64_t i(nextCoarseInteger() >> 20);
26 for (int j(1); j < 6; ++j) {
27 i <<= 8;
28 i |= nextCoarseInteger() >> 24;
29 }
30 return static_cast<double>(i) / static_cast<double>(1l << 52);
31 }
32
37 unsigned int nextInteger() {
38 unsigned int i(nextCoarseInteger() >> 24);
39 // draw 4 random numbers using 8 of their most significant bits
40 for (int j(1); j < 4; ++j) {
41 i <<= 8;
42 i |= nextCoarseInteger() >> 24;
43 }
44 return i;
45 }
46
51 unsigned int nextCoarseInteger() {
52 unsigned int s;
53 if (y - c > x) {
54 s = y - c - x;
55 c = 0;
56 } else {
57 s = y - c - x - 18;
58 c = 1;
59 }
60 x = y;
61 y = z;
62 z = s;
63 n = 69069 * n + 1013904243;
64 return s + n;
65 }
66
67protected:
68 unsigned int x, y, z, n, c;
69};
70} // namespace sisi4s
71
72#endif
Definition RandomGenerator.hpp:13
unsigned int n
Definition RandomGenerator.hpp:68
unsigned int c
Definition RandomGenerator.hpp:68
RandomGenerator(unsigned int seed=1, int shaking=32)
Definition RandomGenerator.hpp:15
unsigned int nextCoarseInteger()
Definition RandomGenerator.hpp:51
unsigned int z
Definition RandomGenerator.hpp:68
unsigned int y
Definition RandomGenerator.hpp:68
double nextUniform()
Definition RandomGenerator.hpp:24
unsigned int x
Definition RandomGenerator.hpp:68
unsigned int nextInteger()
Definition RandomGenerator.hpp:37
Definition Algorithm.hpp:10