-
Notifications
You must be signed in to change notification settings - Fork 6
/
Randomizer.cs
283 lines (265 loc) · 9.66 KB
/
Randomizer.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* Why this class?
*
* I'll tell you why. There are two basic reasons:
* 1. There's a single RNG used throughout the game. It was an instance of System.Random, as a member of Noxico.Toolkit.
* In turn, it was accessed all through the game as Toolkit.Rand, which seems rather superfluous to me compared to
* just plain Random.
* 2. Having it separated like this allows drop-in replacement with another implementation.
*
* As an added bonus, this lets us extend Noxico.Random with extra fun stuff such as coin flips and changing the seed
* value, not only with Now or an int, but with any object that implements GetHashCode().
* If the entire randomizer were reimplemented instead of just wrapping around System.Random, we could easily retrieve
* the current seed value and save it too, which has its uses for gameplay. As it is, we can do that with some sneaky
* use of Reflection and peeking at decompilations.
*/
namespace Noxico
{
/// <summary>
/// Represents a pseudo-random number generator, a device that produces a sequence
/// of numbers that meet certain statistical requirements for randomness.
/// </summary>
public static class Random
{
#if !USE_SYSTEM_RANDOM
private static int seed = 0;
/// <summary>
/// Initializes a new instance of the System.Random class, using a time-dependent
/// default seed value.
/// </summary>
public static void Reseed()
{
seed = System.Environment.TickCount;
}
/// <summary>
/// Initializes a new instance of the System.Random class, using the specified
/// seed value.
/// </summary>
/// <param name="seed">
/// A number used to calculate a starting value for the pseudo-random number
/// sequence. If a negative number is specified, the absolute value of the number
/// is used.
/// </param>
public static void Reseed(int seed)
{
Random.seed = seed;
}
/// <summary>
/// Initializes a new instance of the System.Random class, using the specified
/// object's hash code as the seed value.
/// </summary>
/// <param name="seed">
/// An object whose hash code is used to calculate a starting value for the
/// pseudo-random number sequence.
/// </param>
public static void Reseed(object seed)
{
Random.seed = seed.GetHashCode();
}
/// <summary>
/// Returns a nonnegative random number.
/// </summary>
/// <returns>
/// A 32-bit signed integer greater than or equal to zero and less than System.Int32.MaxValue.
/// </returns>
public static int Next()
{
//Values for the multiplier and increment taken from Pokémon Fire Red.
seed = (seed * 0x41C64E6D) + 0x6073;
return seed;
}
/// <summary>
/// Returns a nonnegative random number less than the specified maximum.
/// </summary>
/// <param name="maxValue">
/// The exclusive upper bound of the random number to be generated. maxValue
/// must be greater than or equal to zero.
/// </param>
/// <returns>
/// A 32-bit signed integer greater than or equal to zero, and less than maxValue;
/// that is, the range of return values ordinarily includes zero but not maxValue.
/// However, if maxValue equals zero, maxValue is returned.
/// </returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// maxValue is less than zero.
/// </exception>
public static int Next(int maxValue)
{
if (maxValue < 0)
throw new System.ArgumentOutOfRangeException("maxValue", string.Format("'{0}' must be positive.", maxValue));
return (int)(NextDouble() * maxValue);
}
/// <summary>
/// Returns a random number within a specified range.
/// </summary>
/// <param name="minValue">
/// The inclusive lower bound of the random number returned.
/// </param>
/// <param name="maxValue">
/// The exclusive upper bound of the random number returned. maxValue must be
/// greater than or equal to minValue.
/// </param>
/// <returns>
/// A 32-bit signed integer greater than or equal to minValue and less than maxValue;
/// that is, the range of return values includes minValue but not maxValue. If
/// minValue equals maxValue, minValue is returned.
/// </returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// minValue is greater than maxValue.
/// </exception>
public static int Next(int minValue, int maxValue)
{
if (minValue > maxValue)
throw new System.ArgumentOutOfRangeException("maxValue", string.Format("'{0}' cannot be greater than {1}.", minValue, maxValue));
long l = maxValue - minValue;
return (((int)(NextDouble() * l)) + minValue);
}
/// <summary>
/// Returns a random number between 0.0 and 1.0.
/// </summary>
/// <returns>
/// A double-precision floating point number greater than or equal to 0.0, and
/// less than 1.0.
/// </returns>
public static double NextDouble()
{
//Not sure if this'll always be in range, but a hundred thousand samples show good results.
var d = (double)Next() / 0x7fffffffL;
if (d < 0)
d = -d;
return d;
}
/// <summary>
/// Returns the last number determined by the RNG, without rolling a new one.
/// </summary>
/// <returns>
/// A 32-bit signed integer greater than or equal to zero and less than System.Int32.MaxValue.
/// </returns>
public static int ExtractSeed()
{
return seed;
}
#else
private static System.Random rand = new System.Random();
/// <summary>
/// Initializes a new instance of the System.Random class, using a time-dependent
/// default seed value.
/// </summary>
public static void Reseed()
{
rand = new System.Random();
}
/// <summary>
/// Initializes a new instance of the System.Random class, using the specified
/// seed value.
/// </summary>
/// <param name="seed">
/// A number used to calculate a starting value for the pseudo-random number
/// sequence. If a negative number is specified, the absolute value of the number
/// is used.
/// </param>
public static void Reseed(int seed)
{
rand = new System.Random(seed);
}
/// <summary>
/// Initializes a new instance of the System.Random class, using the specified
/// object's hash code as the seed value.
/// </summary>
/// <param name="seed">
/// An object whose hash code is used to calculate a starting value for the
/// pseudo-random number sequence.
/// </param>
public static void Reseed(object seed)
{
rand = new System.Random(seed.GetHashCode());
}
/// <summary>
/// Returns a nonnegative random number.
/// </summary>
/// <returns>
/// A 32-bit signed integer greater than or equal to zero and less than System.Int32.MaxValue.
/// </returns>
public static int Next()
{
return rand.Next();
}
/// <summary>
/// Returns a nonnegative random number less than the specified maximum.
/// </summary>
/// <param name="maxValue">
/// The exclusive upper bound of the random number to be generated. maxValue
/// must be greater than or equal to zero.
/// </param>
/// <returns>
/// A 32-bit signed integer greater than or equal to zero, and less than maxValue;
/// that is, the range of return values ordinarily includes zero but not maxValue.
/// However, if maxValue equals zero, maxValue is returned.
/// </returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// maxValue is less than zero.
/// </exception>
public static int Next(int maxValue)
{
return rand.Next(maxValue);
}
/// <summary>
/// Returns a random number within a specified range.
/// </summary>
/// <param name="minValue">
/// The inclusive lower bound of the random number returned.
/// </param>
/// <param name="maxValue">
/// The exclusive upper bound of the random number returned. maxValue must be
/// greater than or equal to minValue.
/// </param>
/// <returns>
/// A 32-bit signed integer greater than or equal to minValue and less than maxValue;
/// that is, the range of return values includes minValue but not maxValue. If
/// minValue equals maxValue, minValue is returned.
/// </returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// minValue is greater than maxValue.
/// </exception>
public static int Next(int minValue, int maxValue)
{
return rand.Next(minValue, maxValue);
}
/// <summary>
/// Returns a random number between 0.0 and 1.0.
/// </summary>
/// <returns>
/// A double-precision floating point number greater than or equal to 0.0, and
/// less than 1.0.
/// </returns>
public static double NextDouble()
{
return rand.NextDouble();
}
//This one's just plain dumb. But it does teach one interesting fact about System.Random, in that it apparently
//pre-rolls about fifty numbers.
/// <summary>
/// Returns the last number determined by the RNG, without rolling a new one.
/// </summary>
/// <returns>
/// A 32-bit signed integer greater than or equal to zero and less than System.Int32.MaxValue.
/// </returns>
public static int ExtractSeed()
{
var randomType = rand.GetType();
var bindings = System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
var nullObject = new object[] { };
var inext = (int)randomType.InvokeMember("inext", bindings, null, rand, null);
var seedArray = (int[])randomType.InvokeMember("SeedArray", bindings, null, rand, null);
return seedArray[inext];
}
#endif
/// <summary>
/// Flips a coin.
/// </summary>
/// <returns>True if it was heads.</returns>
public static bool Flip()
{
return NextDouble() > 0.5;
}
}
}