Client LuaCsForBarotrauma
MTRandom.cs
1 using System;
2 
3 namespace Barotrauma
4 {
8  public sealed class MTRandom : Random
9  {
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;
21 
22  private UInt32[] mt;
23  private int mti;
24  private UInt32[] mag01;
25 
26  private const double c_realUnitInt = 1.0 / ((double)int.MaxValue + 1.0);
27 
31  public MTRandom()
32  {
33  Initialize((uint)Environment.TickCount);
34  }
35 
39  public MTRandom(int seed)
40  {
41  Initialize((uint)Math.Abs(seed));
42  }
43 
47  private void Initialize(uint seed)
48  {
49  mt = new UInt32[N];
50  mti = N + 1;
51  mag01 = new UInt32[] { 0x0U, MATRIX_A };
52  mt[0] = seed;
53  for (int i = 1; i < N; i++)
54  mt[i] = (UInt32)(1812433253 * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i);
55  }
56 
60  private uint NextUInt32()
61  {
62  UInt32 y;
63  if (mti >= N)
64  {
65  GenRandAll();
66  mti = 0;
67  }
68  y = mt[mti++];
69  y ^= (y >> TEMPER3);
70  y ^= (y << TEMPER4) & TEMPER1;
71  y ^= (y << TEMPER5) & TEMPER2;
72  y ^= (y >> TEMPER6);
73  return y;
74  }
75 
79  public override int Next()
80  {
81  var retval = (int)(0x7FFFFFFF & NextUInt32());
82  if (retval == 0x7FFFFFFF)
83  return NextInt32();
84  return retval;
85  }
86 
87  public override int Next(int minValue, int maxValue)
88  {
89  int range = maxValue - minValue;
90  return minValue + Next(range);
91  }
92 
93  public override void NextBytes(byte[] buffer)
94  {
95  throw new NotImplementedException();
96  }
97 
98  public override void NextBytes(Span<byte> buffer)
99  {
100  throw new NotImplementedException();
101  }
102 
106  public override int Next(int maxValue)
107  {
108  return (int)(NextDouble() * maxValue);
109  }
110 
114  public int NextInt32()
115  {
116  return (int)(0x7FFFFFFF & NextUInt32());
117  }
118 
122  public override double NextDouble()
123  {
124  return c_realUnitInt * NextInt32();
125  }
126 
127  private void GenRandAll()
128  {
129  int kk = 1;
130  UInt32 y;
131  UInt32 p;
132  y = mt[0] & UPPER_MASK;
133  do
134  {
135  p = mt[kk];
136  mt[kk - 1] = mt[kk + (M - 1)] ^ ((y | (p & LOWER_MASK)) >> 1) ^ mag01[p & 1];
137  y = p & UPPER_MASK;
138  } while (++kk < N - M + 1);
139  do
140  {
141  p = mt[kk];
142  mt[kk - 1] = mt[kk + (M - N - 1)] ^ ((y | (p & LOWER_MASK)) >> 1) ^ mag01[p & 1];
143  y = p & UPPER_MASK;
144  } while (++kk < N);
145  p = mt[0];
146  mt[N - 1] = mt[M - 1] ^ ((y | (p & LOWER_MASK)) >> 1) ^ mag01[p & 1];
147  }
148  }
149 }
Mersenne Twister based random
Definition: MTRandom.cs:9
MTRandom()
Constructor with randomized seed
Definition: MTRandom.cs:31
override int Next()
Generates a random value that is greater or equal than 0 and less than Int32.MaxValue
Definition: MTRandom.cs:79
override int Next(int maxValue)
Returns a random value is greater or equal than 0 and less than maxValue
Definition: MTRandom.cs:106
override void NextBytes(byte[] buffer)
Definition: MTRandom.cs:93
override double NextDouble()
Returns random value larger or equal to 0.0 and less than 1.0
Definition: MTRandom.cs:122
MTRandom(int seed)
Constructor with provided 32 bit seed
Definition: MTRandom.cs:39
override void NextBytes(Span< byte > buffer)
Definition: MTRandom.cs:98
int NextInt32()
Generates a random value greater or equal than 0 and less or equal than Int32.MaxValue (inclusively)
Definition: MTRandom.cs:114
override int Next(int minValue, int maxValue)
Definition: MTRandom.cs:87