ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jdk8/java/util/concurrent/ThreadLocalRandom.java
Revision: 1.3
Committed: Mon Feb 20 22:07:50 2017 UTC (7 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +1 -2 lines
Log Message:
coalesce multiple @throws IllegalArgumentException for doubles; consistent with ints and longs

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