-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyMT.cs
393 lines (346 loc) · 14.2 KB
/
TinyMT.cs
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
namespace TinyMT
{
/// <summary>Tiny Mersenne Twister pseudo-random number generator.</summary>
/// <remarks>
/// This class is compatible with System.Random.
/// So, it can be easly replaced.
/// </remarks>
/// <example>
/// using System;
/// using Random = TinyMT.Random;
/// </example>
public class Random
{
const int MIN_LOOP = 8;
const int MEXP = 127;
const int SH0 = 12;
const int SH1 = 11;
const int SH8 = 8;
const ulong MASK = 0x7fffffffffffffff;
const double MUL = 1.0d / 9007199254740992.0d;
public ulong[] status = new ulong[2];
// From the original unit test (check64.out.txt).
public uint mat1 = 0xfa051f40;
public uint mat2 = 0xffd0fff4;
public ulong tmat = 0x58d02ffeffbfffbc;
/// <summary>Initializes a new instance with default seed and parameters.</summary>
/// <remarks>Compatible with System.Random.</remarks>
public Random()
{
init((ulong)Environment.TickCount);
}
/// <summary>Initializes an instance with the specified seed and default parmeters.</summary>
/// <param name="seed">The seed value of pseudo-random number sequence. If a negative value is specified, the absolute value is used.</param>
/// <remarks>Compatible with System.Random.</remarks>
public Random(int seed)
{
init((seed >= 0) ? (ulong)seed : (ulong)(-seed));
}
/// <summary>Initializes a new instance with the specified seed and default parameters.</summary>
/// <param name="seed">The seed value of pseudo-random number sequence.</param>
public Random(ulong seed)
{
init(seed);
}
/// <summary>Initializes a new instance with the specified seed and parameters.</summary>
/// <param name="seed">The seed value of pseudo-random number sequence.</param>
/// <param name="mat1">State transition parameter.</param>
/// <param name="mat2">State transition parameter.</param>
/// <param name="tmat">Tempering parameter.</param>
public Random(ulong seed, uint mat1, uint mat2, ulong tmat)
{
this.mat1 = mat1;
this.mat2 = mat2;
this.tmat = tmat;
init(seed);
}
/// <summary>Initializes a new instance with an array of integers used as seeds, and default parameters.</summary>
/// <param name="initKey">The array of 64-bit integers, used as seed.</param>
public Random(ulong[] initKey)
{
initByArray(initKey);
}
/// <summary>Initializes a new instance with an array of integers used as seeds, and parameters.</summary>
/// <param name="initKey">The array of 64-bit integers, used as seed.</param>
/// <param name="mat1">State transition parameter.</param>
/// <param name="mat2">State transition parameter.</param>
/// <param name="tmat">Tempering parameter.</param>
public Random(ulong[] initKey, uint mat1, uint mat2, ulong tmat)
{
this.mat1 = mat1;
this.mat2 = mat2;
this.tmat = tmat;
initByArray(initKey);
}
/// <summary>Always returns 127.</summary>
/// <returns>127.</returns>
/// <remarks>Corresponds to the original function tinymt64_get_mexp.</remarks>
public int GetMExp() => MEXP;
/// <summary>Returns a non-negative random integer.</summary>
/// <returns>A random integer (0 <= ret <= int.MaxValue).</returns>
/// <remarks>Compatible with System.Random.</remarks>
public virtual int Next()
{
return (int)(NextUInt64() & (ulong)int.MaxValue);
}
/// <summary>Returns a non-negative random integer that is less than the specified maximum.</summary>
/// <param name="maxValue">The exclusive upper bound of the generated random value. this must be greater than or equal to 0.</param>
/// <returns>A random integer (0 <= ret < <paramref name="maxValue" />).</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="maxValue" /> is less than 0.</exception>
/// <remarks>Compatible with System.Random.</remarks>
public virtual int Next(int maxValue)
{
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException("maxValue", "'maxValue' must be greater than zero.");
}
return (int)(NextDouble() * maxValue);
}
/// <summary>Returns a random integer that is within a specified range.</summary>
/// <param name="minValue">The inclusive lower bound of the generated random value.</param>
/// <param name="maxValue">The exclusive upper bound of the generated random value. this must be greater than or equal to <paramref name="minValue" />.</param>
/// <returns>A random integer (<paramref name="minValue" /> <= ret < <paramref name="maxValue" />).</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="minValue" /> is greater than <paramref name="maxValue" />.</exception>
/// <remarks>Compatible with System.Random.</remarks>
public virtual int Next(int minValue, int maxValue)
{
if (minValue > maxValue)
{
throw new ArgumentOutOfRangeException("minValue", "'minValue' cannot be greater than maxValue.");
}
var range = maxValue - minValue;
return (int)(NextDouble() * range) + minValue;
}
/// <summary>Returns a random 64bit unsigned integer.</summary>
/// <returns>A random integer (0 <= ret < 2^64).</returns>
/// <remarks>Corresponds to the original function tinymt64_generate_uint64.</remarks>
public ulong NextUInt64()
{
nextState();
return temper();
}
/// <summary>Fills the elements of a specified array of bytes with random numbers.</summary>
/// <param name="buffer">The array to be filled with random numbers.</param>
/// <exception cref="ArgumentNullException"><paramref name="buffer" /> is null.</exception>
/// <remarks>
/// Each element of the array is set to a random value (0 <= value <= byte.MaxValue).
/// Compatible with System.Random.
/// </remarks>
public virtual void NextBytes(byte[] buffer)
{
if (buffer is null)
{
throw new ArgumentNullException("buffer");
}
NextBytes((Span<byte>)buffer);
}
/// <summary>Fills the elements of a specified array of bytes with random numbers.</summary>
/// <param name="buffer">The array to be filled with random numbers.</param>
/// <remarks>
/// Each element of the array is set to a random value (0 <= value <= byte.MaxValue).
/// Compatible with System.Random.
/// </remarks>
public virtual void NextBytes(Span<byte> buffer)
{
var i = 0;
for (; i<buffer.Length / 8; i++)
{
var n = NextUInt64();
for (var j=0; j<8; j++)
{
buffer[i*8+j] = (byte)((n >> (j*8)) & 0xff);
}
}
var r = buffer.Length % 8;
if (r > 0)
{
var n = NextUInt64();
for (var j=0; j<r; j++)
{
buffer[i*8+j] = (byte)((n >> j*8) & 0xff);
}
}
}
/// <summary>Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.</summary>
/// <returns>A double-precision floating point number (0.0 <= ret < 1.0).</returns>
/// <remarks>Compatible with System.Random and corresponds to the original function tinymt64_generate_double.</remarks>
public virtual double NextDouble()
{
nextState();
return (double)(temper() >> 11) * MUL;
}
/// <summary>Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.</summary>
/// <returns>A double-precision floating point number (0.0 <= ret < 1.0).</returns>
/// <remarks>Corresponds to the original function tinymt64_generate_double01.</remarks>
public double NextDouble01()
{
nextState();
return temperConv() - 1.0d;
}
/// <summary>Returns a random floating-point number that is greater than or equal to 1.0, and less than 2.0.</summary>
/// <returns>A double-precision floating point number (1.0 <= ret < 2.0).</returns>
/// <remarks>Corresponds to the original function tinymt64_generate_double12.</remarks>
public double NextDouble12()
{
nextState();
return temperConv();
}
/// <summary>Returns a random floating-point number that is greater than 0.0, and less than or equal to 1.0.</summary>
/// <returns>A double-precision floating point number (0.0 < ret <= 1.0).</returns>
/// <remarks>Corresponds to the original function tinymt64_generate_doubleOC.</remarks>
public double NextDoubleOC()
{
nextState();
return 2.0d - temperConv();
}
/// <summary>Returns a random floating-point number that is greater than 0.0, and less than 1.0.</summary>
/// <returns>A double-precision floating point number (0.0 < ret < 1.0).</returns>
/// <remarks>Corresponds to the original function tinymt64_generate_doubleOO.</remarks>
public double NextDoubleOO()
{
nextState();
return temperConvOpen() - 1.0d;
}
// original function: init_func1
ulong iniFunc1(ulong x)
{
return (x ^ (x >> 59)) * 2173292883993;
}
// original function: init_func2
ulong iniFunc2(ulong x)
{
return (x ^ (x >> 59)) * 58885565329898161;
}
// original function: period_certification
void periodCertification()
{
if ((status[0] & MASK) == 0 && status[1] == 0)
{
status[0] = 'T';
status[1] = 'M';
}
}
// original function: tinymt64_init
void init(ulong seed)
{
status[0] = seed ^ ((ulong)mat1 << 32);
status[1] = mat2 ^ tmat;
for (var i=1; i<MIN_LOOP; i++)
{
status[i & 1] ^= (ulong)i + 6364136223846793005
* (status[(i-1)&1] ^ (status[(i-1)&1] >> 62));
}
periodCertification();
}
// original function: tinymt64_init_by_array
void initByArray(ulong[] initKey)
{
const ulong lag = 1;
const ulong mid = 1;
const ulong size = 4;
ulong keyLength = (ulong)initKey.Length;
ulong[] st = new ulong[] { 0, mat1, mat2, tmat };
var count = keyLength + 1;
if (count < MIN_LOOP)
{
count = MIN_LOOP;
}
ulong r = iniFunc1(st[0] ^ st[mid % size] ^ st[(size-1) % size]);
st[mid % size] += r;
r += keyLength;
st[(mid + lag) % size] += r;
st[0] = r;
count--;
ulong i, j;
for (i = 1, j = 0; (j < count) && (j < keyLength); j++) {
r = iniFunc1(st[i] ^ st[(i + mid) % size] ^ st[(i + size - 1) % size]);
st[(i + mid) % size] += r;
r += initKey[j] + i;
st[(i + mid + lag) % size] += r;
st[i] = r;
i = (i + 1) % size;
}
for (; j < count; j++) {
r = iniFunc1(st[i] ^ st[(i + mid) % size] ^ st[(i + size - 1) % size]);
st[(i + mid) % size] += r;
r += i;
st[(i + mid + lag) % size] += r;
st[i] = r;
i = (i + 1) % size;
}
for (j = 0; j < size; j++) {
r = iniFunc2(st[i] + st[(i + mid) % size] + st[(i + size - 1) % size]);
st[(i + mid) % size] ^= r;
r -= i;
st[(i + mid + lag) % size] ^= r;
st[i] = r;
i = (i + 1) % size;
}
status[0] = st[0] ^ st[1];
status[1] = st[2] ^ st[3];
periodCertification();
}
// original function: tinymt64_next_state
void nextState()
{
ulong x;
status[0] &= MASK;
x = status[0] ^ status[1];
x ^= x << SH0;
x ^= x >> 32;
x ^= x << 32;
x ^= x << SH1;
status[0] = status[1];
status[1] = x;
if ((x & 1) != 0)
{
status[0] ^= mat1;
status[1] ^= (ulong)mat2 << 32;
}
}
// original function: tinymt64_temper
ulong temper()
{
var x = status[0] + status[1];
x ^= status[0] >> SH8;
if ((x & 1) != 0)
{
x ^= tmat;
}
return x;
}
// original function: tinymt64_temper_conv
double temperConv()
{
var x = status[0] + status[1];
x ^= status[0] >> SH8;
long u;
if ((x & 1) != 0)
{
u = (long)((x ^ tmat) >> 12) | 0x3ff0000000000000;
}
else
{
u = (long)(x >> 12) | 0x3ff0000000000000;
}
return BitConverter.Int64BitsToDouble(u);
}
// original function: tinymt64_temper_conv_open
double temperConvOpen()
{
var x = status[0] + status[1];
x ^= status[0] >> SH8;
long u;
if ((x & 1) != 0)
{
u = (long)((x ^ tmat) >> 12) | 0x3ff0000000000001;
}
else
{
u = (long)(x >> 12) | 0x3ff0000000000001;
}
return BitConverter.Int64BitsToDouble(u);
}
}
}