1 |
|
/* |
2 |
< |
* Copyright 1995-2007 Sun Microsystems, Inc. All Rights Reserved. |
2 |
> |
* Copyright 1995-2008 Sun Microsystems, Inc. All Rights Reserved. |
3 |
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
|
* This code is free software; you can redistribute it and/or modify it |
32 |
|
* An instance of this class is used to generate a stream of |
33 |
|
* pseudorandom numbers. The class uses a 48-bit seed, which is |
34 |
|
* modified using a linear congruential formula. (See Donald Knuth, |
35 |
< |
* <i>The Art of Computer Programming, Volume 3</i>, Section 3.2.1.) |
35 |
> |
* <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.) |
36 |
|
* <p> |
37 |
|
* If two instances of {@code Random} are created with the same |
38 |
|
* seed, and the same sequence of method calls is made for each, they |
51 |
|
* Many applications will find the method {@link Math#random} simpler to use. |
52 |
|
* |
53 |
|
* @author Frank Yellin |
54 |
– |
* @version %I%, %G% |
54 |
|
* @since 1.0 |
55 |
|
*/ |
56 |
|
public |
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 |
|
|
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 = fields.get("seed", -1L); |
508 |
|
if (seedVal < 0) |
509 |
|
throw new java.io.StreamCorruptedException( |
510 |
|
"Random: invalid seed"); |
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); |
538 |
|
try { |
539 |
|
seedOffset = unsafe.objectFieldOffset |
540 |
|
(Random.class.getDeclaredField("seed")); |
541 |
< |
} catch (Exception ex) { throw new Error(ex); } |
541 |
> |
} catch (Exception ex) { throw new Error(ex); } |
542 |
|
} |
543 |
|
private void resetSeed(long seedVal) { |
544 |
|
unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal)); |