1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* @(#)Random.java 1.44 05/02/01 |
3 |
|
* |
4 |
|
* Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
33 |
|
* class <code>Math</code> simpler to use. |
34 |
|
* |
35 |
|
* @author Frank Yellin |
36 |
< |
* @version %I%, %G% |
36 |
> |
* @version 1.44, 02/01/05 |
37 |
|
* @see java.lang.Math#random() |
38 |
|
* @since JDK1.0 |
39 |
|
*/ |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
< |
* Sets the seed of this random number generator using a single |
84 |
< |
* <code>long</code> seed. The general contract of <tt>setSeed</tt> |
85 |
< |
* is that it alters the state of this random number generator |
86 |
< |
* object so as to be in exactly the same state as if it had just |
87 |
< |
* been created with the argument <tt>seed</tt> as a seed. The method |
88 |
< |
* <tt>setSeed</tt> is implemented by class Random as follows: |
89 |
< |
* <blockquote><pre> |
90 |
< |
* synchronized public void setSeed(long seed) { |
91 |
< |
* this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1); |
92 |
< |
* haveNextNextGaussian = false; |
93 |
< |
* }</pre></blockquote> |
94 |
< |
* The implementation of <tt>setSeed</tt> by class <tt>Random</tt> |
95 |
< |
* happens to use only 48 bits of the given seed. In general, however, |
96 |
< |
* an overriding method may use all 64 bits of the long argument |
97 |
< |
* as a seed value. |
98 |
< |
* |
99 |
< |
* Note: Although the seed value is an AtomicLong, this method |
100 |
< |
* must still be synchronized to ensure correct semantics |
101 |
< |
* of haveNextNextGaussian. |
83 |
> |
* Sets the seed of this random number generator using a single |
84 |
> |
* <code>long</code> seed. The general contract of |
85 |
> |
* <tt>setSeed</tt> is that it alters the state of this random |
86 |
> |
* number generator object so as to be in exactly the same state |
87 |
> |
* as if it had just been created with the argument <tt>seed</tt> |
88 |
> |
* as a seed. The method <tt>setSeed</tt> is implemented by class |
89 |
> |
* Random using a thread-safe update of the seed to <code> (seed * |
90 |
> |
* 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)</code> and clearing the |
91 |
> |
* <code>haveNextNextGaussian</code> flag used by {@link |
92 |
> |
* #nextGaussian}. The implementation of <tt>setSeed</tt> by class |
93 |
> |
* <tt>Random</tt> happens to use only 48 bits of the given |
94 |
> |
* seed. In general, however, an overriding method may use all 64 |
95 |
> |
* bits of the long argument as a seed value. |
96 |
|
* |
97 |
|
* @param seed the initial seed. |
98 |
|
*/ |
104 |
|
|
105 |
|
/** |
106 |
|
* Generates the next pseudorandom number. Subclass should |
107 |
< |
* override this, as this is used by all other methods.<p> |
108 |
< |
* The general contract of <tt>next</tt> is that it returns an |
109 |
< |
* <tt>int</tt> value and if the argument bits is between <tt>1</tt> |
110 |
< |
* and <tt>32</tt> (inclusive), then that many low-order bits of the |
111 |
< |
* returned value will be (approximately) independently chosen bit |
112 |
< |
* values, each of which is (approximately) equally likely to be |
113 |
< |
* <tt>0</tt> or <tt>1</tt>. The method <tt>next</tt> is implemented |
114 |
< |
* by class <tt>Random</tt> as follows: |
115 |
< |
* <blockquote><pre> |
116 |
< |
* synchronized protected int next(int bits) { |
117 |
< |
* seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); |
118 |
< |
* return (int)(seed >>> (48 - bits)); |
119 |
< |
* }</pre></blockquote> |
120 |
< |
* This is a linear congruential pseudorandom number generator, as |
127 |
< |
* defined by D. H. Lehmer and described by Donald E. Knuth in <i>The |
128 |
< |
* Art of Computer Programming,</i> Volume 2: <i>Seminumerical |
107 |
> |
* override this, as this is used by all other methods.<p> The |
108 |
> |
* general contract of <tt>next</tt> is that it returns an |
109 |
> |
* <tt>int</tt> value and if the argument bits is between |
110 |
> |
* <tt>1</tt> and <tt>32</tt> (inclusive), then that many |
111 |
> |
* low-order bits of the returned value will be (approximately) |
112 |
> |
* independently chosen bit values, each of which is |
113 |
> |
* (approximately) equally likely to be <tt>0</tt> or |
114 |
> |
* <tt>1</tt>. The method <tt>next</tt> is implemented by class |
115 |
> |
* <tt>Random</tt> using a thread-safe update of the seed to <code> |
116 |
> |
* (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)</code> and |
117 |
> |
* returning <code>(int)(seed >>> (48 - bits))</code>. This is a |
118 |
> |
* linear congruential pseudorandom number generator, as defined |
119 |
> |
* by D. H. Lehmer and described by Donald E. Knuth in <i>The Art |
120 |
> |
* of Computer Programming,</i> Volume 2: <i>Seminumerical |
121 |
|
* Algorithms</i>, section 3.2.1. |
122 |
|
* |
123 |
|
* @param bits random bits |
128 |
|
long oldseed, nextseed; |
129 |
|
AtomicLong seed = this.seed; |
130 |
|
do { |
131 |
< |
oldseed = seed.get(); |
132 |
< |
nextseed = (oldseed * multiplier + addend) & mask; |
131 |
> |
oldseed = seed.get(); |
132 |
> |
nextseed = (oldseed * multiplier + addend) & mask; |
133 |
|
} while (!seed.compareAndSet(oldseed, nextseed)); |
134 |
|
return (int)(nextseed >>> (48 - bits)); |
135 |
|
} |
385 |
|
* <tt>double</tt> value, chosen from (approximately) the usual |
386 |
|
* normal distribution with mean <tt>0.0</tt> and standard deviation |
387 |
|
* <tt>1.0</tt>, is pseudorandomly generated and returned. The method |
388 |
< |
* <tt>nextGaussian</tt> is implemented by class <tt>Random</tt> as follows: |
388 |
> |
* <tt>nextGaussian</tt> is implemented by class <tt>Random</tt> as if |
389 |
> |
* by a threadsafe version of the following: |
390 |
|
* <blockquote><pre> |
391 |
< |
* synchronized public double nextGaussian() { |
391 |
> |
* public double nextGaussian() { |
392 |
|
* if (haveNextNextGaussian) { |
393 |
|
* haveNextNextGaussian = false; |
394 |
|
* return nextNextGaussian; |
399 |
|
* v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0 |
400 |
|
* s = v1 * v1 + v2 * v2; |
401 |
|
* } while (s >= 1 || s == 0); |
402 |
< |
* double multiplier = Math.sqrt(-2 * Math.log(s)/s); |
402 |
> |
* double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
403 |
|
* nextNextGaussian = v2 * multiplier; |
404 |
|
* haveNextNextGaussian = true; |
405 |
|
* return v1 * multiplier; |
409 |
|
* G. Marsaglia, as described by Donald E. Knuth in <i>The Art of |
410 |
|
* Computer Programming</i>, Volume 2: <i>Seminumerical Algorithms</i>, |
411 |
|
* section 3.4.1, subsection C, algorithm P. Note that it generates two |
412 |
< |
* independent values at the cost of only one call to <tt>Math.log</tt> |
413 |
< |
* and one call to <tt>Math.sqrt</tt>. |
412 |
> |
* independent values at the cost of only one call to <tt>StrictMath.log</tt> |
413 |
> |
* and one call to <tt>StrictMath.sqrt</tt>. |
414 |
|
* |
415 |
|
* @return the next pseudorandom, Gaussian ("normally") distributed |
416 |
|
* <code>double</code> value with mean <code>0.0</code> and |
429 |
|
v2 = 2 * nextDouble() - 1; // between -1 and 1 |
430 |
|
s = v1 * v1 + v2 * v2; |
431 |
|
} while (s >= 1 || s == 0); |
432 |
< |
double multiplier = Math.sqrt(-2 * Math.log(s)/s); |
432 |
> |
double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
433 |
|
nextNextGaussian = v2 * multiplier; |
434 |
|
haveNextNextGaussian = true; |
435 |
|
return v1 * multiplier; |