1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
import java.io.ObjectStreamField; |
10 |
import java.util.Random; |
11 |
import java.util.Spliterator; |
12 |
import java.util.concurrent.atomic.AtomicInteger; |
13 |
import java.util.concurrent.atomic.AtomicLong; |
14 |
import java.util.function.DoubleConsumer; |
15 |
import java.util.function.IntConsumer; |
16 |
import java.util.function.LongConsumer; |
17 |
import java.util.stream.DoubleStream; |
18 |
import java.util.stream.IntStream; |
19 |
import java.util.stream.LongStream; |
20 |
import java.util.stream.StreamSupport; |
21 |
|
22 |
/** |
23 |
* A random number generator isolated to the current thread. Like the |
24 |
* global {@link java.util.Random} generator used by the {@link |
25 |
* java.lang.Math} class, a {@code ThreadLocalRandom} is initialized |
26 |
* with an internally generated seed that may not otherwise be |
27 |
* modified. When applicable, use of {@code ThreadLocalRandom} rather |
28 |
* than shared {@code Random} objects in concurrent programs will |
29 |
* typically encounter much less overhead and contention. Use of |
30 |
* {@code ThreadLocalRandom} is particularly appropriate when multiple |
31 |
* tasks (for example, each a {@link ForkJoinTask}) use random numbers |
32 |
* in parallel in thread pools. |
33 |
* |
34 |
* <p>Usages of this class should typically be of the form: |
35 |
* {@code ThreadLocalRandom.current().nextX(...)} (where |
36 |
* {@code X} is {@code Int}, {@code Long}, etc). |
37 |
* When all usages are of this form, it is never possible to |
38 |
* accidently share a {@code ThreadLocalRandom} across multiple threads. |
39 |
* |
40 |
* <p>This class also provides additional commonly used bounded random |
41 |
* generation methods. |
42 |
* |
43 |
* <p>Instances of {@code ThreadLocalRandom} are not cryptographically |
44 |
* secure. Consider instead using {@link java.security.SecureRandom} |
45 |
* in security-sensitive applications. Additionally, |
46 |
* default-constructed instances do not use a cryptographically random |
47 |
* seed unless the {@linkplain System#getProperty system property} |
48 |
* {@code java.util.secureRandomSeed} is set to {@code true}. |
49 |
* |
50 |
* @since 1.7 |
51 |
* @author Doug Lea |
52 |
*/ |
53 |
public class ThreadLocalRandom extends Random { |
54 |
/* |
55 |
* This class implements the java.util.Random API (and subclasses |
56 |
* Random) using a single static instance that accesses random |
57 |
* number state held in class Thread (primarily, field |
58 |
* threadLocalRandomSeed). In doing so, it also provides a home |
59 |
* for managing package-private utilities that rely on exactly the |
60 |
* same state as needed to maintain the ThreadLocalRandom |
61 |
* instances. We leverage the need for an initialization flag |
62 |
* field to also use it as a "probe" -- a self-adjusting thread |
63 |
* hash used for contention avoidance, as well as a secondary |
64 |
* simpler (xorShift) random seed that is conservatively used to |
65 |
* avoid otherwise surprising users by hijacking the |
66 |
* ThreadLocalRandom sequence. The dual use is a marriage of |
67 |
* convenience, but is a simple and efficient way of reducing |
68 |
* application-level overhead and footprint of most concurrent |
69 |
* programs. |
70 |
* |
71 |
* Even though this class subclasses java.util.Random, it uses the |
72 |
* same basic algorithm as java.util.SplittableRandom. (See its |
73 |
* internal documentation for explanations, which are not repeated |
74 |
* here.) Because ThreadLocalRandoms are not splittable |
75 |
* though, we use only a single 64bit gamma. |
76 |
* |
77 |
* Because this class is in a different package than class Thread, |
78 |
* field access methods use Unsafe to bypass access control rules. |
79 |
* To conform to the requirements of the Random superclass |
80 |
* constructor, the common static ThreadLocalRandom maintains an |
81 |
* "initialized" field for the sake of rejecting user calls to |
82 |
* setSeed while still allowing a call from constructor. Note |
83 |
* that serialization is completely unnecessary because there is |
84 |
* only a static singleton. But we generate a serial form |
85 |
* containing "rnd" and "initialized" fields to ensure |
86 |
* compatibility across versions. |
87 |
* |
88 |
* Implementations of non-core methods are mostly the same as in |
89 |
* SplittableRandom, that were in part derived from a previous |
90 |
* version of this class. |
91 |
* |
92 |
* The nextLocalGaussian ThreadLocal supports the very rarely used |
93 |
* nextGaussian method by providing a holder for the second of a |
94 |
* pair of them. As is true for the base class version of this |
95 |
* method, this time/space tradeoff is probably never worthwhile, |
96 |
* but we provide identical statistical properties. |
97 |
*/ |
98 |
|
99 |
/** Generates per-thread initialization/probe field */ |
100 |
private static final AtomicInteger probeGenerator = new AtomicInteger(); |
101 |
|
102 |
/** |
103 |
* The next seed for default constructors. |
104 |
*/ |
105 |
private static final AtomicLong seeder = new AtomicLong(initialSeed()); |
106 |
|
107 |
private static long initialSeed() { |
108 |
java.security.PrivilegedAction<Boolean> action = |
109 |
() -> Boolean.getBoolean("java.util.secureRandomSeed"); |
110 |
if (java.security.AccessController.doPrivileged(action)) { |
111 |
byte[] seedBytes = java.security.SecureRandom.getSeed(8); |
112 |
long s = (long)seedBytes[0] & 0xffL; |
113 |
for (int i = 1; i < 8; ++i) |
114 |
s = (s << 8) | ((long)seedBytes[i] & 0xffL); |
115 |
return s; |
116 |
} |
117 |
return (mix64(System.currentTimeMillis()) ^ |
118 |
mix64(System.nanoTime())); |
119 |
} |
120 |
|
121 |
/** |
122 |
* The seed increment. |
123 |
*/ |
124 |
private static final long GAMMA = 0x9e3779b97f4a7c15L; |
125 |
|
126 |
/** |
127 |
* The increment for generating probe values. |
128 |
*/ |
129 |
private static final int PROBE_INCREMENT = 0x9e3779b9; |
130 |
|
131 |
/** |
132 |
* The increment of seeder per new instance. |
133 |
*/ |
134 |
private static final long SEEDER_INCREMENT = 0xbb67ae8584caa73bL; |
135 |
|
136 |
// Constants from SplittableRandom |
137 |
private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53) |
138 |
private static final float FLOAT_UNIT = 0x1.0p-24f; // 1.0f / (1 << 24) |
139 |
|
140 |
/** Rarely-used holder for the second of a pair of Gaussians */ |
141 |
private static final ThreadLocal<Double> nextLocalGaussian = |
142 |
new ThreadLocal<>(); |
143 |
|
144 |
private static long mix64(long z) { |
145 |
z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; |
146 |
z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L; |
147 |
return z ^ (z >>> 33); |
148 |
} |
149 |
|
150 |
private static int mix32(long z) { |
151 |
z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; |
152 |
return (int)(((z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L) >>> 32); |
153 |
} |
154 |
|
155 |
/** |
156 |
* Field used only during singleton initialization. |
157 |
* True when constructor completes. |
158 |
*/ |
159 |
boolean initialized; |
160 |
|
161 |
/** Constructor used only for static singleton */ |
162 |
private ThreadLocalRandom() { |
163 |
initialized = true; // false during super() call |
164 |
} |
165 |
|
166 |
/** The common ThreadLocalRandom */ |
167 |
static final ThreadLocalRandom instance = new ThreadLocalRandom(); |
168 |
|
169 |
/** |
170 |
* Initialize Thread fields for the current thread. Called only |
171 |
* when Thread.threadLocalRandomProbe is zero, indicating that a |
172 |
* thread local seed value needs to be generated. Note that even |
173 |
* though the initialization is purely thread-local, we need to |
174 |
* rely on (static) atomic generators to initialize the values. |
175 |
*/ |
176 |
static final void localInit() { |
177 |
int p = probeGenerator.addAndGet(PROBE_INCREMENT); |
178 |
int probe = (p == 0) ? 1 : p; // skip 0 |
179 |
long seed = mix64(seeder.getAndAdd(SEEDER_INCREMENT)); |
180 |
Thread t = Thread.currentThread(); |
181 |
U.putLong(t, SEED, seed); |
182 |
U.putInt(t, PROBE, probe); |
183 |
} |
184 |
|
185 |
/** |
186 |
* Returns the current thread's {@code ThreadLocalRandom}. |
187 |
* |
188 |
* @return the current thread's {@code ThreadLocalRandom} |
189 |
*/ |
190 |
public static ThreadLocalRandom current() { |
191 |
if (U.getInt(Thread.currentThread(), PROBE) == 0) |
192 |
localInit(); |
193 |
return instance; |
194 |
} |
195 |
|
196 |
/** |
197 |
* Throws {@code UnsupportedOperationException}. Setting seeds in |
198 |
* this generator is not supported. |
199 |
* |
200 |
* @throws UnsupportedOperationException always |
201 |
*/ |
202 |
public void setSeed(long seed) { |
203 |
// only allow call from super() constructor |
204 |
if (initialized) |
205 |
throw new UnsupportedOperationException(); |
206 |
} |
207 |
|
208 |
final long nextSeed() { |
209 |
Thread t; long r; // read and update per-thread seed |
210 |
U.putLong(t = Thread.currentThread(), SEED, |
211 |
r = U.getLong(t, SEED) + GAMMA); |
212 |
return r; |
213 |
} |
214 |
|
215 |
// We must define this, but never use it. |
216 |
protected int next(int bits) { |
217 |
return (int)(mix64(nextSeed()) >>> (64 - bits)); |
218 |
} |
219 |
|
220 |
// IllegalArgumentException messages |
221 |
static final String BAD_BOUND = "bound must be positive"; |
222 |
static final String BAD_RANGE = "bound must be greater than origin"; |
223 |
static final String BAD_SIZE = "size must be non-negative"; |
224 |
|
225 |
/** |
226 |
* The form of nextLong used by LongStream Spliterators. If |
227 |
* origin is greater than bound, acts as unbounded form of |
228 |
* nextLong, else as bounded form. |
229 |
* |
230 |
* @param origin the least value, unless greater than bound |
231 |
* @param bound the upper bound (exclusive), must not equal origin |
232 |
* @return a pseudorandom value |
233 |
*/ |
234 |
final long internalNextLong(long origin, long bound) { |
235 |
long r = mix64(nextSeed()); |
236 |
if (origin < bound) { |
237 |
long n = bound - origin, m = n - 1; |
238 |
if ((n & m) == 0L) // power of two |
239 |
r = (r & m) + origin; |
240 |
else if (n > 0L) { // reject over-represented candidates |
241 |
for (long u = r >>> 1; // ensure nonnegative |
242 |
u + m - (r = u % n) < 0L; // rejection check |
243 |
u = mix64(nextSeed()) >>> 1) // retry |
244 |
; |
245 |
r += origin; |
246 |
} |
247 |
else { // range not representable as long |
248 |
while (r < origin || r >= bound) |
249 |
r = mix64(nextSeed()); |
250 |
} |
251 |
} |
252 |
return r; |
253 |
} |
254 |
|
255 |
/** |
256 |
* The form of nextInt used by IntStream Spliterators. |
257 |
* Exactly the same as long version, except for types. |
258 |
* |
259 |
* @param origin the least value, unless greater than bound |
260 |
* @param bound the upper bound (exclusive), must not equal origin |
261 |
* @return a pseudorandom value |
262 |
*/ |
263 |
final int internalNextInt(int origin, int bound) { |
264 |
int r = mix32(nextSeed()); |
265 |
if (origin < bound) { |
266 |
int n = bound - origin, m = n - 1; |
267 |
if ((n & m) == 0) |
268 |
r = (r & m) + origin; |
269 |
else if (n > 0) { |
270 |
for (int u = r >>> 1; |
271 |
u + m - (r = u % n) < 0; |
272 |
u = mix32(nextSeed()) >>> 1) |
273 |
; |
274 |
r += origin; |
275 |
} |
276 |
else { |
277 |
while (r < origin || r >= bound) |
278 |
r = mix32(nextSeed()); |
279 |
} |
280 |
} |
281 |
return r; |
282 |
} |
283 |
|
284 |
/** |
285 |
* The form of nextDouble used by DoubleStream Spliterators. |
286 |
* |
287 |
* @param origin the least value, unless greater than bound |
288 |
* @param bound the upper bound (exclusive), must not equal origin |
289 |
* @return a pseudorandom value |
290 |
*/ |
291 |
final double internalNextDouble(double origin, double bound) { |
292 |
double r = (nextLong() >>> 11) * DOUBLE_UNIT; |
293 |
if (origin < bound) { |
294 |
r = r * (bound - origin) + origin; |
295 |
if (r >= bound) // correct for rounding |
296 |
r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1); |
297 |
} |
298 |
return r; |
299 |
} |
300 |
|
301 |
/** |
302 |
* Returns a pseudorandom {@code int} value. |
303 |
* |
304 |
* @return a pseudorandom {@code int} value |
305 |
*/ |
306 |
public int nextInt() { |
307 |
return mix32(nextSeed()); |
308 |
} |
309 |
|
310 |
/** |
311 |
* Returns a pseudorandom {@code int} value between zero (inclusive) |
312 |
* and the specified bound (exclusive). |
313 |
* |
314 |
* @param bound the upper bound (exclusive). Must be positive. |
315 |
* @return a pseudorandom {@code int} value between zero |
316 |
* (inclusive) and the bound (exclusive) |
317 |
* @throws IllegalArgumentException if {@code bound} is not positive |
318 |
*/ |
319 |
public int nextInt(int bound) { |
320 |
if (bound <= 0) |
321 |
throw new IllegalArgumentException(BAD_BOUND); |
322 |
int r = mix32(nextSeed()); |
323 |
int m = bound - 1; |
324 |
if ((bound & m) == 0) // power of two |
325 |
r &= m; |
326 |
else { // reject over-represented candidates |
327 |
for (int u = r >>> 1; |
328 |
u + m - (r = u % bound) < 0; |
329 |
u = mix32(nextSeed()) >>> 1) |
330 |
; |
331 |
} |
332 |
return r; |
333 |
} |
334 |
|
335 |
/** |
336 |
* Returns a pseudorandom {@code int} value between the specified |
337 |
* origin (inclusive) and the specified bound (exclusive). |
338 |
* |
339 |
* @param origin the least value returned |
340 |
* @param bound the upper bound (exclusive) |
341 |
* @return a pseudorandom {@code int} value between the origin |
342 |
* (inclusive) and the bound (exclusive) |
343 |
* @throws IllegalArgumentException if {@code origin} is greater than |
344 |
* or equal to {@code bound} |
345 |
*/ |
346 |
public int nextInt(int origin, int bound) { |
347 |
if (origin >= bound) |
348 |
throw new IllegalArgumentException(BAD_RANGE); |
349 |
return internalNextInt(origin, bound); |
350 |
} |
351 |
|
352 |
/** |
353 |
* Returns a pseudorandom {@code long} value. |
354 |
* |
355 |
* @return a pseudorandom {@code long} value |
356 |
*/ |
357 |
public long nextLong() { |
358 |
return mix64(nextSeed()); |
359 |
} |
360 |
|
361 |
/** |
362 |
* Returns a pseudorandom {@code long} value between zero (inclusive) |
363 |
* and the specified bound (exclusive). |
364 |
* |
365 |
* @param bound the upper bound (exclusive). Must be positive. |
366 |
* @return a pseudorandom {@code long} value between zero |
367 |
* (inclusive) and the bound (exclusive) |
368 |
* @throws IllegalArgumentException if {@code bound} is not positive |
369 |
*/ |
370 |
public long nextLong(long bound) { |
371 |
if (bound <= 0) |
372 |
throw new IllegalArgumentException(BAD_BOUND); |
373 |
long r = mix64(nextSeed()); |
374 |
long m = bound - 1; |
375 |
if ((bound & m) == 0L) // power of two |
376 |
r &= m; |
377 |
else { // reject over-represented candidates |
378 |
for (long u = r >>> 1; |
379 |
u + m - (r = u % bound) < 0L; |
380 |
u = mix64(nextSeed()) >>> 1) |
381 |
; |
382 |
} |
383 |
return r; |
384 |
} |
385 |
|
386 |
/** |
387 |
* Returns a pseudorandom {@code long} value between the specified |
388 |
* origin (inclusive) and the specified bound (exclusive). |
389 |
* |
390 |
* @param origin the least value returned |
391 |
* @param bound the upper bound (exclusive) |
392 |
* @return a pseudorandom {@code long} value between the origin |
393 |
* (inclusive) and the bound (exclusive) |
394 |
* @throws IllegalArgumentException if {@code origin} is greater than |
395 |
* or equal to {@code bound} |
396 |
*/ |
397 |
public long nextLong(long origin, long bound) { |
398 |
if (origin >= bound) |
399 |
throw new IllegalArgumentException(BAD_RANGE); |
400 |
return internalNextLong(origin, bound); |
401 |
} |
402 |
|
403 |
/** |
404 |
* Returns a pseudorandom {@code double} value between zero |
405 |
* (inclusive) and one (exclusive). |
406 |
* |
407 |
* @return a pseudorandom {@code double} value between zero |
408 |
* (inclusive) and one (exclusive) |
409 |
*/ |
410 |
public double nextDouble() { |
411 |
return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT; |
412 |
} |
413 |
|
414 |
/** |
415 |
* Returns a pseudorandom {@code double} value between 0.0 |
416 |
* (inclusive) and the specified bound (exclusive). |
417 |
* |
418 |
* @param bound the upper bound (exclusive). Must be positive. |
419 |
* @return a pseudorandom {@code double} value between zero |
420 |
* (inclusive) and the bound (exclusive) |
421 |
* @throws IllegalArgumentException if {@code bound} is not positive |
422 |
*/ |
423 |
public double nextDouble(double bound) { |
424 |
if (!(bound > 0.0)) |
425 |
throw new IllegalArgumentException(BAD_BOUND); |
426 |
double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound; |
427 |
return (result < bound) ? result : // correct for rounding |
428 |
Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1); |
429 |
} |
430 |
|
431 |
/** |
432 |
* Returns a pseudorandom {@code double} value between the specified |
433 |
* origin (inclusive) and bound (exclusive). |
434 |
* |
435 |
* @param origin the least value returned |
436 |
* @param bound the upper bound (exclusive) |
437 |
* @return a pseudorandom {@code double} value between the origin |
438 |
* (inclusive) and the bound (exclusive) |
439 |
* @throws IllegalArgumentException if {@code origin} is greater than |
440 |
* or equal to {@code bound} |
441 |
*/ |
442 |
public double nextDouble(double origin, double bound) { |
443 |
if (!(origin < bound)) |
444 |
throw new IllegalArgumentException(BAD_RANGE); |
445 |
return internalNextDouble(origin, bound); |
446 |
} |
447 |
|
448 |
/** |
449 |
* Returns a pseudorandom {@code boolean} value. |
450 |
* |
451 |
* @return a pseudorandom {@code boolean} value |
452 |
*/ |
453 |
public boolean nextBoolean() { |
454 |
return mix32(nextSeed()) < 0; |
455 |
} |
456 |
|
457 |
/** |
458 |
* Returns a pseudorandom {@code float} value between zero |
459 |
* (inclusive) and one (exclusive). |
460 |
* |
461 |
* @return a pseudorandom {@code float} value between zero |
462 |
* (inclusive) and one (exclusive) |
463 |
*/ |
464 |
public float nextFloat() { |
465 |
return (mix32(nextSeed()) >>> 8) * FLOAT_UNIT; |
466 |
} |
467 |
|
468 |
public double nextGaussian() { |
469 |
// Use nextLocalGaussian instead of nextGaussian field |
470 |
Double d = nextLocalGaussian.get(); |
471 |
if (d != null) { |
472 |
nextLocalGaussian.set(null); |
473 |
return d.doubleValue(); |
474 |
} |
475 |
double v1, v2, s; |
476 |
do { |
477 |
v1 = 2 * nextDouble() - 1; // between -1 and 1 |
478 |
v2 = 2 * nextDouble() - 1; // between -1 and 1 |
479 |
s = v1 * v1 + v2 * v2; |
480 |
} while (s >= 1 || s == 0); |
481 |
double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
482 |
nextLocalGaussian.set(new Double(v2 * multiplier)); |
483 |
return v1 * multiplier; |
484 |
} |
485 |
|
486 |
// stream methods, coded in a way intended to better isolate for |
487 |
// maintenance purposes the small differences across forms. |
488 |
|
489 |
/** |
490 |
* Returns a stream producing the given {@code streamSize} number of |
491 |
* pseudorandom {@code int} values. |
492 |
* |
493 |
* @param streamSize the number of values to generate |
494 |
* @return a stream of pseudorandom {@code int} values |
495 |
* @throws IllegalArgumentException if {@code streamSize} is |
496 |
* less than zero |
497 |
* @since 1.8 |
498 |
*/ |
499 |
public IntStream ints(long streamSize) { |
500 |
if (streamSize < 0L) |
501 |
throw new IllegalArgumentException(BAD_SIZE); |
502 |
return StreamSupport.intStream |
503 |
(new RandomIntsSpliterator |
504 |
(0L, streamSize, Integer.MAX_VALUE, 0), |
505 |
false); |
506 |
} |
507 |
|
508 |
/** |
509 |
* Returns an effectively unlimited stream of pseudorandom {@code int} |
510 |
* values. |
511 |
* |
512 |
* @implNote This method is implemented to be equivalent to {@code |
513 |
* ints(Long.MAX_VALUE)}. |
514 |
* |
515 |
* @return a stream of pseudorandom {@code int} values |
516 |
* @since 1.8 |
517 |
*/ |
518 |
public IntStream ints() { |
519 |
return StreamSupport.intStream |
520 |
(new RandomIntsSpliterator |
521 |
(0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0), |
522 |
false); |
523 |
} |
524 |
|
525 |
/** |
526 |
* Returns a stream producing the given {@code streamSize} number |
527 |
* of pseudorandom {@code int} values, each conforming to the given |
528 |
* origin (inclusive) and bound (exclusive). |
529 |
* |
530 |
* @param streamSize the number of values to generate |
531 |
* @param randomNumberOrigin the origin (inclusive) of each random value |
532 |
* @param randomNumberBound the bound (exclusive) of each random value |
533 |
* @return a stream of pseudorandom {@code int} values, |
534 |
* each with the given origin (inclusive) and bound (exclusive) |
535 |
* @throws IllegalArgumentException if {@code streamSize} is |
536 |
* less than zero, or {@code randomNumberOrigin} |
537 |
* is greater than or equal to {@code randomNumberBound} |
538 |
* @since 1.8 |
539 |
*/ |
540 |
public IntStream ints(long streamSize, int randomNumberOrigin, |
541 |
int randomNumberBound) { |
542 |
if (streamSize < 0L) |
543 |
throw new IllegalArgumentException(BAD_SIZE); |
544 |
if (randomNumberOrigin >= randomNumberBound) |
545 |
throw new IllegalArgumentException(BAD_RANGE); |
546 |
return StreamSupport.intStream |
547 |
(new RandomIntsSpliterator |
548 |
(0L, streamSize, randomNumberOrigin, randomNumberBound), |
549 |
false); |
550 |
} |
551 |
|
552 |
/** |
553 |
* Returns an effectively unlimited stream of pseudorandom {@code |
554 |
* int} values, each conforming to the given origin (inclusive) and bound |
555 |
* (exclusive). |
556 |
* |
557 |
* @implNote This method is implemented to be equivalent to {@code |
558 |
* ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
559 |
* |
560 |
* @param randomNumberOrigin the origin (inclusive) of each random value |
561 |
* @param randomNumberBound the bound (exclusive) of each random value |
562 |
* @return a stream of pseudorandom {@code int} values, |
563 |
* each with the given origin (inclusive) and bound (exclusive) |
564 |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
565 |
* is greater than or equal to {@code randomNumberBound} |
566 |
* @since 1.8 |
567 |
*/ |
568 |
public IntStream ints(int randomNumberOrigin, int randomNumberBound) { |
569 |
if (randomNumberOrigin >= randomNumberBound) |
570 |
throw new IllegalArgumentException(BAD_RANGE); |
571 |
return StreamSupport.intStream |
572 |
(new RandomIntsSpliterator |
573 |
(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
574 |
false); |
575 |
} |
576 |
|
577 |
/** |
578 |
* Returns a stream producing the given {@code streamSize} number of |
579 |
* pseudorandom {@code long} values. |
580 |
* |
581 |
* @param streamSize the number of values to generate |
582 |
* @return a stream of pseudorandom {@code long} values |
583 |
* @throws IllegalArgumentException if {@code streamSize} is |
584 |
* less than zero |
585 |
* @since 1.8 |
586 |
*/ |
587 |
public LongStream longs(long streamSize) { |
588 |
if (streamSize < 0L) |
589 |
throw new IllegalArgumentException(BAD_SIZE); |
590 |
return StreamSupport.longStream |
591 |
(new RandomLongsSpliterator |
592 |
(0L, streamSize, Long.MAX_VALUE, 0L), |
593 |
false); |
594 |
} |
595 |
|
596 |
/** |
597 |
* Returns an effectively unlimited stream of pseudorandom {@code long} |
598 |
* values. |
599 |
* |
600 |
* @implNote This method is implemented to be equivalent to {@code |
601 |
* longs(Long.MAX_VALUE)}. |
602 |
* |
603 |
* @return a stream of pseudorandom {@code long} values |
604 |
* @since 1.8 |
605 |
*/ |
606 |
public LongStream longs() { |
607 |
return StreamSupport.longStream |
608 |
(new RandomLongsSpliterator |
609 |
(0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L), |
610 |
false); |
611 |
} |
612 |
|
613 |
/** |
614 |
* Returns a stream producing the given {@code streamSize} number of |
615 |
* pseudorandom {@code long}, each conforming to the given origin |
616 |
* (inclusive) and bound (exclusive). |
617 |
* |
618 |
* @param streamSize the number of values to generate |
619 |
* @param randomNumberOrigin the origin (inclusive) of each random value |
620 |
* @param randomNumberBound the bound (exclusive) of each random value |
621 |
* @return a stream of pseudorandom {@code long} values, |
622 |
* each with the given origin (inclusive) and bound (exclusive) |
623 |
* @throws IllegalArgumentException if {@code streamSize} is |
624 |
* less than zero, or {@code randomNumberOrigin} |
625 |
* is greater than or equal to {@code randomNumberBound} |
626 |
* @since 1.8 |
627 |
*/ |
628 |
public LongStream longs(long streamSize, long randomNumberOrigin, |
629 |
long randomNumberBound) { |
630 |
if (streamSize < 0L) |
631 |
throw new IllegalArgumentException(BAD_SIZE); |
632 |
if (randomNumberOrigin >= randomNumberBound) |
633 |
throw new IllegalArgumentException(BAD_RANGE); |
634 |
return StreamSupport.longStream |
635 |
(new RandomLongsSpliterator |
636 |
(0L, streamSize, randomNumberOrigin, randomNumberBound), |
637 |
false); |
638 |
} |
639 |
|
640 |
/** |
641 |
* Returns an effectively unlimited stream of pseudorandom {@code |
642 |
* long} values, each conforming to the given origin (inclusive) and bound |
643 |
* (exclusive). |
644 |
* |
645 |
* @implNote This method is implemented to be equivalent to {@code |
646 |
* longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
647 |
* |
648 |
* @param randomNumberOrigin the origin (inclusive) of each random value |
649 |
* @param randomNumberBound the bound (exclusive) of each random value |
650 |
* @return a stream of pseudorandom {@code long} values, |
651 |
* each with the given origin (inclusive) and bound (exclusive) |
652 |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
653 |
* is greater than or equal to {@code randomNumberBound} |
654 |
* @since 1.8 |
655 |
*/ |
656 |
public LongStream longs(long randomNumberOrigin, long randomNumberBound) { |
657 |
if (randomNumberOrigin >= randomNumberBound) |
658 |
throw new IllegalArgumentException(BAD_RANGE); |
659 |
return StreamSupport.longStream |
660 |
(new RandomLongsSpliterator |
661 |
(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
662 |
false); |
663 |
} |
664 |
|
665 |
/** |
666 |
* Returns a stream producing the given {@code streamSize} number of |
667 |
* pseudorandom {@code double} values, each between zero |
668 |
* (inclusive) and one (exclusive). |
669 |
* |
670 |
* @param streamSize the number of values to generate |
671 |
* @return a stream of {@code double} values |
672 |
* @throws IllegalArgumentException if {@code streamSize} is |
673 |
* less than zero |
674 |
* @since 1.8 |
675 |
*/ |
676 |
public DoubleStream doubles(long streamSize) { |
677 |
if (streamSize < 0L) |
678 |
throw new IllegalArgumentException(BAD_SIZE); |
679 |
return StreamSupport.doubleStream |
680 |
(new RandomDoublesSpliterator |
681 |
(0L, streamSize, Double.MAX_VALUE, 0.0), |
682 |
false); |
683 |
} |
684 |
|
685 |
/** |
686 |
* Returns an effectively unlimited stream of pseudorandom {@code |
687 |
* double} values, each between zero (inclusive) and one |
688 |
* (exclusive). |
689 |
* |
690 |
* @implNote This method is implemented to be equivalent to {@code |
691 |
* doubles(Long.MAX_VALUE)}. |
692 |
* |
693 |
* @return a stream of pseudorandom {@code double} values |
694 |
* @since 1.8 |
695 |
*/ |
696 |
public DoubleStream doubles() { |
697 |
return StreamSupport.doubleStream |
698 |
(new RandomDoublesSpliterator |
699 |
(0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0), |
700 |
false); |
701 |
} |
702 |
|
703 |
/** |
704 |
* Returns a stream producing the given {@code streamSize} number of |
705 |
* pseudorandom {@code double} values, each conforming to the given origin |
706 |
* (inclusive) and bound (exclusive). |
707 |
* |
708 |
* @param streamSize the number of values to generate |
709 |
* @param randomNumberOrigin the origin (inclusive) of each random value |
710 |
* @param randomNumberBound the bound (exclusive) of each random value |
711 |
* @return a stream of pseudorandom {@code double} values, |
712 |
* each with the given origin (inclusive) and bound (exclusive) |
713 |
* @throws IllegalArgumentException if {@code streamSize} is |
714 |
* less than zero |
715 |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
716 |
* is greater than or equal to {@code randomNumberBound} |
717 |
* @since 1.8 |
718 |
*/ |
719 |
public DoubleStream doubles(long streamSize, double randomNumberOrigin, |
720 |
double randomNumberBound) { |
721 |
if (streamSize < 0L) |
722 |
throw new IllegalArgumentException(BAD_SIZE); |
723 |
if (!(randomNumberOrigin < randomNumberBound)) |
724 |
throw new IllegalArgumentException(BAD_RANGE); |
725 |
return StreamSupport.doubleStream |
726 |
(new RandomDoublesSpliterator |
727 |
(0L, streamSize, randomNumberOrigin, randomNumberBound), |
728 |
false); |
729 |
} |
730 |
|
731 |
/** |
732 |
* Returns an effectively unlimited stream of pseudorandom {@code |
733 |
* double} values, each conforming to the given origin (inclusive) and bound |
734 |
* (exclusive). |
735 |
* |
736 |
* @implNote This method is implemented to be equivalent to {@code |
737 |
* doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
738 |
* |
739 |
* @param randomNumberOrigin the origin (inclusive) of each random value |
740 |
* @param randomNumberBound the bound (exclusive) of each random value |
741 |
* @return a stream of pseudorandom {@code double} values, |
742 |
* each with the given origin (inclusive) and bound (exclusive) |
743 |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
744 |
* is greater than or equal to {@code randomNumberBound} |
745 |
* @since 1.8 |
746 |
*/ |
747 |
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { |
748 |
if (!(randomNumberOrigin < randomNumberBound)) |
749 |
throw new IllegalArgumentException(BAD_RANGE); |
750 |
return StreamSupport.doubleStream |
751 |
(new RandomDoublesSpliterator |
752 |
(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
753 |
false); |
754 |
} |
755 |
|
756 |
/** |
757 |
* Spliterator for int streams. We multiplex the four int |
758 |
* versions into one class by treating a bound less than origin as |
759 |
* unbounded, and also by treating "infinite" as equivalent to |
760 |
* Long.MAX_VALUE. For splits, it uses the standard divide-by-two |
761 |
* approach. The long and double versions of this class are |
762 |
* identical except for types. |
763 |
*/ |
764 |
private static final class RandomIntsSpliterator |
765 |
implements Spliterator.OfInt { |
766 |
long index; |
767 |
final long fence; |
768 |
final int origin; |
769 |
final int bound; |
770 |
RandomIntsSpliterator(long index, long fence, |
771 |
int origin, int bound) { |
772 |
this.index = index; this.fence = fence; |
773 |
this.origin = origin; this.bound = bound; |
774 |
} |
775 |
|
776 |
public RandomIntsSpliterator trySplit() { |
777 |
long i = index, m = (i + fence) >>> 1; |
778 |
return (m <= i) ? null : |
779 |
new RandomIntsSpliterator(i, index = m, origin, bound); |
780 |
} |
781 |
|
782 |
public long estimateSize() { |
783 |
return fence - index; |
784 |
} |
785 |
|
786 |
public int characteristics() { |
787 |
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
788 |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
789 |
} |
790 |
|
791 |
public boolean tryAdvance(IntConsumer consumer) { |
792 |
if (consumer == null) throw new NullPointerException(); |
793 |
long i = index, f = fence; |
794 |
if (i < f) { |
795 |
consumer.accept(ThreadLocalRandom.current().internalNextInt(origin, bound)); |
796 |
index = i + 1; |
797 |
return true; |
798 |
} |
799 |
return false; |
800 |
} |
801 |
|
802 |
public void forEachRemaining(IntConsumer consumer) { |
803 |
if (consumer == null) throw new NullPointerException(); |
804 |
long i = index, f = fence; |
805 |
if (i < f) { |
806 |
index = f; |
807 |
int o = origin, b = bound; |
808 |
ThreadLocalRandom rng = ThreadLocalRandom.current(); |
809 |
do { |
810 |
consumer.accept(rng.internalNextInt(o, b)); |
811 |
} while (++i < f); |
812 |
} |
813 |
} |
814 |
} |
815 |
|
816 |
/** |
817 |
* Spliterator for long streams. |
818 |
*/ |
819 |
private static final class RandomLongsSpliterator |
820 |
implements Spliterator.OfLong { |
821 |
long index; |
822 |
final long fence; |
823 |
final long origin; |
824 |
final long bound; |
825 |
RandomLongsSpliterator(long index, long fence, |
826 |
long origin, long bound) { |
827 |
this.index = index; this.fence = fence; |
828 |
this.origin = origin; this.bound = bound; |
829 |
} |
830 |
|
831 |
public RandomLongsSpliterator trySplit() { |
832 |
long i = index, m = (i + fence) >>> 1; |
833 |
return (m <= i) ? null : |
834 |
new RandomLongsSpliterator(i, index = m, origin, bound); |
835 |
} |
836 |
|
837 |
public long estimateSize() { |
838 |
return fence - index; |
839 |
} |
840 |
|
841 |
public int characteristics() { |
842 |
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
843 |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
844 |
} |
845 |
|
846 |
public boolean tryAdvance(LongConsumer consumer) { |
847 |
if (consumer == null) throw new NullPointerException(); |
848 |
long i = index, f = fence; |
849 |
if (i < f) { |
850 |
consumer.accept(ThreadLocalRandom.current().internalNextLong(origin, bound)); |
851 |
index = i + 1; |
852 |
return true; |
853 |
} |
854 |
return false; |
855 |
} |
856 |
|
857 |
public void forEachRemaining(LongConsumer consumer) { |
858 |
if (consumer == null) throw new NullPointerException(); |
859 |
long i = index, f = fence; |
860 |
if (i < f) { |
861 |
index = f; |
862 |
long o = origin, b = bound; |
863 |
ThreadLocalRandom rng = ThreadLocalRandom.current(); |
864 |
do { |
865 |
consumer.accept(rng.internalNextLong(o, b)); |
866 |
} while (++i < f); |
867 |
} |
868 |
} |
869 |
|
870 |
} |
871 |
|
872 |
/** |
873 |
* Spliterator for double streams. |
874 |
*/ |
875 |
private static final class RandomDoublesSpliterator |
876 |
implements Spliterator.OfDouble { |
877 |
long index; |
878 |
final long fence; |
879 |
final double origin; |
880 |
final double bound; |
881 |
RandomDoublesSpliterator(long index, long fence, |
882 |
double origin, double bound) { |
883 |
this.index = index; this.fence = fence; |
884 |
this.origin = origin; this.bound = bound; |
885 |
} |
886 |
|
887 |
public RandomDoublesSpliterator trySplit() { |
888 |
long i = index, m = (i + fence) >>> 1; |
889 |
return (m <= i) ? null : |
890 |
new RandomDoublesSpliterator(i, index = m, origin, bound); |
891 |
} |
892 |
|
893 |
public long estimateSize() { |
894 |
return fence - index; |
895 |
} |
896 |
|
897 |
public int characteristics() { |
898 |
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
899 |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
900 |
} |
901 |
|
902 |
public boolean tryAdvance(DoubleConsumer consumer) { |
903 |
if (consumer == null) throw new NullPointerException(); |
904 |
long i = index, f = fence; |
905 |
if (i < f) { |
906 |
consumer.accept(ThreadLocalRandom.current().internalNextDouble(origin, bound)); |
907 |
index = i + 1; |
908 |
return true; |
909 |
} |
910 |
return false; |
911 |
} |
912 |
|
913 |
public void forEachRemaining(DoubleConsumer consumer) { |
914 |
if (consumer == null) throw new NullPointerException(); |
915 |
long i = index, f = fence; |
916 |
if (i < f) { |
917 |
index = f; |
918 |
double o = origin, b = bound; |
919 |
ThreadLocalRandom rng = ThreadLocalRandom.current(); |
920 |
do { |
921 |
consumer.accept(rng.internalNextDouble(o, b)); |
922 |
} while (++i < f); |
923 |
} |
924 |
} |
925 |
} |
926 |
|
927 |
|
928 |
// Within-package utilities |
929 |
|
930 |
/* |
931 |
* Descriptions of the usages of the methods below can be found in |
932 |
* the classes that use them. Briefly, a thread's "probe" value is |
933 |
* a non-zero hash code that (probably) does not collide with |
934 |
* other existing threads with respect to any power of two |
935 |
* collision space. When it does collide, it is pseudo-randomly |
936 |
* adjusted (using a Marsaglia XorShift). The nextSecondarySeed |
937 |
* method is used in the same contexts as ThreadLocalRandom, but |
938 |
* only for transient usages such as random adaptive spin/block |
939 |
* sequences for which a cheap RNG suffices and for which it could |
940 |
* in principle disrupt user-visible statistical properties of the |
941 |
* main ThreadLocalRandom if we were to use it. |
942 |
* |
943 |
* Note: Because of package-protection issues, versions of some |
944 |
* these methods also appear in some subpackage classes. |
945 |
*/ |
946 |
|
947 |
/** |
948 |
* Returns the probe value for the current thread without forcing |
949 |
* initialization. Note that invoking ThreadLocalRandom.current() |
950 |
* can be used to force initialization on zero return. |
951 |
*/ |
952 |
static final int getProbe() { |
953 |
return U.getInt(Thread.currentThread(), PROBE); |
954 |
} |
955 |
|
956 |
/** |
957 |
* Pseudo-randomly advances and records the given probe value for the |
958 |
* given thread. |
959 |
*/ |
960 |
static final int advanceProbe(int probe) { |
961 |
probe ^= probe << 13; // xorshift |
962 |
probe ^= probe >>> 17; |
963 |
probe ^= probe << 5; |
964 |
U.putInt(Thread.currentThread(), PROBE, probe); |
965 |
return probe; |
966 |
} |
967 |
|
968 |
/** |
969 |
* Returns the pseudo-randomly initialized or updated secondary seed. |
970 |
*/ |
971 |
static final int nextSecondarySeed() { |
972 |
int r; |
973 |
Thread t = Thread.currentThread(); |
974 |
if ((r = U.getInt(t, SECONDARY)) != 0) { |
975 |
r ^= r << 13; // xorshift |
976 |
r ^= r >>> 17; |
977 |
r ^= r << 5; |
978 |
} |
979 |
else if ((r = mix32(seeder.getAndAdd(SEEDER_INCREMENT))) == 0) |
980 |
r = 1; // avoid zero |
981 |
U.putInt(t, SECONDARY, r); |
982 |
return r; |
983 |
} |
984 |
|
985 |
// Serialization support |
986 |
|
987 |
private static final long serialVersionUID = -5851777807851030925L; |
988 |
|
989 |
/** |
990 |
* @serialField rnd long |
991 |
* seed for random computations |
992 |
* @serialField initialized boolean |
993 |
* always true |
994 |
*/ |
995 |
private static final ObjectStreamField[] serialPersistentFields = { |
996 |
new ObjectStreamField("rnd", long.class), |
997 |
new ObjectStreamField("initialized", boolean.class), |
998 |
}; |
999 |
|
1000 |
/** |
1001 |
* Saves the {@code ThreadLocalRandom} to a stream (that is, serializes it). |
1002 |
* @param s the stream |
1003 |
* @throws java.io.IOException if an I/O error occurs |
1004 |
*/ |
1005 |
private void writeObject(java.io.ObjectOutputStream s) |
1006 |
throws java.io.IOException { |
1007 |
|
1008 |
java.io.ObjectOutputStream.PutField fields = s.putFields(); |
1009 |
fields.put("rnd", U.getLong(Thread.currentThread(), SEED)); |
1010 |
fields.put("initialized", true); |
1011 |
s.writeFields(); |
1012 |
} |
1013 |
|
1014 |
/** |
1015 |
* Returns the {@link #current() current} thread's {@code ThreadLocalRandom}. |
1016 |
* @return the {@link #current() current} thread's {@code ThreadLocalRandom} |
1017 |
*/ |
1018 |
private Object readResolve() { |
1019 |
return current(); |
1020 |
} |
1021 |
|
1022 |
// Unsafe mechanics |
1023 |
private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe(); |
1024 |
private static final long SEED; |
1025 |
private static final long PROBE; |
1026 |
private static final long SECONDARY; |
1027 |
static { |
1028 |
try { |
1029 |
SEED = U.objectFieldOffset |
1030 |
(Thread.class.getDeclaredField("threadLocalRandomSeed")); |
1031 |
PROBE = U.objectFieldOffset |
1032 |
(Thread.class.getDeclaredField("threadLocalRandomProbe")); |
1033 |
SECONDARY = U.objectFieldOffset |
1034 |
(Thread.class.getDeclaredField("threadLocalRandomSecondarySeed")); |
1035 |
} catch (ReflectiveOperationException e) { |
1036 |
throw new Error(e); |
1037 |
} |
1038 |
} |
1039 |
} |