@@ -106,11 +106,6 @@ public class ReversibleMersenneTwister extends ReverseBitsStreamGenerator implem
106
106
*/
107
107
private int mti ;
108
108
109
- // used to store the first and last element of the initial state
110
- // because, either first or last element can't be restored
111
- private final int [] mt_initial = new int [2 ];
112
- private long twists ;
113
-
114
109
/**
115
110
* Creates a new random number generator.
116
111
* <p>
@@ -168,11 +163,6 @@ private void initGenRand(int seed) {
168
163
}
169
164
170
165
clear (); // Clear normal deviate cache
171
-
172
- // Saving initial state separately
173
- mt_initial [0 ] = mt [0 ];
174
- mt_initial [1 ] = mt [N - 1 ];
175
- twists = 0 ;
176
166
}
177
167
178
168
/**
@@ -189,6 +179,8 @@ private void initGenRand(int seed) {
189
179
@ Override
190
180
public void setSeed (int seed ) {
191
181
initGenRand (seed );
182
+ fixState ();
183
+ clear ();
192
184
}
193
185
194
186
/**
@@ -245,19 +237,15 @@ public void setSeed(int[] seed) {
245
237
}
246
238
247
239
mt [0 ] = 0x80000000 ; // MSB is 1; assuring non-zero initial array
248
-
240
+ fixState ();
249
241
clear (); // Clear normal deviate cache
250
-
251
- // Saving initial state separately
252
- mt_initial [0 ] = mt [0 ];
253
- mt_initial [1 ] = mt [N - 1 ];
254
- twists = 0 ;
255
242
}
256
243
257
244
/**
258
245
* Reinitialize the generator as if just built with the given long seed.
259
- * <p>The state of the generator is exactly the same as a new
260
- * generator built with the same seed.</p>
246
+ * <p>
247
+ * The state of the generator is exactly the same as a new
248
+ * generator built with the same seed.
261
249
*
262
250
* @param seed the initial seed (64 bits integer)
263
251
*/
@@ -266,13 +254,21 @@ public void setSeed(long seed) {
266
254
setSeed (new int []{(int ) (seed >>> 32 ), (int ) (seed & 0xFFFFFFFFL )});
267
255
}
268
256
257
+ private void fixState () {
258
+ // fixing the state so going in reverse first
259
+ // and next go forward should work just fine
260
+ twist ();
261
+ untwist ();
262
+ }
263
+
269
264
/**
270
265
* Generate next pseudorandom number.
271
- * <p>This method is the core generation algorithm. It is used by all the
266
+ * <p>
267
+ * This method is the core generation algorithm. It is used by all the
272
268
* public generation methods for the various primitive types {@link
273
269
* #nextBoolean()}, {@link #nextBytes(byte[])}, {@link #nextDouble()},
274
270
* {@link #nextFloat()}, {@link #nextGaussian()}, {@link #nextInt()},
275
- * {@link #next(int)} and {@link #nextLong()}.</p>
271
+ * {@link #next(int)} and {@link #nextLong()}.
276
272
* <p>
277
273
* Part of the <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c">original code</a>.
278
274
* Modified version of method {@code genrand_int32} to return exactly {@code bits} bits.
@@ -312,10 +308,6 @@ void twist() {
312
308
int x = (mt [i ] & 0x80000000 ) | (mt [(i + 1 ) % N ] & 0x7FFFFFFF );
313
309
mt [i ] = mt [(i + M ) % N ] ^ (x >>> 1 ) ^ MAG01 [x & 0x1 ];
314
310
}
315
- if (++twists == 0 ) {
316
- mt [0 ] = mt_initial [0 ];
317
- mt [N - 1 ] = mt_initial [1 ];
318
- }
319
311
}
320
312
321
313
/**
@@ -344,8 +336,7 @@ void untwist() {
344
336
for (int i = 623 ; i >= 0 ; i --) {
345
337
int result ;
346
338
// first we calculate the first bit
347
- int tmp = mt [i ];
348
- tmp ^= mt [(i + M ) % N ];
339
+ int tmp = mt [i ] ^ mt [(i + M ) % N ];
349
340
// if the first bit is odd, unapply magic
350
341
if ((tmp & 0x80000000 ) == 0x80000000 ) {
351
342
tmp ^= 0x9908B0DF ;
@@ -354,8 +345,7 @@ void untwist() {
354
345
result = (tmp << 1 ) & 0x80000000 ;
355
346
356
347
// work out the remaining 31 bits
357
- tmp = mt [(i - 1 + N ) % N ];
358
- tmp ^= mt [(i + M - 1 ) % N ];
348
+ tmp = mt [(i - 1 + N ) % N ] ^ mt [(i + M - 1 ) % N ];
359
349
if ((tmp & 0x80000000 ) == 0x80000000 ) {
360
350
tmp ^= 0x9908B0DF ;
361
351
// since it was odd, the last bit must have been 1
@@ -365,10 +355,6 @@ void untwist() {
365
355
result |= (tmp << 1 ) & 0x7FFFFFFF ;
366
356
mt [i ] = result ;
367
357
}
368
- if (--twists == 0 ) {
369
- mt [0 ] = mt_initial [0 ];
370
- mt [N - 1 ] = mt_initial [1 ];
371
- }
372
358
}
373
359
374
360
/**
0 commit comments