author | jlaskey |
Thu, 27 Jun 2019 18:30:27 -0300 | |
branch | JDK-8193209-branch |
changeset 57437 | f02ffcb61dce |
parent 57388 | b1e6bc96af3d |
child 57547 | 56cbdc3ea079 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
2 |
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2 | 4 |
* |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
15 |
* accompanied this code). |
2 | 16 |
* |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2 | 20 |
* |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
23 |
* questions. |
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
57388 | 27 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
28 |
import jdk.internal.misc.Unsafe; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
29 |
|
2 | 30 |
import java.io.*; |
57388 | 31 |
import java.math.BigInteger; |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
32 |
import java.util.Objects; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
33 |
import java.util.ServiceLoader; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
34 |
import java.util.ServiceLoader.Provider; |
2 | 35 |
import java.util.concurrent.atomic.AtomicLong; |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
36 |
import java.util.function.Function; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
37 |
import java.util.random.AbstractSharedRNG; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
38 |
import java.util.random.RandomNumberGenerator; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
39 |
import java.util.stream.Collectors; |
2 | 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, |
|
1818 | 45 |
* <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.) |
2 | 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 |
* |
|
4110 | 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 |
* |
|
2 | 75 |
* @author Frank Yellin |
76 |
* @since 1.0 |
|
77 |
*/ |
|
78 |
public |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
79 |
class Random extends AbstractSharedRNG implements java.io.Serializable { |
2 | 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 |
||
7518 | 90 |
private static final long multiplier = 0x5DEECE66DL; |
91 |
private static final long addend = 0xBL; |
|
92 |
private static final long mask = (1L << 48) - 1; |
|
2 | 93 |
|
20540 | 94 |
private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53) |
19609
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
95 |
|
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
96 |
// IllegalArgumentException messages |
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
97 |
static final String BadBound = "bound must be positive"; |
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
98 |
static final String BadRange = "bound must be greater than origin"; |
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
99 |
static final String BadSize = "size must be non-negative"; |
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
100 |
|
2 | 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 |
*/ |
|
5468
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
106 |
public Random() { |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
107 |
this(seedUniquifier() ^ System.nanoTime()); |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
108 |
} |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
109 |
|
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
110 |
private static long seedUniquifier() { |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
111 |
// L'Ecuyer, "Tables of Linear Congruential Generators of |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
112 |
// Different Sizes and Good Lattice Structure", 1999 |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
113 |
for (;;) { |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
114 |
long current = seedUniquifier.get(); |
49954 | 115 |
long next = current * 1181783497276652981L; |
5468
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
116 |
if (seedUniquifier.compareAndSet(current, next)) |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
117 |
return next; |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
118 |
} |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
119 |
} |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
120 |
|
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
121 |
private static final AtomicLong seedUniquifier |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
122 |
= new AtomicLong(8682522807148012L); |
2 | 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) { |
|
10056
7464dc04ae22
7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents:
7668
diff
changeset
|
138 |
if (getClass() == Random.class) |
7464dc04ae22
7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents:
7668
diff
changeset
|
139 |
this.seed = new AtomicLong(initialScramble(seed)); |
7464dc04ae22
7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents:
7668
diff
changeset
|
140 |
else { |
51759
ac6e9a2ebc04
8210786: Typo s/overriden/overridden/ in several places
igerasim
parents:
49954
diff
changeset
|
141 |
// subclass might have overridden setSeed |
10056
7464dc04ae22
7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents:
7668
diff
changeset
|
142 |
this.seed = new AtomicLong(); |
7464dc04ae22
7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents:
7668
diff
changeset
|
143 |
setSeed(seed); |
7464dc04ae22
7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents:
7668
diff
changeset
|
144 |
} |
5468
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
145 |
} |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
146 |
|
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
147 |
private static long initialScramble(long seed) { |
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
148 |
return (seed ^ multiplier) & mask; |
2 | 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 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
170 |
public synchronized void setSeed(long seed) { |
5468
c9aa7dfb4f78
6937857: Concurrent calls to new Random() not random enough
martin
parents:
4110
diff
changeset
|
171 |
this.seed.set(initialScramble(seed)); |
2 | 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 |
|
43003
b1c1a42602d6
8164923: Error in the documentation for java.lang.Random
rriggs
parents:
33674
diff
changeset
|
192 |
* <i>The Art of Computer Programming,</i> Volume 2: |
2 | 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 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
210 |
/* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
211 |
* Period of Random is 2**48 |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
212 |
*/ |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
213 |
private static final BigInteger PERIOD = BigInteger.valueOf(1L<<48); |
57388 | 214 |
|
215 |
/** |
|
216 |
* Returns the period of this random number generator. |
|
217 |
* |
|
218 |
* @return the period of this random number generator. |
|
219 |
*/ |
|
220 |
public BigInteger period() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
221 |
return PERIOD; |
57388 | 222 |
} |
223 |
||
2 | 224 |
/** |
225 |
* Generates random bytes and places them into a user-supplied |
|
226 |
* byte array. The number of random bytes produced is equal to |
|
227 |
* the length of the byte array. |
|
228 |
* |
|
229 |
* <p>The method {@code nextBytes} is implemented by class {@code Random} |
|
230 |
* as if by: |
|
231 |
* <pre> {@code |
|
232 |
* public void nextBytes(byte[] bytes) { |
|
233 |
* for (int i = 0; i < bytes.length; ) |
|
234 |
* for (int rnd = nextInt(), n = Math.min(bytes.length - i, 4); |
|
235 |
* n-- > 0; rnd >>= 8) |
|
236 |
* bytes[i++] = (byte)rnd; |
|
237 |
* }}</pre> |
|
238 |
* |
|
239 |
* @param bytes the byte array to fill with random bytes |
|
240 |
* @throws NullPointerException if the byte array is null |
|
241 |
* @since 1.1 |
|
242 |
*/ |
|
243 |
public void nextBytes(byte[] bytes) { |
|
244 |
for (int i = 0, len = bytes.length; i < len; ) |
|
245 |
for (int rnd = nextInt(), |
|
246 |
n = Math.min(len - i, Integer.SIZE/Byte.SIZE); |
|
247 |
n-- > 0; rnd >>= Byte.SIZE) |
|
248 |
bytes[i++] = (byte)rnd; |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Returns the next pseudorandom, uniformly distributed {@code int} |
|
253 |
* value from this random number generator's sequence. The general |
|
254 |
* contract of {@code nextInt} is that one {@code int} value is |
|
19074
84a8d23e8f32
8020539: Clean up doclint problems in java.util package, part 2
bpb
parents:
18156
diff
changeset
|
255 |
* pseudorandomly generated and returned. All 2<sup>32</sup> possible |
84a8d23e8f32
8020539: Clean up doclint problems in java.util package, part 2
bpb
parents:
18156
diff
changeset
|
256 |
* {@code int} values are produced with (approximately) equal probability. |
2 | 257 |
* |
258 |
* <p>The method {@code nextInt} is implemented by class {@code Random} |
|
259 |
* as if by: |
|
260 |
* <pre> {@code |
|
261 |
* public int nextInt() { |
|
262 |
* return next(32); |
|
263 |
* }}</pre> |
|
264 |
* |
|
265 |
* @return the next pseudorandom, uniformly distributed {@code int} |
|
266 |
* value from this random number generator's sequence |
|
267 |
*/ |
|
268 |
public int nextInt() { |
|
269 |
return next(32); |
|
270 |
} |
|
271 |
||
272 |
/** |
|
57388 | 273 |
* Returns a pseudorandom {@code int} value between zero (inclusive) |
274 |
* and the specified bound (exclusive). |
|
2 | 275 |
* |
19609
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
276 |
* @param bound the upper bound (exclusive). Must be positive. |
57388 | 277 |
* @return a pseudorandom {@code int} value between zero |
278 |
* (inclusive) and the bound (exclusive) |
|
279 |
* @throws IllegalArgumentException if {@code bound} is not positive |
|
2 | 280 |
*/ |
19609
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
281 |
public int nextInt(int bound) { |
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
282 |
if (bound <= 0) |
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
283 |
throw new IllegalArgumentException(BadBound); |
57388 | 284 |
// Specialize internalNextInt for origin 0 |
285 |
int r = nextInt(); |
|
19609
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
286 |
int m = bound - 1; |
57388 | 287 |
if ((bound & m) == 0) // power of two |
288 |
r &= m; |
|
289 |
else { // reject over-represented candidates |
|
290 |
for (int u = r >>> 1; |
|
291 |
u + m - (r = u % bound) < 0; |
|
292 |
u = nextInt() >>> 1) |
|
19609
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
293 |
; |
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
294 |
} |
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
295 |
return r; |
2 | 296 |
} |
297 |
||
298 |
/** |
|
299 |
* Returns the next pseudorandom, uniformly distributed {@code long} |
|
300 |
* value from this random number generator's sequence. The general |
|
301 |
* contract of {@code nextLong} is that one {@code long} value is |
|
302 |
* pseudorandomly generated and returned. |
|
303 |
* |
|
304 |
* <p>The method {@code nextLong} is implemented by class {@code Random} |
|
305 |
* as if by: |
|
306 |
* <pre> {@code |
|
307 |
* public long nextLong() { |
|
308 |
* return ((long)next(32) << 32) + next(32); |
|
309 |
* }}</pre> |
|
310 |
* |
|
311 |
* Because class {@code Random} uses a seed with only 48 bits, |
|
312 |
* this algorithm will not return all possible {@code long} values. |
|
313 |
* |
|
314 |
* @return the next pseudorandom, uniformly distributed {@code long} |
|
315 |
* value from this random number generator's sequence |
|
316 |
*/ |
|
317 |
public long nextLong() { |
|
318 |
// it's okay that the bottom word remains signed. |
|
319 |
return ((long)(next(32)) << 32) + next(32); |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* Returns the next pseudorandom, uniformly distributed |
|
324 |
* {@code boolean} value from this random number generator's |
|
325 |
* sequence. The general contract of {@code nextBoolean} is that one |
|
326 |
* {@code boolean} value is pseudorandomly generated and returned. The |
|
327 |
* values {@code true} and {@code false} are produced with |
|
328 |
* (approximately) equal probability. |
|
329 |
* |
|
330 |
* <p>The method {@code nextBoolean} is implemented by class {@code Random} |
|
331 |
* as if by: |
|
332 |
* <pre> {@code |
|
333 |
* public boolean nextBoolean() { |
|
334 |
* return next(1) != 0; |
|
335 |
* }}</pre> |
|
336 |
* |
|
337 |
* @return the next pseudorandom, uniformly distributed |
|
338 |
* {@code boolean} value from this random number generator's |
|
339 |
* sequence |
|
340 |
* @since 1.2 |
|
341 |
*/ |
|
342 |
public boolean nextBoolean() { |
|
343 |
return next(1) != 0; |
|
344 |
} |
|
345 |
||
346 |
/** |
|
347 |
* Returns the next pseudorandom, uniformly distributed {@code float} |
|
348 |
* value between {@code 0.0} and {@code 1.0} from this random |
|
349 |
* number generator's sequence. |
|
350 |
* |
|
351 |
* <p>The general contract of {@code nextFloat} is that one |
|
352 |
* {@code float} value, chosen (approximately) uniformly from the |
|
353 |
* range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is |
|
19074
84a8d23e8f32
8020539: Clean up doclint problems in java.util package, part 2
bpb
parents:
18156
diff
changeset
|
354 |
* pseudorandomly generated and returned. All 2<sup>24</sup> possible |
84a8d23e8f32
8020539: Clean up doclint problems in java.util package, part 2
bpb
parents:
18156
diff
changeset
|
355 |
* {@code float} values of the form <i>m x </i>2<sup>-24</sup>, |
84a8d23e8f32
8020539: Clean up doclint problems in java.util package, part 2
bpb
parents:
18156
diff
changeset
|
356 |
* where <i>m</i> is a positive integer less than 2<sup>24</sup>, are |
2 | 357 |
* produced with (approximately) equal probability. |
358 |
* |
|
359 |
* <p>The method {@code nextFloat} is implemented by class {@code Random} |
|
360 |
* as if by: |
|
361 |
* <pre> {@code |
|
362 |
* public float nextFloat() { |
|
363 |
* return next(24) / ((float)(1 << 24)); |
|
364 |
* }}</pre> |
|
365 |
* |
|
366 |
* <p>The hedge "approximately" is used in the foregoing description only |
|
367 |
* because the next method is only approximately an unbiased source of |
|
368 |
* independently chosen bits. If it were a perfect source of randomly |
|
369 |
* chosen bits, then the algorithm shown would choose {@code float} |
|
370 |
* values from the stated range with perfect uniformity.<p> |
|
371 |
* [In early versions of Java, the result was incorrectly calculated as: |
|
372 |
* <pre> {@code |
|
373 |
* return next(30) / ((float)(1 << 30));}</pre> |
|
374 |
* This might seem to be equivalent, if not better, but in fact it |
|
375 |
* introduced a slight nonuniformity because of the bias in the rounding |
|
376 |
* of floating-point numbers: it was slightly more likely that the |
|
377 |
* low-order bit of the significand would be 0 than that it would be 1.] |
|
378 |
* |
|
379 |
* @return the next pseudorandom, uniformly distributed {@code float} |
|
380 |
* value between {@code 0.0} and {@code 1.0} from this |
|
381 |
* random number generator's sequence |
|
382 |
*/ |
|
383 |
public float nextFloat() { |
|
384 |
return next(24) / ((float)(1 << 24)); |
|
385 |
} |
|
386 |
||
387 |
/** |
|
388 |
* Returns the next pseudorandom, uniformly distributed |
|
389 |
* {@code double} value between {@code 0.0} and |
|
390 |
* {@code 1.0} from this random number generator's sequence. |
|
391 |
* |
|
392 |
* <p>The general contract of {@code nextDouble} is that one |
|
393 |
* {@code double} value, chosen (approximately) uniformly from the |
|
394 |
* range {@code 0.0d} (inclusive) to {@code 1.0d} (exclusive), is |
|
395 |
* pseudorandomly generated and returned. |
|
396 |
* |
|
397 |
* <p>The method {@code nextDouble} is implemented by class {@code Random} |
|
398 |
* as if by: |
|
399 |
* <pre> {@code |
|
400 |
* public double nextDouble() { |
|
401 |
* return (((long)next(26) << 27) + next(27)) |
|
402 |
* / (double)(1L << 53); |
|
403 |
* }}</pre> |
|
404 |
* |
|
405 |
* <p>The hedge "approximately" is used in the foregoing description only |
|
406 |
* because the {@code next} method is only approximately an unbiased |
|
407 |
* source of independently chosen bits. If it were a perfect source of |
|
408 |
* randomly chosen bits, then the algorithm shown would choose |
|
409 |
* {@code double} values from the stated range with perfect uniformity. |
|
410 |
* <p>[In early versions of Java, the result was incorrectly calculated as: |
|
411 |
* <pre> {@code |
|
412 |
* return (((long)next(27) << 27) + next(27)) |
|
413 |
* / (double)(1L << 54);}</pre> |
|
414 |
* This might seem to be equivalent, if not better, but in fact it |
|
415 |
* introduced a large nonuniformity because of the bias in the rounding |
|
416 |
* of floating-point numbers: it was three times as likely that the |
|
417 |
* low-order bit of the significand would be 0 than that it would be 1! |
|
418 |
* This nonuniformity probably doesn't matter much in practice, but we |
|
419 |
* strive for perfection.] |
|
420 |
* |
|
421 |
* @return the next pseudorandom, uniformly distributed {@code double} |
|
422 |
* value between {@code 0.0} and {@code 1.0} from this |
|
423 |
* random number generator's sequence |
|
424 |
* @see Math#random |
|
425 |
*/ |
|
426 |
public double nextDouble() { |
|
19609
108f52a7438f
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents:
19074
diff
changeset
|
427 |
return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT; |
2 | 428 |
} |
429 |
||
430 |
private double nextNextGaussian; |
|
431 |
private boolean haveNextNextGaussian = false; |
|
432 |
||
433 |
/** |
|
434 |
* Returns the next pseudorandom, Gaussian ("normally") distributed |
|
435 |
* {@code double} value with mean {@code 0.0} and standard |
|
436 |
* deviation {@code 1.0} from this random number generator's sequence. |
|
437 |
* <p> |
|
438 |
* The general contract of {@code nextGaussian} is that one |
|
439 |
* {@code double} value, chosen from (approximately) the usual |
|
440 |
* normal distribution with mean {@code 0.0} and standard deviation |
|
441 |
* {@code 1.0}, is pseudorandomly generated and returned. |
|
442 |
* |
|
443 |
* <p>The method {@code nextGaussian} is implemented by class |
|
444 |
* {@code Random} as if by a threadsafe version of the following: |
|
445 |
* <pre> {@code |
|
446 |
* private double nextNextGaussian; |
|
447 |
* private boolean haveNextNextGaussian = false; |
|
448 |
* |
|
449 |
* public double nextGaussian() { |
|
450 |
* if (haveNextNextGaussian) { |
|
451 |
* haveNextNextGaussian = false; |
|
452 |
* return nextNextGaussian; |
|
453 |
* } else { |
|
454 |
* double v1, v2, s; |
|
455 |
* do { |
|
456 |
* v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0 |
|
457 |
* v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0 |
|
458 |
* s = v1 * v1 + v2 * v2; |
|
459 |
* } while (s >= 1 || s == 0); |
|
460 |
* double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
|
461 |
* nextNextGaussian = v2 * multiplier; |
|
462 |
* haveNextNextGaussian = true; |
|
463 |
* return v1 * multiplier; |
|
464 |
* } |
|
465 |
* }}</pre> |
|
466 |
* This uses the <i>polar method</i> of G. E. P. Box, M. E. Muller, and |
|
467 |
* G. Marsaglia, as described by Donald E. Knuth in <i>The Art of |
|
43003
b1c1a42602d6
8164923: Error in the documentation for java.lang.Random
rriggs
parents:
33674
diff
changeset
|
468 |
* Computer Programming</i>, Volume 2: <i>Seminumerical Algorithms</i>, |
2 | 469 |
* section 3.4.1, subsection C, algorithm P. Note that it generates two |
470 |
* independent values at the cost of only one call to {@code StrictMath.log} |
|
471 |
* and one call to {@code StrictMath.sqrt}. |
|
472 |
* |
|
473 |
* @return the next pseudorandom, Gaussian ("normally") distributed |
|
474 |
* {@code double} value with mean {@code 0.0} and |
|
475 |
* standard deviation {@code 1.0} from this random number |
|
476 |
* generator's sequence |
|
477 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
478 |
public synchronized double nextGaussian() { |
2 | 479 |
// See Knuth, ACP, Section 3.4.1 Algorithm C. |
480 |
if (haveNextNextGaussian) { |
|
481 |
haveNextNextGaussian = false; |
|
482 |
return nextNextGaussian; |
|
483 |
} else { |
|
484 |
double v1, v2, s; |
|
485 |
do { |
|
486 |
v1 = 2 * nextDouble() - 1; // between -1 and 1 |
|
487 |
v2 = 2 * nextDouble() - 1; // between -1 and 1 |
|
488 |
s = v1 * v1 + v2 * v2; |
|
489 |
} while (s >= 1 || s == 0); |
|
490 |
double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
|
491 |
nextNextGaussian = v2 * multiplier; |
|
492 |
haveNextNextGaussian = true; |
|
493 |
return v1 * multiplier; |
|
494 |
} |
|
495 |
} |
|
496 |
||
17421
f3fbcfe6e2cf
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
14342
diff
changeset
|
497 |
/** |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
498 |
* Creates a new random number generator that uses the random number generator algorithm |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
499 |
* specified by name. The seed of the random number generator to a value very likely to be |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
500 |
* distinct from any other invocation. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
501 |
* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
502 |
* @param name name of random number generator algorithm to use. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
503 |
* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
504 |
* @return an instance of random number generator. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
505 |
* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
506 |
* @throws IllegalArgumentException if {@code name} is an unknown random number generator |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
507 |
* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
508 |
* @since 14 |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
509 |
*/ |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
510 |
public static RandomNumberGenerator byName(String name) throws IllegalArgumentException { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
511 |
Objects.requireNonNull(name); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
512 |
Map<String, Provider<RandomNumberGenerator>> rngs = getRNGMap(); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
513 |
Provider<RandomNumberGenerator> provider = rngs.get(name.toUpperCase()); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
514 |
if (provider == null) { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
515 |
throw new IllegalArgumentException(name + " is an unknown random number generator"); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
516 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
517 |
return provider.get(); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
518 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
519 |
|
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
520 |
private static Map<String, Provider<RandomNumberGenerator>> rngMap; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
521 |
|
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
522 |
private static Map<String, Provider<RandomNumberGenerator>> getRNGMap() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
523 |
if (rngMap == null) { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
524 |
synchronized (Random.class) { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
525 |
if (rngMap == null) { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
526 |
rngMap = ServiceLoader |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
527 |
.load(RandomNumberGenerator.class) |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
528 |
.stream() |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
529 |
.filter(p -> !p.type().isInterface()) |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
530 |
.collect(Collectors.toMap(p -> p.type().getSimpleName().toUpperCase(), |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
531 |
Function.identity())); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
532 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
533 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
534 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
535 |
return rngMap; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
536 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
537 |
|
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57388
diff
changeset
|
538 |
/** |
2 | 539 |
* Serializable fields for Random. |
540 |
* |
|
541 |
* @serialField seed long |
|
542 |
* seed for random computations |
|
543 |
* @serialField nextNextGaussian double |
|
544 |
* next Gaussian to be returned |
|
545 |
* @serialField haveNextNextGaussian boolean |
|
546 |
* nextNextGaussian is valid |
|
547 |
*/ |
|
548 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
549 |
new ObjectStreamField("seed", Long.TYPE), |
|
550 |
new ObjectStreamField("nextNextGaussian", Double.TYPE), |
|
551 |
new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE) |
|
552 |
}; |
|
553 |
||
554 |
/** |
|
555 |
* Reconstitute the {@code Random} instance from a stream (that is, |
|
556 |
* deserialize it). |
|
557 |
*/ |
|
558 |
private void readObject(java.io.ObjectInputStream s) |
|
559 |
throws java.io.IOException, ClassNotFoundException { |
|
560 |
||
561 |
ObjectInputStream.GetField fields = s.readFields(); |
|
562 |
||
563 |
// The seed is read in as {@code long} for |
|
564 |
// historical reasons, but it is converted to an AtomicLong. |
|
51 | 565 |
long seedVal = fields.get("seed", -1L); |
2 | 566 |
if (seedVal < 0) |
567 |
throw new java.io.StreamCorruptedException( |
|
568 |
"Random: invalid seed"); |
|
569 |
resetSeed(seedVal); |
|
570 |
nextNextGaussian = fields.get("nextNextGaussian", 0.0); |
|
571 |
haveNextNextGaussian = fields.get("haveNextNextGaussian", false); |
|
572 |
} |
|
573 |
||
574 |
/** |
|
575 |
* Save the {@code Random} instance to a stream. |
|
576 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
577 |
private synchronized void writeObject(ObjectOutputStream s) |
2 | 578 |
throws IOException { |
579 |
||
580 |
// set the values of the Serializable fields |
|
581 |
ObjectOutputStream.PutField fields = s.putFields(); |
|
582 |
||
583 |
// The seed is serialized as a long for historical reasons. |
|
584 |
fields.put("seed", seed.get()); |
|
585 |
fields.put("nextNextGaussian", nextNextGaussian); |
|
586 |
fields.put("haveNextNextGaussian", haveNextNextGaussian); |
|
587 |
||
588 |
// save them |
|
589 |
s.writeFields(); |
|
590 |
} |
|
591 |
||
592 |
// Support for resetting seed while deserializing |
|
593 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
|
594 |
private static final long seedOffset; |
|
595 |
static { |
|
596 |
try { |
|
597 |
seedOffset = unsafe.objectFieldOffset |
|
598 |
(Random.class.getDeclaredField("seed")); |
|
599 |
} catch (Exception ex) { throw new Error(ex); } |
|
600 |
} |
|
601 |
private void resetSeed(long seedVal) { |
|
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
51759
diff
changeset
|
602 |
unsafe.putReferenceVolatile(this, seedOffset, new AtomicLong(seedVal)); |
2 | 603 |
} |
604 |
} |