ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ThreadLocalRandom.java
Revision: 1.27
Committed: Fri May 30 16:12:06 2014 UTC (10 years ago) by jsr166
Branch: MAIN
Changes since 1.26: +1 -1 lines
Log Message:
whitespace

File Contents

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