ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ThreadLocalRandom.java
Revision: 1.28
Committed: Thu Jun 19 12:01:07 2014 UTC (9 years, 11 months ago) by dl
Branch: MAIN
Changes since 1.27: +2 -5 lines
Log Message:
Avoid double-initialization

File Contents

# User Rev Content
1 jsr166 1.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 jsr166 1.6 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.1 */
6    
7     package java.util.concurrent;
8    
9 dl 1.25 import java.io.ObjectStreamField;
10 dl 1.24 import java.net.NetworkInterface;
11     import java.util.Enumeration;
12 jsr166 1.1 import java.util.Random;
13 dl 1.19 import java.util.Spliterator;
14 dl 1.7 import java.util.concurrent.atomic.AtomicInteger;
15     import java.util.concurrent.atomic.AtomicLong;
16 dl 1.19 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 jsr166 1.1
24     /**
25 jsr166 1.3 * A random number generator isolated to the current thread. Like the
26 jsr166 1.2 * global {@link java.util.Random} generator used by the {@link
27 jsr166 1.3 * 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 jsr166 1.1 *
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 jsr166 1.3 * accidently share a {@code ThreadLocalRandom} across multiple threads.
41 jsr166 1.1 *
42     * <p>This class also provides additional commonly used bounded random
43     * generation methods.
44     *
45 dl 1.21 * <p>Instances of {@code ThreadLocalRandom} are not cryptographically
46     * secure. Consider instead using {@link java.security.SecureRandom}
47 dl 1.23 * 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 dl 1.21 *
52 jsr166 1.1 * @since 1.7
53     * @author Doug Lea
54     */
55     public class ThreadLocalRandom extends Random {
56 dl 1.7 /*
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 dl 1.19 * 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 dl 1.7 * Because this class is in a different package than class Thread,
80 jsr166 1.15 * field access methods use Unsafe to bypass access control rules.
81 dl 1.19 * 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 dl 1.7 *
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 dl 1.19 /** Generates per-thread initialization/probe field */
102     private static final AtomicInteger probeGenerator =
103     new AtomicInteger();
104 jsr166 1.1
105 dl 1.19 /**
106     * The next seed for default constructors.
107     */
108 dl 1.23 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 dl 1.24 long h = 0L;
122 dl 1.23 try {
123 dl 1.24 Enumeration<NetworkInterface> ifcs =
124 dl 1.25 NetworkInterface.getNetworkInterfaces();
125 dl 1.24 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 dl 1.23 } catch (Exception ignore) {
147     }
148 dl 1.24 return (h ^ mix64(System.currentTimeMillis()) ^
149 dl 1.23 mix64(System.nanoTime()));
150     }
151    
152 dl 1.19 /**
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 dl 1.21 * The increment of seeder per new instance
164 dl 1.19 */
165     private static final long SEEDER_INCREMENT = 0xbb67ae8584caa73bL;
166    
167     // Constants from SplittableRandom
168 dl 1.25 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 jsr166 1.1
171 dl 1.7 /** Rarely-used holder for the second of a pair of Gaussians */
172     private static final ThreadLocal<Double> nextLocalGaussian =
173 jsr166 1.26 new ThreadLocal<>();
174 jsr166 1.1
175 dl 1.19 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 jsr166 1.11 /**
187     * Field used only during singleton initialization.
188     * True when constructor completes.
189 jsr166 1.1 */
190 jsr166 1.11 boolean initialized;
191 dl 1.7
192     /** Constructor used only for static singleton */
193     private ThreadLocalRandom() {
194     initialized = true; // false during super() call
195     }
196 jsr166 1.1
197 dl 1.7 /** The common ThreadLocalRandom */
198     static final ThreadLocalRandom instance = new ThreadLocalRandom();
199 jsr166 1.1
200     /**
201 dl 1.7 * 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 dl 1.19 int p = probeGenerator.addAndGet(PROBE_INCREMENT);
209 dl 1.7 int probe = (p == 0) ? 1 : p; // skip 0
210 dl 1.21 long seed = mix64(seeder.getAndAdd(SEEDER_INCREMENT));
211 dl 1.7 Thread t = Thread.currentThread();
212 dl 1.19 UNSAFE.putLong(t, SEED, seed);
213 dl 1.7 UNSAFE.putInt(t, PROBE, probe);
214 jsr166 1.1 }
215    
216     /**
217 jsr166 1.3 * Returns the current thread's {@code ThreadLocalRandom}.
218 jsr166 1.1 *
219 jsr166 1.3 * @return the current thread's {@code ThreadLocalRandom}
220 jsr166 1.1 */
221     public static ThreadLocalRandom current() {
222 dl 1.7 if (UNSAFE.getInt(Thread.currentThread(), PROBE) == 0)
223     localInit();
224     return instance;
225 jsr166 1.1 }
226    
227     /**
228 jsr166 1.3 * Throws {@code UnsupportedOperationException}. Setting seeds in
229     * this generator is not supported.
230 jsr166 1.1 *
231     * @throws UnsupportedOperationException always
232     */
233     public void setSeed(long seed) {
234 jsr166 1.15 // only allow call from super() constructor
235     if (initialized)
236 jsr166 1.1 throw new UnsupportedOperationException();
237     }
238    
239 dl 1.19 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 jsr166 1.1 protected int next(int bits) {
248 dl 1.19 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 dl 1.21 * @param bound the upper bound (exclusive). Must be positive.
346 dl 1.19 * @return a pseudorandom {@code int} value between zero
347     * (inclusive) and the bound (exclusive)
348 dl 1.20 * @throws IllegalArgumentException if {@code bound} is not positive
349 dl 1.19 */
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 jsr166 1.1 }
365    
366     /**
367 dl 1.19 * Returns a pseudorandom {@code int} value between the specified
368     * origin (inclusive) and the specified bound (exclusive).
369 jsr166 1.1 *
370 dl 1.19 * @param origin the least value returned
371 jsr166 1.1 * @param bound the upper bound (exclusive)
372 dl 1.19 * @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 jsr166 1.1 */
388 dl 1.19 public long nextLong() {
389     return mix64(nextSeed());
390 jsr166 1.1 }
391    
392     /**
393 dl 1.19 * Returns a pseudorandom {@code long} value between zero (inclusive)
394     * and the specified bound (exclusive).
395 jsr166 1.1 *
396 dl 1.21 * @param bound the upper bound (exclusive). Must be positive.
397 dl 1.19 * @return a pseudorandom {@code long} value between zero
398     * (inclusive) and the bound (exclusive)
399 dl 1.20 * @throws IllegalArgumentException if {@code bound} is not positive
400 dl 1.19 */
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 jsr166 1.1 }
414 dl 1.19 return r;
415 jsr166 1.1 }
416    
417     /**
418 dl 1.19 * Returns a pseudorandom {@code long} value between the specified
419     * origin (inclusive) and the specified bound (exclusive).
420 jsr166 1.1 *
421 dl 1.19 * @param origin the least value returned
422 jsr166 1.1 * @param bound the upper bound (exclusive)
423 dl 1.19 * @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 dl 1.21 * (inclusive) and one (exclusive)
440 jsr166 1.1 */
441 dl 1.19 public double nextDouble() {
442     return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT;
443 jsr166 1.1 }
444    
445     /**
446 dl 1.19 * Returns a pseudorandom {@code double} value between 0.0
447     * (inclusive) and the specified bound (exclusive).
448 jsr166 1.1 *
449 dl 1.21 * @param bound the upper bound (exclusive). Must be positive.
450 dl 1.19 * @return a pseudorandom {@code double} value between zero
451     * (inclusive) and the bound (exclusive)
452 dl 1.20 * @throws IllegalArgumentException if {@code bound} is not positive
453 dl 1.19 */
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 jsr166 1.27 return (result < bound) ? result : // correct for rounding
459 dl 1.19 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 dl 1.21 * @param bound the upper bound (exclusive)
468 dl 1.19 * @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 jsr166 1.1 */
484 dl 1.19 public boolean nextBoolean() {
485     return mix32(nextSeed()) < 0;
486 jsr166 1.1 }
487    
488     /**
489 dl 1.19 * Returns a pseudorandom {@code float} value between zero
490     * (inclusive) and one (exclusive).
491 jsr166 1.1 *
492 dl 1.19 * @return a pseudorandom {@code float} value between zero
493 dl 1.21 * (inclusive) and one (exclusive)
494 dl 1.19 */
495     public float nextFloat() {
496     return (mix32(nextSeed()) >>> 8) * FLOAT_UNIT;
497 jsr166 1.1 }
498    
499 dl 1.7 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 dl 1.19 // 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 dl 1.21 * @since 1.8
529 dl 1.19 */
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 dl 1.21 * @since 1.8
548 dl 1.19 */
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 dl 1.21 * 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 dl 1.19 *
561     * @param streamSize the number of values to generate
562 dl 1.21 * @param randomNumberOrigin the origin (inclusive) of each random value
563     * @param randomNumberBound the bound (exclusive) of each random value
564 dl 1.19 * @return a stream of pseudorandom {@code int} values,
565 dl 1.21 * each with the given origin (inclusive) and bound (exclusive)
566 dl 1.19 * @throws IllegalArgumentException if {@code streamSize} is
567     * less than zero, or {@code randomNumberOrigin}
568     * is greater than or equal to {@code randomNumberBound}
569 dl 1.21 * @since 1.8
570 dl 1.19 */
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 dl 1.21 * int} values, each conforming to the given origin (inclusive) and bound
586     * (exclusive).
587 dl 1.19 *
588     * @implNote This method is implemented to be equivalent to {@code
589     * ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
590     *
591 dl 1.21 * @param randomNumberOrigin the origin (inclusive) of each random value
592     * @param randomNumberBound the bound (exclusive) of each random value
593 dl 1.19 * @return a stream of pseudorandom {@code int} values,
594 dl 1.21 * each with the given origin (inclusive) and bound (exclusive)
595 dl 1.19 * @throws IllegalArgumentException if {@code randomNumberOrigin}
596     * is greater than or equal to {@code randomNumberBound}
597 dl 1.21 * @since 1.8
598 dl 1.19 */
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 dl 1.21 * @since 1.8
617 dl 1.19 */
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 dl 1.21 * @since 1.8
636 dl 1.19 */
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 dl 1.21 * pseudorandom {@code long}, each conforming to the given origin
647     * (inclusive) and bound (exclusive).
648 dl 1.19 *
649     * @param streamSize the number of values to generate
650 dl 1.21 * @param randomNumberOrigin the origin (inclusive) of each random value
651     * @param randomNumberBound the bound (exclusive) of each random value
652 dl 1.19 * @return a stream of pseudorandom {@code long} values,
653 dl 1.21 * each with the given origin (inclusive) and bound (exclusive)
654 dl 1.19 * @throws IllegalArgumentException if {@code streamSize} is
655     * less than zero, or {@code randomNumberOrigin}
656     * is greater than or equal to {@code randomNumberBound}
657 dl 1.21 * @since 1.8
658 dl 1.19 */
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 dl 1.21 * long} values, each conforming to the given origin (inclusive) and bound
674     * (exclusive).
675 dl 1.19 *
676     * @implNote This method is implemented to be equivalent to {@code
677     * longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
678     *
679 dl 1.21 * @param randomNumberOrigin the origin (inclusive) of each random value
680     * @param randomNumberBound the bound (exclusive) of each random value
681 dl 1.19 * @return a stream of pseudorandom {@code long} values,
682 dl 1.21 * each with the given origin (inclusive) and bound (exclusive)
683 dl 1.19 * @throws IllegalArgumentException if {@code randomNumberOrigin}
684     * is greater than or equal to {@code randomNumberBound}
685 dl 1.21 * @since 1.8
686 dl 1.19 */
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 dl 1.21 * @since 1.8
706 dl 1.19 */
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 dl 1.21 * @since 1.8
726 dl 1.19 */
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 dl 1.21 * pseudorandom {@code double} values, each conforming to the given origin
737     * (inclusive) and bound (exclusive).
738 dl 1.19 *
739     * @param streamSize the number of values to generate
740 dl 1.21 * @param randomNumberOrigin the origin (inclusive) of each random value
741     * @param randomNumberBound the bound (exclusive) of each random value
742 dl 1.19 * @return a stream of pseudorandom {@code double} values,
743 dl 1.21 * each with the given origin (inclusive) and bound (exclusive)
744 dl 1.19 * @throws IllegalArgumentException if {@code streamSize} is
745 dl 1.21 * less than zero
746 dl 1.19 * @throws IllegalArgumentException if {@code randomNumberOrigin}
747     * is greater than or equal to {@code randomNumberBound}
748 dl 1.21 * @since 1.8
749 dl 1.19 */
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 dl 1.21 * double} values, each conforming to the given origin (inclusive) and bound
765     * (exclusive).
766 dl 1.19 *
767     * @implNote This method is implemented to be equivalent to {@code
768     * doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
769     *
770 dl 1.21 * @param randomNumberOrigin the origin (inclusive) of each random value
771     * @param randomNumberBound the bound (exclusive) of each random value
772 dl 1.19 * @return a stream of pseudorandom {@code double} values,
773 dl 1.21 * each with the given origin (inclusive) and bound (exclusive)
774 dl 1.19 * @throws IllegalArgumentException if {@code randomNumberOrigin}
775     * is greater than or equal to {@code randomNumberBound}
776 dl 1.21 * @since 1.8
777 dl 1.19 */
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 dl 1.7 // 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 dl 1.9 * these methods also appear in some subpackage classes.
973 dl 1.7 */
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 dl 1.28 else if ((r = mix32(seeder.getAndAdd(SEEDER_INCREMENT))) == 0)
1008     r = 1; // avoid zero
1009 dl 1.7 UNSAFE.putInt(t, SECONDARY, r);
1010     return r;
1011     }
1012    
1013 jsr166 1.13 // Serialization support
1014 dl 1.9
1015 jsr166 1.1 private static final long serialVersionUID = -5851777807851030925L;
1016 dl 1.7
1017 dl 1.9 /**
1018 jsr166 1.14 * @serialField rnd long
1019     * seed for random computations
1020     * @serialField initialized boolean
1021     * always true
1022     */
1023     private static final ObjectStreamField[] serialPersistentFields = {
1024     new ObjectStreamField("rnd", long.class),
1025     new ObjectStreamField("initialized", boolean.class),
1026     };
1027    
1028     /**
1029     * Saves the {@code ThreadLocalRandom} to a stream (that is, serializes it).
1030 jsr166 1.16 * @param s the stream
1031 jsr166 1.17 * @throws java.io.IOException if an I/O error occurs
1032 jsr166 1.14 */
1033 jsr166 1.16 private void writeObject(java.io.ObjectOutputStream s)
1034 jsr166 1.14 throws java.io.IOException {
1035    
1036 jsr166 1.16 java.io.ObjectOutputStream.PutField fields = s.putFields();
1037 jsr166 1.14 fields.put("rnd", UNSAFE.getLong(Thread.currentThread(), SEED));
1038     fields.put("initialized", true);
1039 jsr166 1.16 s.writeFields();
1040 jsr166 1.14 }
1041    
1042     /**
1043 dl 1.9 * Returns the {@link #current() current} thread's {@code ThreadLocalRandom}.
1044 jsr166 1.16 * @return the {@link #current() current} thread's {@code ThreadLocalRandom}
1045 dl 1.9 */
1046     private Object readResolve() {
1047     return current();
1048     }
1049    
1050 dl 1.7 // Unsafe mechanics
1051     private static final sun.misc.Unsafe UNSAFE;
1052     private static final long SEED;
1053     private static final long PROBE;
1054     private static final long SECONDARY;
1055     static {
1056     try {
1057     UNSAFE = sun.misc.Unsafe.getUnsafe();
1058     Class<?> tk = Thread.class;
1059     SEED = UNSAFE.objectFieldOffset
1060     (tk.getDeclaredField("threadLocalRandomSeed"));
1061     PROBE = UNSAFE.objectFieldOffset
1062     (tk.getDeclaredField("threadLocalRandomProbe"));
1063     SECONDARY = UNSAFE.objectFieldOffset
1064     (tk.getDeclaredField("threadLocalRandomSecondarySeed"));
1065     } catch (Exception e) {
1066     throw new Error(e);
1067     }
1068     }
1069 jsr166 1.1 }