forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMersenneTwister.cs
87 lines (73 loc) · 1.82 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
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
namespace OpenRA.Support
{
// Quick & dirty Mersenne Twister [MT19937] implementation
public class MersenneTwister
{
readonly uint[] mt = new uint[624];
int index = 0;
public int Last;
public int TotalCount = 0;
public MersenneTwister()
: this(Environment.TickCount) { }
public MersenneTwister(int seed)
{
mt[0] = (uint)seed;
for (var i = 1u; i < mt.Length; i++)
mt[i] = 1812433253u * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i;
}
public int Next()
{
if (index == 0) Generate();
var y = mt[index];
y ^= y >> 11;
y ^= (y << 7) & 2636928640;
y ^= (y << 15) & 4022730752;
y ^= y >> 18;
index = (index + 1) % 624;
TotalCount++;
Last = (int)(y % int.MaxValue);
return Last;
}
public int Next(int low, int high)
{
if (high < low)
throw new ArgumentOutOfRangeException(nameof(high), "Maximum value is less than the minimum value.");
var diff = high - low;
if (diff <= 1)
return low;
return low + Next() % diff;
}
public int Next(int high)
{
return Next(0, high);
}
public float NextFloat()
{
return Math.Abs(Next() / (float)0x7fffffff);
}
void Generate()
{
unchecked
{
for (var i = 0u; i < mt.Length; i++)
{
var y = (mt[i] & 0x80000000) | (mt[(i + 1) % 624] & 0x7fffffff);
mt[i] = mt[(i + 397u) % 624u] ^ (y >> 1);
if ((y & 1) == 1)
mt[i] = mt[i] ^ 2567483615;
}
}
}
}
}