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