forked from MetacoSA/NBitcoin
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathMersenneTwister.cs
361 lines (295 loc) · 10.4 KB
/
MersenneTwister.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
using System;
using System.Diagnostics;
/* C# Version Copyright (C) 2001-2004 Akihilo Kramot (Takel). */
/* C# porting from a C-program for MT19937, originaly coded by */
/* Takuji Nishimura, considering the suggestions by */
/* Topher Cooper and Marc Rieffel in July-Aug. 1997. */
/* This library is free software under the Artistic license: */
/* */
/* You can find the original C-program at */
/* http://www.math.keio.ac.jp/~matumoto/mt.html */
/* */
namespace TomanuExtensions.Utils
{
public class MersenneTwister : System.Random
{
/* Period parameters */
private const int N = 624;
private const int M = 397;
private const uint MATRIX_A = 0x9908b0df; /* constant vector a */
private const uint UPPER_MASK = 0x80000000; /* most significant w-r bits */
private const uint LOWER_MASK = 0x7fffffff; /* least significant r bits */
/* Tempering parameters */
private const uint TEMPERING_MASK_B = 0x9d2c5680;
private const uint TEMPERING_MASK_C = 0xefc60000;
private static uint TEMPERING_SHIFT_U(uint y) { return (y >> 11); }
private static uint TEMPERING_SHIFT_S(uint y) { return (y << 7); }
private static uint TEMPERING_SHIFT_T(uint y) { return (y << 15); }
private static uint TEMPERING_SHIFT_L(uint y) { return (y >> 18); }
private uint[] mt = new uint[N]; /* the array for the state vector */
private short mti;
private static uint[] mag01 = { 0x0, MATRIX_A };
/* initializing the array with a NONZERO seed */
public MersenneTwister(uint seed)
{
/* setting initial seeds to mt[N] using */
/* the generator Line 25 of Table 1 in */
/* [KNUTH 1981, The Art of Computer Programming */
/* Vol. 2 (2nd Ed.), pp102] */
mt[0] = seed & 0xffffffffU;
for (mti = 1; mti < N; ++mti)
{
mt[mti] = (69069 * mt[mti - 1]) & 0xffffffffU;
}
}
public MersenneTwister()
: this((uint)System.Environment.TickCount)
{
}
protected uint GenerateUInt()
{
uint y;
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti >= N) /* generate N words at one time */
{
short kk = 0;
for (; kk < N - M; ++kk)
{
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1];
}
for (; kk < N - 1; ++kk)
{
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1];
mti = 0;
}
y = mt[mti++];
y ^= TEMPERING_SHIFT_U(y);
y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
y ^= TEMPERING_SHIFT_L(y);
return y;
}
public virtual uint NextUInt()
{
return this.GenerateUInt();
}
public virtual uint NextUInt(uint maxValue)
{
return (uint)(this.GenerateUInt() / ((double)uint.MaxValue / maxValue));
}
public virtual ushort NextUShort(ushort maxValue)
{
return (ushort)(this.GenerateUInt() / ((double)ushort.MaxValue / maxValue));
}
public virtual ushort NextUShort(int maxValue)
{
if (maxValue > ushort.MaxValue)
throw new ArgumentOutOfRangeException();
if (maxValue <= 0)
throw new ArgumentOutOfRangeException();
return (ushort)(this.GenerateUInt() / ((double)ushort.MaxValue / maxValue));
}
public virtual uint NextUInt(uint minValue, uint maxValue) /* throws ArgumentOutOfRangeException */
{
Debug.Assert(minValue < maxValue);
return (uint)(this.GenerateUInt() / ((double)uint.MaxValue / (maxValue - minValue)) + minValue);
}
public override int Next()
{
return this.Next(int.MaxValue);
}
public override int Next(int maxValue) /* throws ArgumentOutOfRangeException */
{
Debug.Assert(maxValue > 0);
return (int)(this.NextDouble() * maxValue);
}
public override int Next(int minValue, int maxValue)
{
Debug.Assert(maxValue >= minValue);
if (maxValue == minValue)
{
return minValue;
}
else
{
return this.Next(maxValue - minValue) + minValue;
}
}
public override void NextBytes(byte[] buffer) /* throws ArgumentNullException*/
{
int bufLen = buffer.Length;
for (int idx = 0; idx < bufLen; ++idx)
buffer[idx] = (byte)this.Next(256);
}
public override double NextDouble()
{
return (double)this.GenerateUInt() / ((ulong)uint.MaxValue + 1);
}
public float NextFloat()
{
return (float)this.GenerateUInt() / ((ulong)uint.MaxValue + 1);
}
public byte NextByte()
{
return (byte)NextUInt(byte.MaxValue);
}
public char NextChar()
{
return (char)NextUInt(char.MaxValue);
}
public short NextShort()
{
return (short)Next(short.MinValue, short.MaxValue);
}
public ushort NextUShort()
{
return (ushort)NextUInt(ushort.MaxValue);
}
public int NextInt()
{
return (int)GenerateUInt();
}
public long NextLong()
{
return ((long)NextUInt() << 32) | NextUInt();
}
public ulong NextULong()
{
return ((ulong)NextUInt() << 32) | NextUInt();
}
public byte[] NextBytes(int a_length)
{
byte[] result = new byte[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextByte();
return result;
}
public char[] NextChars(int a_length)
{
char[] result = new char[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextChar();
return result;
}
public short[] NextShorts(int a_length)
{
short[] result = new short[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextShort();
return result;
}
public ushort[] NextUShorts(int a_length)
{
ushort[] result = new ushort[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextUShort();
return result;
}
public int[] NextInts(int a_length)
{
int[] result = new int[a_length];
for (int i = 0; i < a_length; i++)
result[i] = Next();
return result;
}
public uint[] NextUInts(int a_length)
{
uint[] result = new uint[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextUInt();
return result;
}
public long[] NextLongs(int a_length)
{
long[] result = new long[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextLong();
return result;
}
public ulong[] NextULongs(int a_length)
{
ulong[] result = new ulong[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextULong();
return result;
}
public string NextString(int a_length)
{
return new string(NextChars(a_length));
}
public double NextDoubleFull()
{
return BitConverter.Int64BitsToDouble(NextLong());
}
public float NextFloatFull()
{
return BitConverter.ToSingle(BitConverter.GetBytes(NextUInt()), 0);
}
public double[] NextDoublesFull(int a_length)
{
double[] result = new double[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextDoubleFull();
return result;
}
public double[] NextDoublesFullSafe(int a_length)
{
double[] result = new double[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextDoubleFullSafe();
return result;
}
public double[] NextDoubles(int a_length)
{
double[] result = new double[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextDouble();
return result;
}
public float[] NextFloatsFull(int a_length)
{
float[] result = new float[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextFloatFull();
return result;
}
public float[] NextFloatsFullSafe(int a_length)
{
float[] result = new float[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextFloatFullSafe();
return result;
}
public float[] NextFloats(int a_length)
{
float[] result = new float[a_length];
for (int i = 0; i < a_length; i++)
result[i] = NextFloat();
return result;
}
public double NextDoubleFullSafe()
{
for (; ; )
{
double d = NextDoubleFull();
if (Double.IsNaN(d))
continue;
return d;
}
}
public float NextFloatFullSafe()
{
for (; ; )
{
float f = NextFloatFull();
if (!Single.IsNaN(f))
return f;
}
}
}
}