13 private const int N = 624;
14 private const int M = 397;
15 private const uint MATRIX_A = 0x9908b0dfU;
16 private const uint UPPER_MASK = 0x80000000U;
17 private const uint LOWER_MASK = 0x7fffffffU;
18 private const int MAX_RAND_INT = 0x7fffffff;
19 private uint[] mag01 = { 0x0U, MATRIX_A };
20 private uint[] mt =
new uint[N];
21 private int mti = N + 1;
28 internal static void Init() {
38 StateTracker.AddTracking(r,
"mt");
39 StateTracker.AddTracking(r,
"mti");
45 init_genrand((uint)DateTime.Now.Millisecond);
49 init_genrand((uint)seed);
53 uint[] initArray =
new uint[init.Length];
54 for (
int i = 0; i < init.Length; ++i)
55 initArray[i] = (uint)init[i];
56 init_by_array(initArray, (uint)initArray.Length);
59 public static int MaxRandomInt {
get {
return 0x7fffffff; } }
65 return genrand_int31();
72 return instance.
Next();
78 public int Next(
int minValue,
int maxValue) {
79 if (minValue > maxValue) {
85 int range = maxValue - minValue;
87 return minValue +
Next() % range;
93 public FP
Next(
float minValue,
float maxValue) {
94 int minValueInt = (int)(minValue * 1000), maxValueInt = (int)(maxValue * 1000);
96 if (minValueInt > maxValueInt) {
97 int tmp = maxValueInt;
98 maxValueInt = minValueInt;
102 return (FP.Floor((maxValueInt - minValueInt + 1) *
NextFP() +
103 minValueInt)) / 1000;
109 public static int Range(
int minValue,
int maxValue) {
110 return instance.
Next(minValue, maxValue);
116 public static FP
Range(
float minValue,
float maxValue) {
117 return instance.
Next(minValue, maxValue);
124 return ((FP)
Next()) / (MaxRandomInt);
145 private float NextFloat() {
146 return (
float)genrand_real2();
149 private float NextFloat(
bool includeOne) {
151 return (
float)genrand_real1();
153 return (
float)genrand_real2();
156 private float NextFloatPositive() {
157 return (
float)genrand_real3();
160 private double NextDouble() {
161 return genrand_real2();
164 private double NextDouble(
bool includeOne) {
166 return genrand_real1();
168 return genrand_real2();
171 private double NextDoublePositive() {
172 return genrand_real3();
175 private double Next53BitRes() {
176 return genrand_res53();
179 public void Initialize() {
180 init_genrand((uint)DateTime.Now.Millisecond);
183 public void Initialize(
int seed) {
184 init_genrand((uint)seed);
187 public void Initialize(
int[] init) {
188 uint[] initArray =
new uint[init.Length];
189 for (
int i = 0; i < init.Length; ++i)
190 initArray[i] = (uint)init[i];
191 init_by_array(initArray, (uint)initArray.Length);
194 private void init_genrand(uint s) {
195 mt[0] = s & 0xffffffffU;
196 for (mti = 1; mti < N; mti++) {
197 mt[mti] = (uint)(1812433253U * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
198 mt[mti] &= 0xffffffffU;
202 private void init_by_array(uint[] init_key, uint key_length) {
204 init_genrand(19650218U);
207 k = (int)(N > key_length ? N : key_length);
209 mt[i] = (uint)((uint)(mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525U)) + init_key[j] + j);
210 mt[i] &= 0xffffffffU;
220 for (k = N - 1; k > 0; k--) {
221 mt[i] = (uint)((uint)(mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) *
223 mt[i] &= 0xffffffffU;
233 uint genrand_int32() {
239 for (kk = 0; kk < N - M; kk++) {
240 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
241 mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1U];
243 for (; kk < N - 1; kk++) {
244 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
245 mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1U];
247 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
248 mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1U];
253 y ^= (y << 7) & 0x9d2c5680U;
254 y ^= (y << 15) & 0xefc60000U;
259 private int genrand_int31() {
260 return (
int)(genrand_int32() >> 1);
264 return (FP)genrand_int32() * (FP.One / (FP)4294967295);
267 double genrand_real1() {
268 return genrand_int32() * (1.0 / 4294967295.0);
270 double genrand_real2() {
271 return genrand_int32() * (1.0 / 4294967296.0);
274 double genrand_real3() {
275 return (((
double)genrand_int32()) + 0.5) * (1.0 / 4294967296.0);
278 double genrand_res53() {
279 uint a = genrand_int32() >> 5, b = genrand_int32() >> 6;
280 return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
int Next(int minValue, int maxValue)
Returns a integer between a min value [inclusive] and a max value [exclusive].
static int Range(int minValue, int maxValue)
Returns a integer between a min value [inclusive] and a max value [exclusive].
int Next()
Returns a random integer.
Generates random numbers based on a deterministic approach.
static FP value
Returns a FP between 0.0 [inclusive] and 1.0 [inclusive].
FP Next(float minValue, float maxValue)
Returns a FP between a min value [inclusive] and a max value [inclusive].
static TSRandom instance
Static instance of TSRandom with seed 1.
static int CallNext()
Returns a random integer.
static TSVector insideUnitSphere
Returns a random TSVector representing a point inside a sphere with radius 1.
FP NextFP()
Returns a FP between 0.0 [inclusive] and 1.0 [inclusive].
static FP Range(float minValue, float maxValue)
Returns a FP between a min value [inclusive] and a max value [inclusive].
static TSRandom New(int seed)
Generates a new instance based on a given seed.