-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRandom.as
266 lines (172 loc) · 4.04 KB
/
Random.as
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
namespace Random
{
final class Xorshift
{
private uint64 m_iseed;
uint64 seed
{
get const { return m_iseed; }
}
//Xorshift Functions
uint nextInt(uint upper)
{
uint threshold = -upper % upper;
while (true)
{
uint r = nextInt();
if (r >= threshold)
return r % upper;
}
return upper;
}
uint32 nextInt()
{
/*
Used this site to assist with this:
http://excamera.com/sphinx/article-xorshift.html
Might convert this into Xorshift* in the near future.
*/
m_iseed ^= m_iseed >> 12;
m_iseed ^= m_iseed << 25;
m_iseed ^= m_iseed >> 27;
return m_iseed;
}
double nextDouble()
{
return nextInt() * pow(2.0,-32.0);
}
//Xorshift Constructor
Xorshift(uint64 in_seed)
{
m_iseed = in_seed;
}
//Default Constructor
Xorshift()
{
m_iseed = UnixTimestamp();
}
}
final class PCG
{
private uint64 m_iseed;
uint64 seed
{
get const { return m_iseed; }
}
//PCG Functions
uint nextInt(uint upper)
{
uint threshold = -upper % upper;
while (true)
{
uint r = nextInt();
if (r >= threshold)
return r % upper;
}
return upper;
}
uint nextInt()
{
uint64 oldstate = m_iseed;
m_iseed = oldstate * uint64(6364136223846793005) + uint(0);
uint xorshifted = ((oldstate >> uint(18)) ^ oldstate) >> uint(27);
uint rot = oldstate >> uint(59);
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}
double nextDouble()
{
return nextInt() * pow(2.0,-32.0);
}
//PCG Constructors
PCG(uint64 in_seed)
{
m_iseed = in_seed;
}
//Default Constructor
PCG()
{
m_iseed = UnixTimestamp();
}
}
final class MersenneTwister
{
private uint64 m_iseed;
private uint index = 313;
private uint lower_mask = 0x7FFFFFFF;
private uint upper_mask = ~lower_mask;
private array<uint> MT(312);
uint64 seed
{
get const { return m_iseed; }
}
//Mersenne Twister Functions
uint nextInt(uint upper)
{
uint threshold = -upper % upper;
while (true)
{
uint r = nextInt();
if (r >= threshold)
return r % upper;
}
return upper;
}
uint nextInt()
{
if (index >= 312)
{
if (index > 312)
{
g_Log.PrintF("[Random] MersenneTwister Generator was never seeded!\n");
}
twist();
}
uint y = MT[index];
y = y ^ ((y >> uint(29)) & uint(0x5555555555555555));
y = y ^ ((y << uint(17)) & uint(0x71D67FFFEDA60000));
y = y ^ ((y << uint(37)) & uint(0xFFF7EEE000000000));
y = y ^ (y >> uint(43));
++index;
return y;
}
double nextDouble()
{
return nextInt() * pow(2.0,-32.0);
}
void seed_mt(uint64 seed)
{
index = 312;
MT[0] = seed;
for (uint i = 1; i < 312; ++i)
{
MT[i] = (uint(6364136223846793005) * (MT[i - 1] ^ (MT[i - 1] >> (62))) + i);
}
}
void twist()
{
for (uint i = 0; i < 312; ++i)
{
uint x = (MT[i] & upper_mask) + (MT[(i + 1) % 312] & lower_mask);
uint xA = x >> 1;
if (x % 2 != 0)
{
xA = xA ^ uint(0xB5026F5AA96619E9);
}
MT[i] = MT[(i + uint(156)) % 312] ^ xA;
}
index = 0;
}
//MersenneTwister Constructors
MersenneTwister(uint64 in_seed)
{
m_iseed = in_seed;
seed_mt(seed);
}
//Default Constructor
MersenneTwister()
{
m_iseed = UnixTimestamp();
seed_mt(m_iseed);
}
}
}