-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom.hpp
171 lines (145 loc) · 4.96 KB
/
random.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
// Based on: https://prng.di.unimi.it/xoshiro128plus.c
#pragma once
/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
#include <chrono>
#include <cstdint>
/* This is xoshiro128+ 1.0, our best and fastest 32-bit generator for 32-bit
floating-point numbers. We suggest to use its upper bits for
floating-point generation, as it is slightly faster than xoshiro128**.
It passes all tests we are aware of except for
linearity tests, as the lowest four bits have low linear complexity, so
if low linear complexity is not considered an issue (as it is usually
the case) it can be used to generate 32-bit outputs, too.
We suggest to use a sign test to extract a random Boolean value, and
right shifts to extract subsets of bits.
The state must be seeded so that it is not everywhere zero. */
/* Written in 2015 by Sebastiano Vigna (vigna@acm.org)
To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
/**
* Xoshiro128+ random number generator wrapper class
* around the original C implementation.
*
* Benchmarks show that this implementation is about 3-10x faster than std::mt19937.
*/
struct xoshiro128p
{
uint32_t s[4];
xoshiro128p() noexcept
{
// seed with current time
uint64_t seed = static_cast<uint64_t>(
std::chrono::high_resolution_clock::now().time_since_epoch().count()
);
uint64_t z = seed_splitmix64(seed);
s[0] = static_cast<uint32_t>(z);
s[1] = static_cast<uint32_t>(z >> 32);
z = seed_splitmix64(z);
s[2] = static_cast<uint32_t>(z);
s[3] = static_cast<uint32_t>(z >> 32);
}
explicit xoshiro128p(uint64_t seed) noexcept
{
uint64_t z = seed_splitmix64(seed);
s[0] = static_cast<uint32_t>(z);
s[1] = static_cast<uint32_t>(z >> 32);
z = seed_splitmix64(z);
s[2] = static_cast<uint32_t>(z);
s[3] = static_cast<uint32_t>(z >> 32);
}
static uint64_t seed_splitmix64(uint64_t x) noexcept
{
uint64_t z = (x += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
static inline uint32_t rotl(const uint32_t x, int k) noexcept
{
return (x << k) | (x >> (32 - k));
}
/**
* Returns a random 32-bit unsigned integer.
*/
inline uint32_t next() noexcept
{
const uint32_t result = s[0] + s[3];
const uint32_t t = s[1] << 9;
s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];
s[2] ^= t;
s[3] = rotl(s[3], 11);
return result;
}
/**
* This is the jump function for the generator. It is equivalent
* to 2^64 calls to next(); it can be used to generate 2^64
* non-overlapping subsequences for parallel computations.
*/
inline void jump() noexcept
{
static const uint32_t JUMP[] = {0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b};
uint32_t s0 = 0;
uint32_t s1 = 0;
uint32_t s2 = 0;
uint32_t s3 = 0;
for (auto jump_value : JUMP)
{
for (int b = 0; b < 32; b++)
{
if (jump_value & (UINT32_C(1) << b))
{
s0 ^= s[0];
s1 ^= s[1];
s2 ^= s[2];
s3 ^= s[3];
}
this->next();
}
}
s[0] = s0;
s[1] = s1;
s[2] = s2;
s[3] = s3;
}
/**
* This is the long-jump function for the generator. It is equivalent to
* 2^96 calls to next(); it can be used to generate 2^32 starting points,
* from each of which jump() will generate 2^32 non-overlapping
* subsequences for parallel distributed computations.
*/
inline void long_jump(void) noexcept
{
static const uint32_t LONG_JUMP[] = {0xb523952e, 0x0b6f099f, 0xccf5a0ef, 0x1c580662};
uint32_t s0 = 0;
uint32_t s1 = 0;
uint32_t s2 = 0;
uint32_t s3 = 0;
for (auto jump_value : LONG_JUMP)
{
for (int b = 0; b < 32; b++)
{
if (jump_value & UINT32_C(1) << b)
{
s0 ^= s[0];
s1 ^= s[1];
s2 ^= s[2];
s3 ^= s[3];
}
this->next();
}
}
s[0] = s0;
s[1] = s1;
s[2] = s2;
s[3] = s3;
}
};