Repository navigation
Expand file tree
/
Copy pathRNG.cpp
More file actions
215 lines (189 loc) · 5.18 KB
/
Copy pathRNG.cpp
File metadata and controls
215 lines (189 loc) · 5.18 KB
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
/*
* Copyright 2010-2013 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it 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.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RNG.h"
#define _USE_MATH_DEFINES
#include <cmath>
#include <ctime>
#include <climits>
/*Period parameters */
#define CMATH_N 624
#define CMATH_M 397
#define CMATH_MATRIX_A 0x9908b0df /*constant vector a */
#define CMATH_UPPER_MASK 0x80000000 /*most significant w-r bits */
#define CMATH_LOWER_MASK 0x7fffffff /*least significant r bits */
/*Tempering parameters */
#define CMATH_TEMPERING_MASK_B 0x9d2c5680
#define CMATH_TEMPERING_MASK_C 0xefc60000
#define CMATH_TEMPERING_SHIFT_U(y) (y >> 11)
#define CMATH_TEMPERING_SHIFT_S(y) (y << 7)
#define CMATH_TEMPERING_SHIFT_T(y) (y << 15)
#define CMATH_TEMPERING_SHIFT_L(y) (y >> 18)
namespace OpenXcom
{
namespace RNG
{
unsigned int rseed = 1;
unsigned int mt[CMATH_N]; /*the array for the state vector */
int mti = CMATH_N+1; /*mti==N+1 means mt [N ] is not initialized **/
/**
* Returns the current seed in use by the generator.
* @return Current seed.
*/
unsigned int getSeed()
{
return rseed;
}
/**
* Changes the current seed in use by the generator.
* @param n New seed.
*/
void setSeed(unsigned int n)
{
/* 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]= n & 0xffffffff;
for (mti=1; mti<CMATH_N; mti++)
mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
rseed = n;
}
/**
* Generates a number using a Mersenne Twister
* pseudorandom number generator.
* @return Random unsigned 32-bit number (0-LONG_MAX)
*/
unsigned int random()
{
unsigned int y;
static unsigned int mag01[2]={0x0, CMATH_MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti >= CMATH_N) { /* generate N words at one time */
int kk;
if (mti == CMATH_N+1) /* if sgenrand() has not been called, */
setSeed(4357); /* a default initial seed is used */
for (kk=0;kk<CMATH_N-CMATH_M;kk++) {
y = (mt[kk]&CMATH_UPPER_MASK)|(mt[kk+1]&CMATH_LOWER_MASK);
mt[kk] = mt[kk+CMATH_M] ^ (y >> 1) ^ mag01[y & 0x1];
}
for (;kk<CMATH_N-1;kk++) {
y = (mt[kk]&CMATH_UPPER_MASK)|(mt[kk+1]&CMATH_LOWER_MASK);
mt[kk] = mt[kk+(CMATH_M-CMATH_N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (mt[CMATH_N-1]&CMATH_UPPER_MASK)|(mt[0]&CMATH_LOWER_MASK);
mt[CMATH_N-1] = mt[CMATH_M-1] ^ (y >> 1) ^ mag01[y & 0x1];
mti = 0;
}
y = mt[mti++];
y ^= CMATH_TEMPERING_SHIFT_U(y);
y ^= CMATH_TEMPERING_SHIFT_S(y) & CMATH_TEMPERING_MASK_B;
y ^= CMATH_TEMPERING_SHIFT_T(y) & CMATH_TEMPERING_MASK_C;
y ^= CMATH_TEMPERING_SHIFT_L(y);
return y;
}
/**
* Seeds the random generator with the current time.
*/
void init()
{
setSeed((unsigned int)time(NULL));
}
/**
* Seeds the random generator with a new number.
* @param seed New seed.
*/
void init(unsigned int seed)
{
setSeed(seed);
}
/**
* Generates a random integer number within a certain range.
* @param min Minimum number, inclusive.
* @param max Maximum number, inclusive.
* @return Generated number.
*/
int generate(int min, int max)
{
unsigned int num = random();
return (int)(num % (max - min + 1) + min);
}
/**
* Generates a random decimal number within a certain range.
* @param min Minimum number.
* @param max Maximum number.
* @return Generated number.
*/
double generate(double min, double max)
{
double num = random();
return (double)(num / ((double)UINT_MAX / (max - min)) + min);
}
/**
* Normal random variate generator
* @param m mean
* @param s standard deviation
* @return normally distributed value.
*/
double boxMuller(double m, double s)
{
double y1;
static double y2;
static int use_last = 0;
if (use_last) /* use value from previous call */
{
y1 = y2;
use_last = 0;
}
else
{
double x1, x2, w;
do {
x1 = 2.0 * generate(0.0, 1.0) - 1.0;
x2 = 2.0 * generate(0.0, 1.0) - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 );
w = sqrt( (-2.0 * log( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;
use_last = 1;
}
return( m + y1 * s );
}
/**
* Generates a random percent chance of an event occuring,
* and returns the result
* @param value Value percentage (0-100%)
* @return True if the chance succeeded.
*/
bool percent(int value)
{
return (generate(0, 99) < value);
}
/**
* Generates a random positive integer up to a number.
* @param max Maximum number, exclusive.
* @return Generated number.
*/
int generateEx(int max)
{
unsigned int num = random();
return (int)(num % max);
}
}
}