|
1 /* |
|
2 * Copyright (c) 1995, 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
|
4 * |
|
5 * |
|
6 * |
|
7 * |
|
8 * |
|
9 * |
|
10 * |
|
11 * |
|
12 * |
|
13 * |
|
14 * |
|
15 * |
|
16 * |
|
17 * |
|
18 * |
|
19 * |
|
20 * |
|
21 * |
|
22 * |
|
23 * |
|
24 */ |
|
25 |
|
26 // package java.util; |
|
27 |
|
28 import java.io.*; |
|
29 import java.math.BigInteger; |
|
30 import java.util.concurrent.atomic.AtomicLong; |
|
31 import java.util.function.DoubleConsumer; |
|
32 import java.util.function.IntConsumer; |
|
33 import java.util.function.LongConsumer; |
|
34 import java.util.stream.DoubleStream; |
|
35 import java.util.stream.IntStream; |
|
36 import java.util.stream.LongStream; |
|
37 import java.util.stream.StreamSupport; |
|
38 |
|
39 import sun.misc.Unsafe; |
|
40 |
|
41 /** |
|
42 * An instance of this class is used to generate a stream of |
|
43 * pseudorandom numbers. The class uses a 48-bit seed, which is |
|
44 * modified using a linear congruential formula. (See Donald Knuth, |
|
45 * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.) |
|
46 * <p> |
|
47 * If two instances of {@code Random} are created with the same |
|
48 * seed, and the same sequence of method calls is made for each, they |
|
49 * will generate and return identical sequences of numbers. In order to |
|
50 * guarantee this property, particular algorithms are specified for the |
|
51 * class {@code Random}. Java implementations must use all the algorithms |
|
52 * shown here for the class {@code Random}, for the sake of absolute |
|
53 * portability of Java code. However, subclasses of class {@code Random} |
|
54 * are permitted to use other algorithms, so long as they adhere to the |
|
55 * general contracts for all the methods. |
|
56 * <p> |
|
57 * The algorithms implemented by class {@code Random} use a |
|
58 * {@code protected} utility method that on each invocation can supply |
|
59 * up to 32 pseudorandomly generated bits. |
|
60 * <p> |
|
61 * Many applications will find the method {@link Math#random} simpler to use. |
|
62 * |
|
63 * <p>Instances of {@code java.util.Random} are threadsafe. |
|
64 * However, the concurrent use of the same {@code java.util.Random} |
|
65 * instance across threads may encounter contention and consequent |
|
66 * poor performance. Consider instead using |
|
67 * {@link java.util.concurrent.ThreadLocalRandom} in multithreaded |
|
68 * designs. |
|
69 * |
|
70 * <p>Instances of {@code java.util.Random} are not cryptographically |
|
71 * secure. Consider instead using {@link java.security.SecureRandom} to |
|
72 * get a cryptographically secure pseudo-random number generator for use |
|
73 * by security-sensitive applications. |
|
74 * |
|
75 * @author Frank Yellin |
|
76 * @since 1.0 |
|
77 */ |
|
78 public |
|
79 class Random extends AbstractSharedRng implements java.io.Serializable { |
|
80 /** use serialVersionUID from JDK 1.1 for interoperability */ |
|
81 static final long serialVersionUID = 3905348978240129619L; |
|
82 |
|
83 /** |
|
84 * The internal state associated with this pseudorandom number generator. |
|
85 * (The specs for the methods in this class describe the ongoing |
|
86 * computation of this value.) |
|
87 */ |
|
88 private final AtomicLong seed; |
|
89 |
|
90 private static final long multiplier = 0x5DEECE66DL; |
|
91 private static final long addend = 0xBL; |
|
92 private static final long mask = (1L << 48) - 1; |
|
93 |
|
94 private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53) |
|
95 |
|
96 // IllegalArgumentException messages |
|
97 static final String BadBound = "bound must be positive"; |
|
98 static final String BadRange = "bound must be greater than origin"; |
|
99 static final String BadSize = "size must be non-negative"; |
|
100 |
|
101 /** |
|
102 * Creates a new random number generator. This constructor sets |
|
103 * the seed of the random number generator to a value very likely |
|
104 * to be distinct from any other invocation of this constructor. |
|
105 */ |
|
106 public Random() { |
|
107 this(seedUniquifier() ^ System.nanoTime()); |
|
108 } |
|
109 |
|
110 private static long seedUniquifier() { |
|
111 // L'Ecuyer, "Tables of Linear Congruential Generators of |
|
112 // Different Sizes and Good Lattice Structure", 1999 |
|
113 for (;;) { |
|
114 long current = seedUniquifier.get(); |
|
115 long next = current * 181783497276652981L; |
|
116 if (seedUniquifier.compareAndSet(current, next)) |
|
117 return next; |
|
118 } |
|
119 } |
|
120 |
|
121 private static final AtomicLong seedUniquifier |
|
122 = new AtomicLong(8682522807148012L); |
|
123 |
|
124 /** |
|
125 * Creates a new random number generator using a single {@code long} seed. |
|
126 * The seed is the initial value of the internal state of the pseudorandom |
|
127 * number generator which is maintained by method {@link #next}. |
|
128 * |
|
129 * <p>The invocation {@code new Random(seed)} is equivalent to: |
|
130 * <pre> {@code |
|
131 * Random rnd = new Random(); |
|
132 * rnd.setSeed(seed);}</pre> |
|
133 * |
|
134 * @param seed the initial seed |
|
135 * @see #setSeed(long) |
|
136 */ |
|
137 public Random(long seed) { |
|
138 if (getClass() == Random.class) |
|
139 this.seed = new AtomicLong(initialScramble(seed)); |
|
140 else { |
|
141 // subclass might have overriden setSeed |
|
142 this.seed = new AtomicLong(); |
|
143 setSeed(seed); |
|
144 } |
|
145 } |
|
146 |
|
147 private static long initialScramble(long seed) { |
|
148 return (seed ^ multiplier) & mask; |
|
149 } |
|
150 |
|
151 /** |
|
152 * Sets the seed of this random number generator using a single |
|
153 * {@code long} seed. The general contract of {@code setSeed} is |
|
154 * that it alters the state of this random number generator object |
|
155 * so as to be in exactly the same state as if it had just been |
|
156 * created with the argument {@code seed} as a seed. The method |
|
157 * {@code setSeed} is implemented by class {@code Random} by |
|
158 * atomically updating the seed to |
|
159 * <pre>{@code (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)}</pre> |
|
160 * and clearing the {@code haveNextNextGaussian} flag used by {@link |
|
161 * #nextGaussian}. |
|
162 * |
|
163 * <p>The implementation of {@code setSeed} by class {@code Random} |
|
164 * happens to use only 48 bits of the given seed. In general, however, |
|
165 * an overriding method may use all 64 bits of the {@code long} |
|
166 * argument as a seed value. |
|
167 * |
|
168 * @param seed the initial seed |
|
169 */ |
|
170 synchronized public void setSeed(long seed) { |
|
171 this.seed.set(initialScramble(seed)); |
|
172 haveNextNextGaussian = false; |
|
173 } |
|
174 |
|
175 /** |
|
176 * Generates the next pseudorandom number. Subclasses should |
|
177 * override this, as this is used by all other methods. |
|
178 * |
|
179 * <p>The general contract of {@code next} is that it returns an |
|
180 * {@code int} value and if the argument {@code bits} is between |
|
181 * {@code 1} and {@code 32} (inclusive), then that many low-order |
|
182 * bits of the returned value will be (approximately) independently |
|
183 * chosen bit values, each of which is (approximately) equally |
|
184 * likely to be {@code 0} or {@code 1}. The method {@code next} is |
|
185 * implemented by class {@code Random} by atomically updating the seed to |
|
186 * <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre> |
|
187 * and returning |
|
188 * <pre>{@code (int)(seed >>> (48 - bits))}.</pre> |
|
189 * |
|
190 * This is a linear congruential pseudorandom number generator, as |
|
191 * defined by D. H. Lehmer and described by Donald E. Knuth in |
|
192 * <i>The Art of Computer Programming,</i> Volume 3: |
|
193 * <i>Seminumerical Algorithms</i>, section 3.2.1. |
|
194 * |
|
195 * @param bits random bits |
|
196 * @return the next pseudorandom value from this random number |
|
197 * generator's sequence |
|
198 * @since 1.1 |
|
199 */ |
|
200 protected int next(int bits) { |
|
201 long oldseed, nextseed; |
|
202 AtomicLong seed = this.seed; |
|
203 do { |
|
204 oldseed = seed.get(); |
|
205 nextseed = (oldseed * multiplier + addend) & mask; |
|
206 } while (!seed.compareAndSet(oldseed, nextseed)); |
|
207 return (int)(nextseed >>> (48 - bits)); |
|
208 } |
|
209 |
|
210 static final BigInteger thePeriod = BigInteger.valueOf(1L<<48); // Period is 2**48 |
|
211 |
|
212 /** |
|
213 * Returns the period of this random number generator. |
|
214 * |
|
215 * @return the period of this random number generator. |
|
216 */ |
|
217 public BigInteger period() { |
|
218 // Here we also take care of checking for instances of class SecureRandom, |
|
219 // just so as not to bother the implementors of that class. |
|
220 // (Any specific instance of SecureRandom can of course override this method.) |
|
221 // The cast to (Object) is of course needed only during development. |
|
222 return ((Object)this instanceof java.security.SecureRandom) ? Rng.HUGE_PERIOD : thePeriod; |
|
223 } |
|
224 |
|
225 /** |
|
226 * Generates random bytes and places them into a user-supplied |
|
227 * byte array. The number of random bytes produced is equal to |
|
228 * the length of the byte array. |
|
229 * |
|
230 * <p>The method {@code nextBytes} is implemented by class {@code Random} |
|
231 * as if by: |
|
232 * <pre> {@code |
|
233 * public void nextBytes(byte[] bytes) { |
|
234 * for (int i = 0; i < bytes.length; ) |
|
235 * for (int rnd = nextInt(), n = Math.min(bytes.length - i, 4); |
|
236 * n-- > 0; rnd >>= 8) |
|
237 * bytes[i++] = (byte)rnd; |
|
238 * }}</pre> |
|
239 * |
|
240 * @param bytes the byte array to fill with random bytes |
|
241 * @throws NullPointerException if the byte array is null |
|
242 * @since 1.1 |
|
243 */ |
|
244 public void nextBytes(byte[] bytes) { |
|
245 for (int i = 0, len = bytes.length; i < len; ) |
|
246 for (int rnd = nextInt(), |
|
247 n = Math.min(len - i, Integer.SIZE/Byte.SIZE); |
|
248 n-- > 0; rnd >>= Byte.SIZE) |
|
249 bytes[i++] = (byte)rnd; |
|
250 } |
|
251 |
|
252 /** |
|
253 * Returns the next pseudorandom, uniformly distributed {@code int} |
|
254 * value from this random number generator's sequence. The general |
|
255 * contract of {@code nextInt} is that one {@code int} value is |
|
256 * pseudorandomly generated and returned. All 2<sup>32</sup> possible |
|
257 * {@code int} values are produced with (approximately) equal probability. |
|
258 * |
|
259 * <p>The method {@code nextInt} is implemented by class {@code Random} |
|
260 * as if by: |
|
261 * <pre> {@code |
|
262 * public int nextInt() { |
|
263 * return next(32); |
|
264 * }}</pre> |
|
265 * |
|
266 * @return the next pseudorandom, uniformly distributed {@code int} |
|
267 * value from this random number generator's sequence |
|
268 */ |
|
269 public int nextInt() { |
|
270 return next(32); |
|
271 } |
|
272 |
|
273 /** |
|
274 * Returns a pseudorandom {@code int} value between zero (inclusive) |
|
275 * and the specified bound (exclusive). |
|
276 * |
|
277 * @param bound the upper bound (exclusive). Must be positive. |
|
278 * @return a pseudorandom {@code int} value between zero |
|
279 * (inclusive) and the bound (exclusive) |
|
280 * @throws IllegalArgumentException if {@code bound} is not positive |
|
281 */ |
|
282 public int nextInt(int bound) { |
|
283 if (bound <= 0) |
|
284 throw new IllegalArgumentException(BadBound); |
|
285 // Specialize internalNextInt for origin 0 |
|
286 int r = nextInt(); |
|
287 int m = bound - 1; |
|
288 if ((bound & m) == 0) // power of two |
|
289 r &= m; |
|
290 else { // reject over-represented candidates |
|
291 for (int u = r >>> 1; |
|
292 u + m - (r = u % bound) < 0; |
|
293 u = nextInt() >>> 1) |
|
294 ; |
|
295 } |
|
296 return r; |
|
297 } |
|
298 |
|
299 /** |
|
300 * Returns the next pseudorandom, uniformly distributed {@code long} |
|
301 * value from this random number generator's sequence. The general |
|
302 * contract of {@code nextLong} is that one {@code long} value is |
|
303 * pseudorandomly generated and returned. |
|
304 * |
|
305 * <p>The method {@code nextLong} is implemented by class {@code Random} |
|
306 * as if by: |
|
307 * <pre> {@code |
|
308 * public long nextLong() { |
|
309 * return ((long)next(32) << 32) + next(32); |
|
310 * }}</pre> |
|
311 * |
|
312 * Because class {@code Random} uses a seed with only 48 bits, |
|
313 * this algorithm will not return all possible {@code long} values. |
|
314 * |
|
315 * @return the next pseudorandom, uniformly distributed {@code long} |
|
316 * value from this random number generator's sequence |
|
317 */ |
|
318 public long nextLong() { |
|
319 // it's okay that the bottom word remains signed. |
|
320 return ((long)(next(32)) << 32) + next(32); |
|
321 } |
|
322 |
|
323 /** |
|
324 * Returns the next pseudorandom, uniformly distributed |
|
325 * {@code boolean} value from this random number generator's |
|
326 * sequence. The general contract of {@code nextBoolean} is that one |
|
327 * {@code boolean} value is pseudorandomly generated and returned. The |
|
328 * values {@code true} and {@code false} are produced with |
|
329 * (approximately) equal probability. |
|
330 * |
|
331 * <p>The method {@code nextBoolean} is implemented by class {@code Random} |
|
332 * as if by: |
|
333 * <pre> {@code |
|
334 * public boolean nextBoolean() { |
|
335 * return next(1) != 0; |
|
336 * }}</pre> |
|
337 * |
|
338 * @return the next pseudorandom, uniformly distributed |
|
339 * {@code boolean} value from this random number generator's |
|
340 * sequence |
|
341 * @since 1.2 |
|
342 */ |
|
343 public boolean nextBoolean() { |
|
344 return next(1) != 0; |
|
345 } |
|
346 |
|
347 /** |
|
348 * Returns the next pseudorandom, uniformly distributed {@code float} |
|
349 * value between {@code 0.0} and {@code 1.0} from this random |
|
350 * number generator's sequence. |
|
351 * |
|
352 * <p>The general contract of {@code nextFloat} is that one |
|
353 * {@code float} value, chosen (approximately) uniformly from the |
|
354 * range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is |
|
355 * pseudorandomly generated and returned. All 2<sup>24</sup> possible |
|
356 * {@code float} values of the form <i>m x </i>2<sup>-24</sup>, |
|
357 * where <i>m</i> is a positive integer less than 2<sup>24</sup>, are |
|
358 * produced with (approximately) equal probability. |
|
359 * |
|
360 * <p>The method {@code nextFloat} is implemented by class {@code Random} |
|
361 * as if by: |
|
362 * <pre> {@code |
|
363 * public float nextFloat() { |
|
364 * return next(24) / ((float)(1 << 24)); |
|
365 * }}</pre> |
|
366 * |
|
367 * <p>The hedge "approximately" is used in the foregoing description only |
|
368 * because the next method is only approximately an unbiased source of |
|
369 * independently chosen bits. If it were a perfect source of randomly |
|
370 * chosen bits, then the algorithm shown would choose {@code float} |
|
371 * values from the stated range with perfect uniformity.<p> |
|
372 * [In early versions of Java, the result was incorrectly calculated as: |
|
373 * <pre> {@code |
|
374 * return next(30) / ((float)(1 << 30));}</pre> |
|
375 * This might seem to be equivalent, if not better, but in fact it |
|
376 * introduced a slight nonuniformity because of the bias in the rounding |
|
377 * of floating-point numbers: it was slightly more likely that the |
|
378 * low-order bit of the significand would be 0 than that it would be 1.] |
|
379 * |
|
380 * @return the next pseudorandom, uniformly distributed {@code float} |
|
381 * value between {@code 0.0} and {@code 1.0} from this |
|
382 * random number generator's sequence |
|
383 */ |
|
384 public float nextFloat() { |
|
385 return next(24) / ((float)(1 << 24)); |
|
386 } |
|
387 |
|
388 /** |
|
389 * Returns the next pseudorandom, uniformly distributed |
|
390 * {@code double} value between {@code 0.0} and |
|
391 * {@code 1.0} from this random number generator's sequence. |
|
392 * |
|
393 * <p>The general contract of {@code nextDouble} is that one |
|
394 * {@code double} value, chosen (approximately) uniformly from the |
|
395 * range {@code 0.0d} (inclusive) to {@code 1.0d} (exclusive), is |
|
396 * pseudorandomly generated and returned. |
|
397 * |
|
398 * <p>The method {@code nextDouble} is implemented by class {@code Random} |
|
399 * as if by: |
|
400 * <pre> {@code |
|
401 * public double nextDouble() { |
|
402 * return (((long)next(26) << 27) + next(27)) |
|
403 * / (double)(1L << 53); |
|
404 * }}</pre> |
|
405 * |
|
406 * <p>The hedge "approximately" is used in the foregoing description only |
|
407 * because the {@code next} method is only approximately an unbiased |
|
408 * source of independently chosen bits. If it were a perfect source of |
|
409 * randomly chosen bits, then the algorithm shown would choose |
|
410 * {@code double} values from the stated range with perfect uniformity. |
|
411 * <p>[In early versions of Java, the result was incorrectly calculated as: |
|
412 * <pre> {@code |
|
413 * return (((long)next(27) << 27) + next(27)) |
|
414 * / (double)(1L << 54);}</pre> |
|
415 * This might seem to be equivalent, if not better, but in fact it |
|
416 * introduced a large nonuniformity because of the bias in the rounding |
|
417 * of floating-point numbers: it was three times as likely that the |
|
418 * low-order bit of the significand would be 0 than that it would be 1! |
|
419 * This nonuniformity probably doesn't matter much in practice, but we |
|
420 * strive for perfection.] |
|
421 * |
|
422 * @return the next pseudorandom, uniformly distributed {@code double} |
|
423 * value between {@code 0.0} and {@code 1.0} from this |
|
424 * random number generator's sequence |
|
425 * @see Math#random |
|
426 */ |
|
427 public double nextDouble() { |
|
428 return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT; |
|
429 } |
|
430 |
|
431 private double nextNextGaussian; |
|
432 private boolean haveNextNextGaussian = false; |
|
433 |
|
434 /** |
|
435 * Returns the next pseudorandom, Gaussian ("normally") distributed |
|
436 * {@code double} value with mean {@code 0.0} and standard |
|
437 * deviation {@code 1.0} from this random number generator's sequence. |
|
438 * <p> |
|
439 * The general contract of {@code nextGaussian} is that one |
|
440 * {@code double} value, chosen from (approximately) the usual |
|
441 * normal distribution with mean {@code 0.0} and standard deviation |
|
442 * {@code 1.0}, is pseudorandomly generated and returned. |
|
443 * |
|
444 * <p>The method {@code nextGaussian} is implemented by class |
|
445 * {@code Random} as if by a threadsafe version of the following: |
|
446 * <pre> {@code |
|
447 * private double nextNextGaussian; |
|
448 * private boolean haveNextNextGaussian = false; |
|
449 * |
|
450 * public double nextGaussian() { |
|
451 * if (haveNextNextGaussian) { |
|
452 * haveNextNextGaussian = false; |
|
453 * return nextNextGaussian; |
|
454 * } else { |
|
455 * double v1, v2, s; |
|
456 * do { |
|
457 * v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0 |
|
458 * v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0 |
|
459 * s = v1 * v1 + v2 * v2; |
|
460 * } while (s >= 1 || s == 0); |
|
461 * double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
|
462 * nextNextGaussian = v2 * multiplier; |
|
463 * haveNextNextGaussian = true; |
|
464 * return v1 * multiplier; |
|
465 * } |
|
466 * }}</pre> |
|
467 * This uses the <i>polar method</i> of G. E. P. Box, M. E. Muller, and |
|
468 * G. Marsaglia, as described by Donald E. Knuth in <i>The Art of |
|
469 * Computer Programming</i>, Volume 3: <i>Seminumerical Algorithms</i>, |
|
470 * section 3.4.1, subsection C, algorithm P. Note that it generates two |
|
471 * independent values at the cost of only one call to {@code StrictMath.log} |
|
472 * and one call to {@code StrictMath.sqrt}. |
|
473 * |
|
474 * @return the next pseudorandom, Gaussian ("normally") distributed |
|
475 * {@code double} value with mean {@code 0.0} and |
|
476 * standard deviation {@code 1.0} from this random number |
|
477 * generator's sequence |
|
478 */ |
|
479 synchronized public double nextGaussian() { |
|
480 // See Knuth, ACP, Section 3.4.1 Algorithm C. |
|
481 if (haveNextNextGaussian) { |
|
482 haveNextNextGaussian = false; |
|
483 return nextNextGaussian; |
|
484 } else { |
|
485 double v1, v2, s; |
|
486 do { |
|
487 v1 = 2 * nextDouble() - 1; // between -1 and 1 |
|
488 v2 = 2 * nextDouble() - 1; // between -1 and 1 |
|
489 s = v1 * v1 + v2 * v2; |
|
490 } while (s >= 1 || s == 0); |
|
491 double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
|
492 nextNextGaussian = v2 * multiplier; |
|
493 haveNextNextGaussian = true; |
|
494 return v1 * multiplier; |
|
495 } |
|
496 } |
|
497 |
|
498 /** |
|
499 * Serializable fields for Random. |
|
500 * |
|
501 * @serialField seed long |
|
502 * seed for random computations |
|
503 * @serialField nextNextGaussian double |
|
504 * next Gaussian to be returned |
|
505 * @serialField haveNextNextGaussian boolean |
|
506 * nextNextGaussian is valid |
|
507 */ |
|
508 private static final ObjectStreamField[] serialPersistentFields = { |
|
509 new ObjectStreamField("seed", Long.TYPE), |
|
510 new ObjectStreamField("nextNextGaussian", Double.TYPE), |
|
511 new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE) |
|
512 }; |
|
513 |
|
514 /** |
|
515 * Reconstitute the {@code Random} instance from a stream (that is, |
|
516 * deserialize it). |
|
517 */ |
|
518 private void readObject(java.io.ObjectInputStream s) |
|
519 throws java.io.IOException, ClassNotFoundException { |
|
520 |
|
521 ObjectInputStream.GetField fields = s.readFields(); |
|
522 |
|
523 // The seed is read in as {@code long} for |
|
524 // historical reasons, but it is converted to an AtomicLong. |
|
525 long seedVal = fields.get("seed", -1L); |
|
526 if (seedVal < 0) |
|
527 throw new java.io.StreamCorruptedException( |
|
528 "Random: invalid seed"); |
|
529 resetSeed(seedVal); |
|
530 nextNextGaussian = fields.get("nextNextGaussian", 0.0); |
|
531 haveNextNextGaussian = fields.get("haveNextNextGaussian", false); |
|
532 } |
|
533 |
|
534 /** |
|
535 * Save the {@code Random} instance to a stream. |
|
536 */ |
|
537 synchronized private void writeObject(ObjectOutputStream s) |
|
538 throws IOException { |
|
539 |
|
540 // set the values of the Serializable fields |
|
541 ObjectOutputStream.PutField fields = s.putFields(); |
|
542 |
|
543 // The seed is serialized as a long for historical reasons. |
|
544 fields.put("seed", seed.get()); |
|
545 fields.put("nextNextGaussian", nextNextGaussian); |
|
546 fields.put("haveNextNextGaussian", haveNextNextGaussian); |
|
547 |
|
548 // save them |
|
549 s.writeFields(); |
|
550 } |
|
551 |
|
552 // Support for resetting seed while deserializing |
|
553 private static final Unsafe unsafe = Unsafe.getUnsafe(); |
|
554 private static final long seedOffset; |
|
555 static { |
|
556 try { |
|
557 seedOffset = unsafe.objectFieldOffset |
|
558 (Random.class.getDeclaredField("seed")); |
|
559 } catch (Exception ex) { throw new Error(ex); } |
|
560 } |
|
561 private void resetSeed(long seedVal) { |
|
562 unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal)); |
|
563 } |
|
564 } |