-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
223 lines (162 loc) · 4.96 KB
/
main.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
#include <cassert>
#include <cstdint>
#include <cmath>
#include <algorithm>
#include <utility>
#include "LCG_PLE63.hpp"
#include "std_LCG_PLE63.hpp"
#include "uniform_distribution.hpp"
static uint64_t find_period(uint64_t N, std::ostream& os)
{
constexpr uint32_t BILLION = 1024U*1024U*1024U;
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> ugen;
auto seed = ugen.get_seed();
N /= BILLION; // number of billions
for( auto i = 0ULL; i != N; ++i )
{
for( auto k = 0U; k != BILLION; ++k)
{
auto s = ugen();
if (s == seed)
{
return i*BILLION + uint64_t(k);
}
}
os << "Passed: " << i << std::endl;
}
// skip the leftover
return 0ULL;
}
static inline std::pair<float,float> kahan_summation(std::pair<float,float> s, float r)
{
float y = r - s.second;
float t = s.first + y;
return std::pair<float,float>{t, (t - s.first) - y};
}
static std::pair<float,float> test_mean_sigma(uint64_t n)
{
std::pair<float,float> s{0.0f, 0.0f};
std::pair<float,float> d{0.0f, 0.0f};
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> ugen;
std::uniform_distribution<float> rng;
for(uint64_t k = 0ULL; k != n; ++k)
{
float r = rng(ugen);
s = kahan_summation(s, r);
d = kahan_summation(d, r*r);
}
s.first /= float(n);
d.first /= float(n);
d.first = sqrt(d.first);
d.first = (d.first - s.first)*(d.first + s.first);
if (d.first < 0.0f)
d.first = 0.0f;
return std::pair<float,float>{s.first, d.first};
}
static bool test_skip_ahead(int64_t ns)
{
assert( ns > 0LL);
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> ugen;
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> ugens{ugen};
ugens.discard(ns);
// manual skip ahead
uint64_t sum{0ULL};
for(int64_t k = 0; k != ns; ++k)
{
auto r = ugen();
sum += r;
}
// compare manual and fast skip
return (ugen.get_seed() == ugens.get_seed());
}
static bool test_skip_ahead_and_back(int64_t ns)
{
assert( ns > 0LL);
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> ugen;
auto seed = ugen.get_seed();
ugen.discard(ns);
ugen.discard(-ns);
return (ugen.get_seed() == seed);
}
static bool test_skip_zero()
{
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> ugen;
auto seed = ugen.get_seed();
ugen.discard(0LL);
return (ugen.get_seed() == seed);
}
static bool test_skip_backward(int64_t ns)
{
assert( ns < 0LL);
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> ugen;
auto seed = ugen.get_seed();
// internal skip-ahead
ugen.discard(ns);
// manual skip-ahead by same number of steps
uint64_t fns = abs(ns);
uint64_t sum{0ULL};
for(uint64_t k = 0ULL; k != fns; ++k)
{
auto q = ugen();
sum += q;
}
return (ugen.get_seed() == seed);
}
static bool test_skip_backward_and_back(int64_t ns)
{
assert( ns < 0LL);
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> ugen;
auto seed = ugen.get_seed();
ugen.discard(ns);
ugen.discard(-ns);
return (ugen.get_seed() == seed);
}
static bool test_custom_vs_std(uint64_t ns)
{
OTI::lcg_PLE63 rng;
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> rng_std;
for(uint64_t k = 0; k != ns; ++k)
{
rng.sample();
auto rseed = rng.seed();
auto sseed = rng_std();
if (rseed != sseed)
return false;
}
return true;
}
static bool test_skip_custom_vs_std(int64_t ns)
{
OTI::lcg_PLE63 rng;
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> rng_std;
rng.skip(ns);
OTI::lcg_PLE63::seed_type rseed = rng.seed();
rng_std.discard(ns);
OTI::lcg_PLE63::seed_type sseed = rng_std.get_seed();
return (rseed == sseed);
}
int main(int argc, char* argv[])
{
OTI::lcg_PLE63::result_type period = find_period(2500000000ULL, std::cout);
std::cout << "Found period: " << period << std::endl;
std::pair<float,float> r = test_mean_sigma(50000000ULL);
std::cout << "Mean, Sigma " << r.first*2.0f << " " << r.second*12.0f << std::endl;
bool q;
q = test_skip_ahead(777777LL);
assert(q);
q = test_skip_ahead_and_back(12391LL);
assert(q);
test_skip_zero();
assert(q);
q = test_skip_backward(-7788991LL);
assert(q);
q = test_skip_backward_and_back(-12391LL);
assert(q);
q = test_custom_vs_std(10ULL);
assert(q);
q = test_skip_custom_vs_std(1336789ULL);
assert(q);
q = test_skip_custom_vs_std(-123987LL);
assert(q);
return 0;
}