-
Notifications
You must be signed in to change notification settings - Fork 99
/
test_rng.cpp
88 lines (70 loc) · 1.77 KB
/
test_rng.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
#include "framework/configfile.h"
#include "framework/logger.h"
#include "library/xorshift.h"
using namespace OpenApoc;
int main(int argc, char **argv)
{
if (config().parseOptions(argc, argv))
{
return EXIT_FAILURE;
}
Xorshift128Plus<uint64_t> rng{};
uint64_t r1 = rng();
uint64_t r2 = rng();
uint64_t expected_r1 = 0x03aacfee1f751183;
uint64_t expected_r2 = 0xcb8aa3521c8fc259;
uint64_t expected_r3 = 0xdd420b258a17fa82;
if (r1 != expected_r1)
{
LogError("unexpected r1 0x%016x, expected 0x%016x", r1, expected_r1);
return EXIT_FAILURE;
}
if (r2 != expected_r2)
{
LogError("unexpected r2 0x%016x, expected 0x%016x", r2, expected_r2);
return EXIT_FAILURE;
}
// Reseed and check it matches
rng.seed(0);
r1 = rng();
r2 = rng();
if (r1 != expected_r1)
{
LogError("unexpected post-reseed r1 0x%016x, expected 0x%016x", r1, expected_r1);
return EXIT_FAILURE;
}
if (r2 != expected_r2)
{
LogError("unexpected post-reseed r2 0x%016x, expected 0x%016x", r2, expected_r2);
return EXIT_FAILURE;
}
// Save the state to another rng and check that result matches
uint64_t s[2];
Xorshift128Plus<uint64_t> rng2{};
rng.getState(s);
rng2.setState(s);
uint64_t r3 = rng2();
if (r3 != expected_r3)
{
LogError("unexpected r3 0x%016x, expected 0x%016x", r3, expected_r3);
return EXIT_FAILURE;
}
constexpr int num_test_buckets = 4;
constexpr int num_test_iterations = 500000;
unsigned buckets[num_test_buckets];
for (int i = 0; i < num_test_buckets; i++)
{
buckets[i] = 0;
}
for (int i = 0; i < num_test_iterations; i++)
{
auto value = randBoundsExclusive(rng, 0, num_test_buckets);
buckets[value]++;
}
LogWarning("RNG buckets:");
for (int i = 0; i < num_test_buckets; i++)
{
LogWarning("%d:\t%u", i, buckets[i]);
}
return EXIT_SUCCESS;
}