ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ThreadLocalRandom.java
Revision: 1.21
Committed: Thu Aug 22 23:36:02 2013 UTC (10 years, 9 months ago) by dl
Branch: MAIN
Changes since 1.20: +84 -48 lines
Log Message:
Optional SecureRandom initial seed

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