10 private const int N = 624;
11 private const int M = 397;
12 private const uint MATRIX_A = 0x9908b0dfU;
13 private const uint UPPER_MASK = 0x80000000U;
14 private const uint LOWER_MASK = 0x7fffffffU;
15 private const uint TEMPER1 = 0x9d2c5680U;
16 private const uint TEMPER2 = 0xefc60000U;
17 private const int TEMPER3 = 11;
18 private const int TEMPER4 = 7;
19 private const int TEMPER5 = 15;
20 private const int TEMPER6 = 18;
24 private UInt32[] mag01;
26 private const double c_realUnitInt = 1.0 / ((double)
int.MaxValue + 1.0);
33 Initialize((uint)Environment.TickCount);
41 Initialize((uint)Math.Abs(seed));
47 private void Initialize(uint seed)
51 mag01 =
new UInt32[] { 0x0U, MATRIX_A };
53 for (
int i = 1; i < N; i++)
54 mt[i] = (UInt32)(1812433253 * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i);
60 private uint NextUInt32()
70 y ^= (y << TEMPER4) & TEMPER1;
71 y ^= (y << TEMPER5) & TEMPER2;
81 var retval = (int)(0x7FFFFFFF & NextUInt32());
82 if (retval == 0x7FFFFFFF)
87 public override int Next(
int minValue,
int maxValue)
89 int range = maxValue - minValue;
90 return minValue +
Next(range);
95 throw new NotImplementedException();
100 throw new NotImplementedException();
106 public override int Next(
int maxValue)
116 return (
int)(0x7FFFFFFF & NextUInt32());
127 private void GenRandAll()
132 y = mt[0] & UPPER_MASK;
136 mt[kk - 1] = mt[kk + (M - 1)] ^ ((y | (p & LOWER_MASK)) >> 1) ^ mag01[p & 1];
138 }
while (++kk < N - M + 1);
142 mt[kk - 1] = mt[kk + (M - N - 1)] ^ ((y | (p & LOWER_MASK)) >> 1) ^ mag01[p & 1];
146 mt[N - 1] = mt[M - 1] ^ ((y | (p & LOWER_MASK)) >> 1) ^ mag01[p & 1];
Mersenne Twister based random
MTRandom()
Constructor with randomized seed
override int Next()
Generates a random value that is greater or equal than 0 and less than Int32.MaxValue
override int Next(int maxValue)
Returns a random value is greater or equal than 0 and less than maxValue
override void NextBytes(byte[] buffer)
override double NextDouble()
Returns random value larger or equal to 0.0 and less than 1.0
MTRandom(int seed)
Constructor with provided 32 bit seed
override void NextBytes(Span< byte > buffer)
int NextInt32()
Generates a random value greater or equal than 0 and less or equal than Int32.MaxValue (inclusively)
override int Next(int minValue, int maxValue)