-
Notifications
You must be signed in to change notification settings - Fork 15
/
xorshift.hh
149 lines (140 loc) · 2.61 KB
/
xorshift.hh
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
/*
Class of pseudorandom number generators, discovered by George Marsaglia
Copyright 2018 Ahmet Inan <[email protected]>
*/
#pragma once
namespace CODE {
class Xorshift32
{
static const uint32_t Y = 2463534242;
uint32_t y_;
public:
typedef uint32_t result_type;
static constexpr result_type min()
{
return 0;
}
static constexpr result_type max()
{
return UINT32_MAX;
}
Xorshift32(uint32_t y = Y) : y_(y) {}
void reset(uint32_t y = Y)
{
y_ = y;
}
uint32_t operator()()
{
y_ ^= y_ << 13;
y_ ^= y_ >> 17;
y_ ^= y_ << 5;
return y_;
}
};
class Xorshift64
{
static const uint64_t X = 88172645463325252;
uint64_t x_;
public:
typedef uint64_t result_type;
static constexpr result_type min()
{
return 0;
}
static constexpr result_type max()
{
return UINT64_MAX;
}
Xorshift64(uint64_t x = X) : x_(x) {}
void reset(uint64_t x = X)
{
x_ = x;
}
uint64_t operator()()
{
x_ ^= x_ << 13;
x_ ^= x_ >> 7;
x_ ^= x_ << 17;
return x_;
}
};
class Xorwow
{
static const uint32_t X = 123456789;
static const uint32_t Y = 362436069;
static const uint32_t Z = 521288629;
static const uint32_t W = 88675123;
static const uint32_t V = 5783321;
static const uint32_t D = 6615241;
uint32_t x_, y_, z_, w_, v_, d_;
public:
typedef uint32_t result_type;
static constexpr result_type min()
{
return 0;
}
static constexpr result_type max()
{
return UINT32_MAX;
}
Xorwow(uint32_t x = X, uint32_t y = Y,
uint32_t z = Z, uint32_t w = W,
uint32_t v = V, uint32_t d = D) :
x_(x), y_(y), z_(z), w_(w), v_(v), d_(d) {}
void reset(uint32_t x = X, uint32_t y = Y,
uint32_t z = Z, uint32_t w = W,
uint32_t v = V, uint32_t d = D)
{
x_ = x;
y_ = y;
z_ = z;
w_ = w;
v_ = v;
d_ = d;
}
uint32_t operator()()
{
uint32_t t = x_ ^ (x_ >> 2);
x_ = y_; y_ = z_; z_ = w_; w_ = v_;
v_ = (v_ ^ (v_ << 4)) ^ (t ^ (t << 1));
d_ += 362437;
return d_ + v_;
}
};
class Xorshift128
{
static const uint32_t X = 123456789;
static const uint32_t Y = 362436069;
static const uint32_t Z = 521288629;
static const uint32_t W = 88675123;
uint32_t x_, y_, z_, w_;
public:
typedef uint32_t result_type;
static constexpr result_type min()
{
return 0;
}
static constexpr result_type max()
{
return UINT32_MAX;
}
Xorshift128(uint32_t x = X, uint32_t y = Y,
uint32_t z = Z, uint32_t w = W) :
x_(x), y_(y), z_(z), w_(w) {}
void reset(uint32_t x = X, uint32_t y = Y,
uint32_t z = Z, uint32_t w = W)
{
x_ = x;
y_ = y;
z_ = z;
w_ = w;
}
uint32_t operator()()
{
uint32_t t = (x_ ^ (x_ << 11));
x_ = y_; y_ = z_; z_ = w_;
w_ = (w_ ^ (w_ >> 19)) ^ (t ^ (t >> 8));
return w_;
}
};
}