1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1995-2007 Sun Microsystems, Inc. All Rights Reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
6 |
< |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
5 |
> |
* This code is free software; you can redistribute it and/or modify it |
6 |
> |
* under the terms of the GNU General Public License version 2 only, as |
7 |
> |
* published by the Free Software Foundation. Sun designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Sun in the LICENSE file that accompanied this code. |
10 |
> |
* |
11 |
> |
* This code is distributed in the hope that it will be useful, but WITHOUT |
12 |
> |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 |
> |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
> |
* version 2 for more details (a copy is included in the LICENSE file that |
15 |
> |
* accompanied this code). |
16 |
> |
* |
17 |
> |
* You should have received a copy of the GNU General Public License version |
18 |
> |
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
> |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
> |
* |
21 |
> |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
> |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
> |
* have any questions. |
24 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
27 |
|
import java.io.*; |
28 |
|
import java.util.concurrent.atomic.AtomicLong; |
29 |
+ |
import sun.misc.Unsafe; |
30 |
|
|
31 |
|
/** |
32 |
|
* An instance of this class is used to generate a stream of |
51 |
|
* Many applications will find the method {@link Math#random} simpler to use. |
52 |
|
* |
53 |
|
* @author Frank Yellin |
35 |
– |
* @version %I%, %G% |
54 |
|
* @since 1.0 |
55 |
|
*/ |
56 |
|
public |
62 |
|
* The internal state associated with this pseudorandom number generator. |
63 |
|
* (The specs for the methods in this class describe the ongoing |
64 |
|
* computation of this value.) |
47 |
– |
* |
48 |
– |
* @serial |
65 |
|
*/ |
66 |
< |
private AtomicLong seed; |
66 |
> |
private final AtomicLong seed; |
67 |
|
|
68 |
|
private final static long multiplier = 0x5DEECE66DL; |
69 |
|
private final static long addend = 0xBL; |
117 |
|
synchronized public void setSeed(long seed) { |
118 |
|
seed = (seed ^ multiplier) & mask; |
119 |
|
this.seed.set(seed); |
120 |
< |
haveNextNextGaussian = false; |
120 |
> |
haveNextNextGaussian = false; |
121 |
|
} |
122 |
|
|
123 |
|
/** |
149 |
|
long oldseed, nextseed; |
150 |
|
AtomicLong seed = this.seed; |
151 |
|
do { |
152 |
< |
oldseed = seed.get(); |
153 |
< |
nextseed = (oldseed * multiplier + addend) & mask; |
152 |
> |
oldseed = seed.get(); |
153 |
> |
nextseed = (oldseed * multiplier + addend) & mask; |
154 |
|
} while (!seed.compareAndSet(oldseed, nextseed)); |
155 |
|
return (int)(nextseed >>> (48 - bits)); |
156 |
|
} |
175 |
|
* @since 1.1 |
176 |
|
*/ |
177 |
|
public void nextBytes(byte[] bytes) { |
178 |
< |
for (int i = 0, len = bytes.length; i < len; ) |
179 |
< |
for (int rnd = nextInt(), |
180 |
< |
n = Math.min(len - i, Integer.SIZE/Byte.SIZE); |
181 |
< |
n-- > 0; rnd >>= Byte.SIZE) |
182 |
< |
bytes[i++] = (byte)rnd; |
178 |
> |
for (int i = 0, len = bytes.length; i < len; ) |
179 |
> |
for (int rnd = nextInt(), |
180 |
> |
n = Math.min(len - i, Integer.SIZE/Byte.SIZE); |
181 |
> |
n-- > 0; rnd >>= Byte.SIZE) |
182 |
> |
bytes[i++] = (byte)rnd; |
183 |
|
} |
184 |
|
|
185 |
|
/** |
201 |
|
* value from this random number generator's sequence |
202 |
|
*/ |
203 |
|
public int nextInt() { |
204 |
< |
return next(32); |
204 |
> |
return next(32); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
252 |
|
* successive calls to this method if n is a small power of two. |
253 |
|
* |
254 |
|
* @param n the bound on the random number to be returned. Must be |
255 |
< |
* positive. |
255 |
> |
* positive. |
256 |
|
* @return the next pseudorandom, uniformly distributed {@code int} |
257 |
|
* value between {@code 0} (inclusive) and {@code n} (exclusive) |
258 |
|
* from this random number generator's sequence |
316 |
|
* |
317 |
|
* @return the next pseudorandom, uniformly distributed |
318 |
|
* {@code boolean} value from this random number generator's |
319 |
< |
* sequence |
319 |
> |
* sequence |
320 |
|
* @since 1.2 |
321 |
|
*/ |
322 |
|
public boolean nextBoolean() { |
323 |
< |
return next(1) != 0; |
323 |
> |
return next(1) != 0; |
324 |
|
} |
325 |
|
|
326 |
|
/** |
407 |
|
*/ |
408 |
|
public double nextDouble() { |
409 |
|
return (((long)(next(26)) << 27) + next(27)) |
410 |
< |
/ (double)(1L << 53); |
410 |
> |
/ (double)(1L << 53); |
411 |
|
} |
412 |
|
|
413 |
|
private double nextNextGaussian; |
461 |
|
synchronized public double nextGaussian() { |
462 |
|
// See Knuth, ACP, Section 3.4.1 Algorithm C. |
463 |
|
if (haveNextNextGaussian) { |
464 |
< |
haveNextNextGaussian = false; |
465 |
< |
return nextNextGaussian; |
466 |
< |
} else { |
464 |
> |
haveNextNextGaussian = false; |
465 |
> |
return nextNextGaussian; |
466 |
> |
} else { |
467 |
|
double v1, v2, s; |
468 |
< |
do { |
468 |
> |
do { |
469 |
|
v1 = 2 * nextDouble() - 1; // between -1 and 1 |
470 |
< |
v2 = 2 * nextDouble() - 1; // between -1 and 1 |
470 |
> |
v2 = 2 * nextDouble() - 1; // between -1 and 1 |
471 |
|
s = v1 * v1 + v2 * v2; |
472 |
< |
} while (s >= 1 || s == 0); |
473 |
< |
double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
474 |
< |
nextNextGaussian = v2 * multiplier; |
475 |
< |
haveNextNextGaussian = true; |
476 |
< |
return v1 * multiplier; |
472 |
> |
} while (s >= 1 || s == 0); |
473 |
> |
double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
474 |
> |
nextNextGaussian = v2 * multiplier; |
475 |
> |
haveNextNextGaussian = true; |
476 |
> |
return v1 * multiplier; |
477 |
|
} |
478 |
|
} |
479 |
|
|
480 |
|
/** |
481 |
|
* Serializable fields for Random. |
482 |
|
* |
483 |
< |
* @serialField seed long; |
483 |
> |
* @serialField seed long |
484 |
|
* seed for random computations |
485 |
< |
* @serialField nextNextGaussian double; |
485 |
> |
* @serialField nextNextGaussian double |
486 |
|
* next Gaussian to be returned |
487 |
|
* @serialField haveNextNextGaussian boolean |
488 |
|
* nextNextGaussian is valid |
502 |
|
|
503 |
|
ObjectInputStream.GetField fields = s.readFields(); |
504 |
|
|
505 |
< |
// The seed is read in as {@code long} for |
506 |
< |
// historical reasons, but it is converted to an AtomicLong. |
507 |
< |
long seedVal = (long) fields.get("seed", -1L); |
505 |
> |
// The seed is read in as {@code long} for |
506 |
> |
// historical reasons, but it is converted to an AtomicLong. |
507 |
> |
long seedVal = (long) fields.get("seed", -1L); |
508 |
|
if (seedVal < 0) |
509 |
|
throw new java.io.StreamCorruptedException( |
510 |
|
"Random: invalid seed"); |
511 |
< |
seed = new AtomicLong(seedVal); |
511 |
> |
resetSeed(seedVal); |
512 |
|
nextNextGaussian = fields.get("nextNextGaussian", 0.0); |
513 |
|
haveNextNextGaussian = fields.get("haveNextNextGaussian", false); |
514 |
|
} |
517 |
|
* Save the {@code Random} instance to a stream. |
518 |
|
*/ |
519 |
|
synchronized private void writeObject(ObjectOutputStream s) |
520 |
< |
throws IOException { |
520 |
> |
throws IOException { |
521 |
|
|
522 |
|
// set the values of the Serializable fields |
523 |
|
ObjectOutputStream.PutField fields = s.putFields(); |
524 |
|
|
525 |
< |
// The seed is serialized as a long for historical reasons. |
525 |
> |
// The seed is serialized as a long for historical reasons. |
526 |
|
fields.put("seed", seed.get()); |
527 |
|
fields.put("nextNextGaussian", nextNextGaussian); |
528 |
|
fields.put("haveNextNextGaussian", haveNextNextGaussian); |
531 |
|
s.writeFields(); |
532 |
|
} |
533 |
|
|
534 |
+ |
// Support for resetting seed while deserializing |
535 |
+ |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
536 |
+ |
private static final long seedOffset; |
537 |
+ |
static { |
538 |
+ |
try { |
539 |
+ |
seedOffset = unsafe.objectFieldOffset |
540 |
+ |
(Random.class.getDeclaredField("seed")); |
541 |
+ |
} catch (Exception ex) { throw new Error(ex); } |
542 |
+ |
} |
543 |
+ |
private void resetSeed(long seedVal) { |
544 |
+ |
unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal)); |
545 |
+ |
} |
546 |
|
} |