[cvs] / jsr166 / src / main / java / util / SplittableRandom.java Repository:
ViewVC logotype

Annotation of /jsr166/src/main/java/util/SplittableRandom.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (view) (download)

1 : dl 1.1 /*
2 :     * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 :     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 :     *
5 :     * This code is free software; you can redistribute it and/or modify it
6 :     * under the terms of the GNU General Public License version 2 only, as
7 :     * published by the Free Software Foundation. Oracle designates this
8 :     * particular file as subject to the "Classpath" exception as provided
9 :     * by Oracle in the LICENSE file that accompanied this code.
10 :     *
11 :     * This code is distributed in the hope that it will be useful, but WITHOUT
12 :     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 :     * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 :     * version 2 for more details (a copy is included in the LICENSE file that
15 :     * accompanied this code).
16 :     *
17 :     * You should have received a copy of the GNU General Public License version
18 :     * 2 along with this work; if not, write to the Free Software Foundation,
19 :     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 :     *
21 :     * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 :     * or visit www.oracle.com if you need additional information or have any
23 :     * questions.
24 :     */
25 :    
26 :     package java.util;
27 :    
28 : dl 1.21 import java.net.NetworkInterface;
29 : dl 1.1 import java.util.concurrent.atomic.AtomicLong;
30 :     import java.util.function.IntConsumer;
31 :     import java.util.function.LongConsumer;
32 :     import java.util.function.DoubleConsumer;
33 :     import java.util.stream.StreamSupport;
34 :     import java.util.stream.IntStream;
35 :     import java.util.stream.LongStream;
36 :     import java.util.stream.DoubleStream;
37 :    
38 :     /**
39 :     * A generator of uniform pseudorandom values applicable for use in
40 :     * (among other contexts) isolated parallel computations that may
41 : dl 1.18 * generate subtasks. Class {@code SplittableRandom} supports methods for
42 : jsr166 1.3 * producing pseudorandom numbers of type {@code int}, {@code long},
43 : dl 1.1 * and {@code double} with similar usages as for class
44 : jsr166 1.9 * {@link java.util.Random} but differs in the following ways:
45 :     *
46 :     * <ul>
47 : dl 1.1 *
48 :     * <li>Series of generated values pass the DieHarder suite testing
49 :     * independence and uniformity properties of random number generators.
50 :     * (Most recently validated with <a
51 :     * href="http://www.phy.duke.edu/~rgb/General/dieharder.php"> version
52 :     * 3.31.1</a>.) These tests validate only the methods for certain
53 :     * types and ranges, but similar properties are expected to hold, at
54 : dl 1.11 * least approximately, for others as well. The <em>period</em>
55 :     * (length of any series of generated values before it repeats) is at
56 :     * least 2<sup>64</sup>. </li>
57 : dl 1.1 *
58 :     * <li> Method {@link #split} constructs and returns a new
59 :     * SplittableRandom instance that shares no mutable state with the
60 : dl 1.7 * current instance. However, with very high probability, the
61 :     * values collectively generated by the two objects have the same
62 : dl 1.1 * statistical properties as if the same quantity of values were
63 :     * generated by a single thread using a single {@code
64 :     * SplittableRandom} object. </li>
65 :     *
66 :     * <li>Instances of SplittableRandom are <em>not</em> thread-safe.
67 :     * They are designed to be split, not shared, across threads. For
68 :     * example, a {@link java.util.concurrent.ForkJoinTask
69 :     * fork/join-style} computation using random numbers might include a
70 :     * construction of the form {@code new
71 :     * Subtask(aSplittableRandom.split()).fork()}.
72 :     *
73 :     * <li>This class provides additional methods for generating random
74 :     * streams, that employ the above techniques when used in {@code
75 :     * stream.parallel()} mode.</li>
76 :     *
77 :     * </ul>
78 :     *
79 : dl 1.18 * <p>Instances of {@code SplittableRandom} are not cryptographically
80 :     * secure. Consider instead using {@link java.security.SecureRandom}
81 :     * in security-sensitive applications. Additionally,
82 :     * default-constructed instances do not use a cryptographically random
83 :     * seed unless the {@linkplain System#getProperty system property}
84 :     * {@code java.util.secureRandomSeed} is set to {@code true}.
85 :     *
86 : dl 1.1 * @author Guy Steele
87 : dl 1.2 * @author Doug Lea
88 : dl 1.1 * @since 1.8
89 :     */
90 : dl 1.23 public final class SplittableRandom {
91 : dl 1.1
92 :     /*
93 :     * Implementation Overview.
94 :     *
95 :     * This algorithm was inspired by the "DotMix" algorithm by
96 :     * Leiserson, Schardl, and Sukha "Deterministic Parallel
97 :     * Random-Number Generation for Dynamic-Multithreading Platforms",
98 : dl 1.15 * PPoPP 2012, as well as those in "Parallel random numbers: as
99 :     * easy as 1, 2, 3" by Salmon, Morae, Dror, and Shaw, SC 2011. It
100 :     * differs mainly in simplifying and cheapening operations.
101 :     *
102 :     * The primary update step (method nextSeed()) is to add a
103 :     * constant ("gamma") to the current (64 bit) seed, forming a
104 :     * simple sequence. The seed and the gamma values for any two
105 :     * SplittableRandom instances are highly likely to be different.
106 :     *
107 :     * Methods nextLong, nextInt, and derivatives do not return the
108 :     * sequence (seed) values, but instead a hash-like bit-mix of
109 :     * their bits, producing more independently distributed sequences.
110 : dl 1.21 * For nextLong, the mix64 function is based on David Stafford's
111 :     * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
112 :     * "Mix13" variant of the "64-bit finalizer" function in Austin
113 :     * Appleby's MurmurHash3 algorithm See
114 :     * http://code.google.com/p/smhasher/wiki/MurmurHash3 . The mix32
115 :     * function is based on Stafford's Mix04 mix function, but returns
116 :     * the upper 32 bits cast as int.
117 : dl 1.15 *
118 :     * The split operation uses the current generator to form the seed
119 :     * and gamma for another SplittableRandom. To conservatively
120 :     * avoid potential correlations between seed and value generation,
121 : dl 1.21 * gamma selection (method mixGamma) uses different
122 :     * (Murmurhash3's) mix constants. To avoid potential weaknesses
123 :     * in bit-mixing transformations, we restrict gammas to odd values
124 :     * with at least 24 0-1 or 1-0 bit transitions. Rather than
125 :     * rejecting candidates with too few or too many bits set, method
126 :     * mixGamma flips some bits (which has the effect of mapping at
127 :     * most 4 to any given gamma value). This reduces the effective
128 :     * set of 64bit odd gamma values by about 2%, and serves as an
129 : dl 1.15 * automated screening for sequence constant selection that is
130 :     * left as an empirical decision in some other hashing and crypto
131 :     * algorithms.
132 :     *
133 :     * The resulting generator thus transforms a sequence in which
134 :     * (typically) many bits change on each step, with an inexpensive
135 :     * mixer with good (but less than cryptographically secure)
136 :     * avalanching.
137 :     *
138 :     * The default (no-argument) constructor, in essence, invokes
139 : dl 1.21 * split() for a common "defaultGen" SplittableRandom. Unlike
140 :     * other cases, this split must be performed in a thread-safe
141 :     * manner, so we use an AtomicLong to represent the seed rather
142 :     * than use an explicit SplittableRandom. To bootstrap the
143 :     * defaultGen, we start off using a seed based on current time and
144 :     * network interface address unless the java.util.secureRandomSeed
145 :     * property is set. This serves as a slimmed-down (and insecure)
146 :     * variant of SecureRandom that also avoids stalls that may occur
147 :     * when using /dev/random.
148 : dl 1.15 *
149 :     * It is a relatively simple matter to apply the basic design here
150 :     * to use 128 bit seeds. However, emulating 128bit arithmetic and
151 :     * carrying around twice the state add more overhead than appears
152 :     * warranted for current usages.
153 : dl 1.13 *
154 : dl 1.15 * File organization: First the non-public methods that constitute
155 :     * the main algorithm, then the main public methods, followed by
156 :     * some custom spliterator classes needed for stream methods.
157 : dl 1.1 */
158 :    
159 :     /**
160 : dl 1.21 * The golden ratio scaled to 64bits, used as the initial gamma
161 :     * value for (unsplit) SplittableRandoms.
162 : dl 1.1 */
163 : dl 1.21 private static final long GOLDEN_GAMMA = 0x9e3779b97f4a7c15L;
164 : dl 1.11
165 :     /**
166 : dl 1.5 * The least non-zero value returned by nextDouble(). This value
167 : dl 1.7 * is scaled by a random value of 53 bits to produce a result.
168 : dl 1.5 */
169 : dl 1.21 private static final double DOUBLE_ULP = 1.0 / (1L << 53);
170 : dl 1.5
171 :     /**
172 : dl 1.15 * The seed. Updated only via method nextSeed.
173 : dl 1.1 */
174 :     private long seed;
175 :    
176 :     /**
177 : dl 1.15 * The step value.
178 : dl 1.1 */
179 :     private final long gamma;
180 :    
181 :     /**
182 : dl 1.15 * Internal constructor used by all others except default constructor.
183 : dl 1.1 */
184 : dl 1.15 private SplittableRandom(long seed, long gamma) {
185 :     this.seed = seed;
186 :     this.gamma = gamma;
187 : dl 1.1 }
188 :    
189 :     /**
190 : dl 1.21 * Computes Stafford variant 13 of 64bit mix function.
191 : dl 1.1 */
192 :     private static long mix64(long z) {
193 : dl 1.21 z *= 0xbf58476d1ce4e5b9L;
194 :     z = (z ^ (z >>> 32)) * 0x94d049bb133111ebL;
195 :     return z ^ (z >>> 32);
196 :     }
197 :    
198 : dl 1.1 /**
199 : dl 1.21 * Returns the 32 high bits of Stafford variant 4 mix64 function as int.
200 : dl 1.1 */
201 :     private static int mix32(long z) {
202 : dl 1.21 z *= 0x62a9d9ed799705f5L;
203 :     return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32);
204 : dl 1.1 }
205 :    
206 :     /**
207 : dl 1.15 * Returns the gamma value to use for a new split instance.
208 : dl 1.13 */
209 : dl 1.21 private static long mixGamma(long z) {
210 :     z *= 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
211 :     z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
212 :     z = (z ^ (z >>> 33)) | 1L; // force to be odd
213 :     int n = Long.bitCount(z ^ (z >>> 1)); // ensure enough transitions
214 :     return (n < 24) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
215 : dl 1.13 }
216 :    
217 :     /**
218 : dl 1.15 * Adds gamma to seed.
219 : dl 1.7 */
220 : dl 1.15 private long nextSeed() {
221 :     return seed += gamma;
222 : dl 1.7 }
223 :    
224 :     /**
225 : dl 1.15 * The seed generator for default constructors.
226 : dl 1.7 */
227 : dl 1.21 private static final AtomicLong defaultGen = new AtomicLong(initialSeed());
228 : dl 1.7
229 : dl 1.18 private static long initialSeed() {
230 : dl 1.21 String pp = java.security.AccessController.doPrivileged(
231 :     new sun.security.action.GetPropertyAction(
232 :     "java.util.secureRandomSeed"));
233 :     if (pp != null && pp.equalsIgnoreCase("true")) {
234 :     byte[] seedBytes = java.security.SecureRandom.getSeed(8);
235 :     long s = (long)(seedBytes[0]) & 0xffL;
236 :     for (int i = 1; i < 8; ++i)
237 :     s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
238 :     return s;
239 : dl 1.18 }
240 : dl 1.21 long h = 0L;
241 : jsr166 1.17 try {
242 : dl 1.21 Enumeration<NetworkInterface> ifcs =
243 : dl 1.23 NetworkInterface.getNetworkInterfaces();
244 : dl 1.21 boolean retry = false; // retry once if getHardwareAddress is null
245 :     while (ifcs.hasMoreElements()) {
246 :     NetworkInterface ifc = ifcs.nextElement();
247 :     if (!ifc.isVirtual()) { // skip fake addresses
248 :     byte[] bs = ifc.getHardwareAddress();
249 :     if (bs != null) {
250 :     int n = bs.length;
251 :     int m = Math.min(n >>> 1, 4);
252 :     for (int i = 0; i < m; ++i)
253 :     h = (h << 16) ^ (bs[i] << 8) ^ bs[n-1-i];
254 :     if (m < 4)
255 :     h = (h << 8) ^ bs[n-1-m];
256 :     h = mix64(h);
257 :     break;
258 :     }
259 :     else if (!retry)
260 :     retry = true;
261 :     else
262 :     break;
263 :     }
264 :     }
265 : dl 1.18 } catch (Exception ignore) {
266 : jsr166 1.17 }
267 : dl 1.21 return (h ^ mix64(System.currentTimeMillis()) ^
268 : dl 1.18 mix64(System.nanoTime()));
269 : dl 1.1 }
270 :    
271 : dl 1.15 // IllegalArgumentException messages
272 :     static final String BadBound = "bound must be positive";
273 :     static final String BadRange = "bound must be greater than origin";
274 :     static final String BadSize = "size must be non-negative";
275 : dl 1.12
276 : dl 1.1 /*
277 :     * Internal versions of nextX methods used by streams, as well as
278 :     * the public nextX(origin, bound) methods. These exist mainly to
279 :     * avoid the need for multiple versions of stream spliterators
280 :     * across the different exported forms of streams.
281 :     */
282 :    
283 :     /**
284 :     * The form of nextLong used by LongStream Spliterators. If
285 :     * origin is greater than bound, acts as unbounded form of
286 :     * nextLong, else as bounded form.
287 :     *
288 :     * @param origin the least value, unless greater than bound
289 :     * @param bound the upper bound (exclusive), must not equal origin
290 :     * @return a pseudorandom value
291 :     */
292 :     final long internalNextLong(long origin, long bound) {
293 :     /*
294 :     * Four Cases:
295 :     *
296 :     * 1. If the arguments indicate unbounded form, act as
297 :     * nextLong().
298 :     *
299 :     * 2. If the range is an exact power of two, apply the
300 :     * associated bit mask.
301 :     *
302 :     * 3. If the range is positive, loop to avoid potential bias
303 :     * when the implicit nextLong() bound (2<sup>64</sup>) is not
304 :     * evenly divisible by the range. The loop rejects candidates
305 :     * computed from otherwise over-represented values. The
306 :     * expected number of iterations under an ideal generator
307 : dl 1.4 * varies from 1 to 2, depending on the bound. The loop itself
308 :     * takes an unlovable form. Because the first candidate is
309 :     * already available, we need a break-in-the-middle
310 :     * construction, which is concisely but cryptically performed
311 :     * within the while-condition of a body-less for loop.
312 : dl 1.1 *
313 :     * 4. Otherwise, the range cannot be represented as a positive
314 : dl 1.4 * long. The loop repeatedly generates unbounded longs until
315 :     * obtaining a candidate meeting constraints (with an expected
316 :     * number of iterations of less than two).
317 : dl 1.1 */
318 :    
319 :     long r = mix64(nextSeed());
320 :     if (origin < bound) {
321 :     long n = bound - origin, m = n - 1;
322 : dl 1.7 if ((n & m) == 0L) // power of two
323 : dl 1.1 r = (r & m) + origin;
324 : dl 1.7 else if (n > 0L) { // reject over-represented candidates
325 : dl 1.1 for (long u = r >>> 1; // ensure nonnegative
326 : dl 1.7 u + m - (r = u % n) < 0L; // rejection check
327 : dl 1.1 u = mix64(nextSeed()) >>> 1) // retry
328 :     ;
329 :     r += origin;
330 :     }
331 : dl 1.7 else { // range not representable as long
332 : dl 1.1 while (r < origin || r >= bound)
333 :     r = mix64(nextSeed());
334 :     }
335 :     }
336 :     return r;
337 :     }
338 :    
339 :     /**
340 :     * The form of nextInt used by IntStream Spliterators.
341 :     * Exactly the same as long version, except for types.
342 :     *
343 :     * @param origin the least value, unless greater than bound
344 :     * @param bound the upper bound (exclusive), must not equal origin
345 :     * @return a pseudorandom value
346 :     */
347 :     final int internalNextInt(int origin, int bound) {
348 :     int r = mix32(nextSeed());
349 :     if (origin < bound) {
350 :     int n = bound - origin, m = n - 1;
351 : dl 1.13 if ((n & m) == 0)
352 : dl 1.1 r = (r & m) + origin;
353 :     else if (n > 0) {
354 :     for (int u = r >>> 1;
355 : dl 1.7 u + m - (r = u % n) < 0;
356 : dl 1.1 u = mix32(nextSeed()) >>> 1)
357 :     ;
358 :     r += origin;
359 :     }
360 :     else {
361 :     while (r < origin || r >= bound)
362 :     r = mix32(nextSeed());
363 :     }
364 :     }
365 :     return r;
366 :     }
367 :    
368 :     /**
369 :     * The form of nextDouble used by DoubleStream Spliterators.
370 :     *
371 :     * @param origin the least value, unless greater than bound
372 :     * @param bound the upper bound (exclusive), must not equal origin
373 :     * @return a pseudorandom value
374 :     */
375 :     final double internalNextDouble(double origin, double bound) {
376 : dl 1.21 double r = (nextLong() >>> 11) * DOUBLE_ULP;
377 : dl 1.1 if (origin < bound) {
378 :     r = r * (bound - origin) + origin;
379 : dl 1.7 if (r >= bound) // correct for rounding
380 : dl 1.1 r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
381 :     }
382 :     return r;
383 :     }
384 :    
385 :     /* ---------------- public methods ---------------- */
386 :    
387 :     /**
388 : dl 1.7 * Creates a new SplittableRandom instance using the specified
389 :     * initial seed. SplittableRandom instances created with the same
390 : dl 1.11 * seed in the same program generate identical sequences of values.
391 : dl 1.1 *
392 :     * @param seed the initial seed
393 :     */
394 :     public SplittableRandom(long seed) {
395 : dl 1.21 this(seed, GOLDEN_GAMMA);
396 : dl 1.1 }
397 :    
398 :     /**
399 :     * Creates a new SplittableRandom instance that is likely to
400 :     * generate sequences of values that are statistically independent
401 :     * of those of any other instances in the current program; and
402 :     * may, and typically does, vary across program invocations.
403 :     */
404 : dl 1.21 public SplittableRandom() { // emulate defaultGen.split()
405 : dl 1.23 long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA);
406 : dl 1.21 this.seed = mix64(s);
407 :     this.gamma = mixGamma(s + GOLDEN_GAMMA);
408 : dl 1.1 }
409 :    
410 :     /**
411 :     * Constructs and returns a new SplittableRandom instance that
412 :     * shares no mutable state with this instance. However, with very
413 :     * high probability, the set of values collectively generated by
414 :     * the two objects has the same statistical properties as if the
415 :     * same quantity of values were generated by a single thread using
416 :     * a single SplittableRandom object. Either or both of the two
417 :     * objects may be further split using the {@code split()} method,
418 :     * and the same expected statistical properties apply to the
419 :     * entire set of generators constructed by such recursive
420 :     * splitting.
421 :     *
422 :     * @return the new SplittableRandom instance
423 :     */
424 :     public SplittableRandom split() {
425 : dl 1.21 return new SplittableRandom(nextLong(), mixGamma(nextSeed()));
426 : dl 1.1 }
427 :    
428 :     /**
429 :     * Returns a pseudorandom {@code int} value.
430 :     *
431 : dl 1.7 * @return a pseudorandom {@code int} value
432 : dl 1.1 */
433 :     public int nextInt() {
434 :     return mix32(nextSeed());
435 :     }
436 :    
437 :     /**
438 : dl 1.7 * Returns a pseudorandom {@code int} value between zero (inclusive)
439 : dl 1.1 * and the specified bound (exclusive).
440 :     *
441 : dl 1.18 * @param bound the upper bound (exclusive). Must be positive.
442 : dl 1.7 * @return a pseudorandom {@code int} value between zero
443 : jsr166 1.10 * (inclusive) and the bound (exclusive)
444 : dl 1.16 * @throws IllegalArgumentException if {@code bound} is not positive
445 : dl 1.1 */
446 :     public int nextInt(int bound) {
447 :     if (bound <= 0)
448 : dl 1.15 throw new IllegalArgumentException(BadBound);
449 : dl 1.1 // Specialize internalNextInt for origin 0
450 :     int r = mix32(nextSeed());
451 :     int m = bound - 1;
452 : dl 1.13 if ((bound & m) == 0) // power of two
453 : dl 1.1 r &= m;
454 :     else { // reject over-represented candidates
455 :     for (int u = r >>> 1;
456 : dl 1.7 u + m - (r = u % bound) < 0;
457 : dl 1.1 u = mix32(nextSeed()) >>> 1)
458 :     ;
459 :     }
460 :     return r;
461 :     }
462 :    
463 :     /**
464 :     * Returns a pseudorandom {@code int} value between the specified
465 :     * origin (inclusive) and the specified bound (exclusive).
466 :     *
467 :     * @param origin the least value returned
468 :     * @param bound the upper bound (exclusive)
469 :     * @return a pseudorandom {@code int} value between the origin
470 : jsr166 1.10 * (inclusive) and the bound (exclusive)
471 : dl 1.7 * @throws IllegalArgumentException if {@code origin} is greater than
472 : dl 1.1 * or equal to {@code bound}
473 :     */
474 :     public int nextInt(int origin, int bound) {
475 :     if (origin >= bound)
476 : dl 1.15 throw new IllegalArgumentException(BadRange);
477 : dl 1.1 return internalNextInt(origin, bound);
478 :     }
479 :    
480 :     /**
481 :     * Returns a pseudorandom {@code long} value.
482 :     *
483 : dl 1.7 * @return a pseudorandom {@code long} value
484 : dl 1.1 */
485 :     public long nextLong() {
486 :     return mix64(nextSeed());
487 :     }
488 :    
489 :     /**
490 : dl 1.7 * Returns a pseudorandom {@code long} value between zero (inclusive)
491 : dl 1.1 * and the specified bound (exclusive).
492 :     *
493 : dl 1.18 * @param bound the upper bound (exclusive). Must be positive.
494 : dl 1.7 * @return a pseudorandom {@code long} value between zero
495 : jsr166 1.10 * (inclusive) and the bound (exclusive)
496 : dl 1.16 * @throws IllegalArgumentException if {@code bound} is not positive
497 : dl 1.1 */
498 :     public long nextLong(long bound) {
499 :     if (bound <= 0)
500 : dl 1.15 throw new IllegalArgumentException(BadBound);
501 : dl 1.1 // Specialize internalNextLong for origin 0
502 :     long r = mix64(nextSeed());
503 :     long m = bound - 1;
504 :     if ((bound & m) == 0L) // power of two
505 :     r &= m;
506 :     else { // reject over-represented candidates
507 :     for (long u = r >>> 1;
508 :     u + m - (r = u % bound) < 0L;
509 :     u = mix64(nextSeed()) >>> 1)
510 :     ;
511 :     }
512 :     return r;
513 :     }
514 :    
515 :     /**
516 :     * Returns a pseudorandom {@code long} value between the specified
517 :     * origin (inclusive) and the specified bound (exclusive).
518 :     *
519 :     * @param origin the least value returned
520 :     * @param bound the upper bound (exclusive)
521 :     * @return a pseudorandom {@code long} value between the origin
522 : jsr166 1.10 * (inclusive) and the bound (exclusive)
523 : dl 1.7 * @throws IllegalArgumentException if {@code origin} is greater than
524 : dl 1.1 * or equal to {@code bound}
525 :     */
526 :     public long nextLong(long origin, long bound) {
527 :     if (origin >= bound)
528 : dl 1.15 throw new IllegalArgumentException(BadRange);
529 : dl 1.1 return internalNextLong(origin, bound);
530 :     }
531 :    
532 :     /**
533 : dl 1.7 * Returns a pseudorandom {@code double} value between zero
534 :     * (inclusive) and one (exclusive).
535 : dl 1.1 *
536 : dl 1.7 * @return a pseudorandom {@code double} value between zero
537 : dl 1.18 * (inclusive) and one (exclusive)
538 : dl 1.1 */
539 :     public double nextDouble() {
540 : dl 1.21 return (mix64(nextSeed()) >>> 11) * DOUBLE_ULP;
541 : dl 1.1 }
542 :    
543 :     /**
544 :     * Returns a pseudorandom {@code double} value between 0.0
545 :     * (inclusive) and the specified bound (exclusive).
546 :     *
547 : dl 1.18 * @param bound the upper bound (exclusive). Must be positive.
548 : dl 1.7 * @return a pseudorandom {@code double} value between zero
549 : jsr166 1.10 * (inclusive) and the bound (exclusive)
550 : dl 1.16 * @throws IllegalArgumentException if {@code bound} is not positive
551 : dl 1.1 */
552 :     public double nextDouble(double bound) {
553 : dl 1.7 if (!(bound > 0.0))
554 : dl 1.15 throw new IllegalArgumentException(BadBound);
555 : dl 1.21 double result = (mix64(nextSeed()) >>> 11) * DOUBLE_ULP * bound;
556 : dl 1.1 return (result < bound) ? result : // correct for rounding
557 :     Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
558 :     }
559 :    
560 :     /**
561 : dl 1.7 * Returns a pseudorandom {@code double} value between the specified
562 : dl 1.1 * origin (inclusive) and bound (exclusive).
563 :     *
564 :     * @param origin the least value returned
565 : dl 1.18 * @param bound the upper bound (exclusive)
566 : dl 1.1 * @return a pseudorandom {@code double} value between the origin
567 : jsr166 1.10 * (inclusive) and the bound (exclusive)
568 : dl 1.1 * @throws IllegalArgumentException if {@code origin} is greater than
569 :     * or equal to {@code bound}
570 :     */
571 :     public double nextDouble(double origin, double bound) {
572 : dl 1.7 if (!(origin < bound))
573 : dl 1.15 throw new IllegalArgumentException(BadRange);
574 : dl 1.1 return internalNextDouble(origin, bound);
575 :     }
576 :    
577 : dl 1.11 /**
578 :     * Returns a pseudorandom {@code boolean} value.
579 :     *
580 :     * @return a pseudorandom {@code boolean} value
581 :     */
582 :     public boolean nextBoolean() {
583 :     return mix32(nextSeed()) < 0;
584 :     }
585 :    
586 : dl 1.1 // stream methods, coded in a way intended to better isolate for
587 :     // maintenance purposes the small differences across forms.
588 :    
589 :     /**
590 : dl 1.16 * Returns a stream producing the given {@code streamSize} number
591 :     * of pseudorandom {@code int} values from this generator and/or
592 :     * one split from it.
593 : dl 1.1 *
594 :     * @param streamSize the number of values to generate
595 :     * @return a stream of pseudorandom {@code int} values
596 :     * @throws IllegalArgumentException if {@code streamSize} is
597 : dl 1.7 * less than zero
598 : dl 1.1 */
599 :     public IntStream ints(long streamSize) {
600 :     if (streamSize < 0L)
601 : dl 1.15 throw new IllegalArgumentException(BadSize);
602 : dl 1.1 return StreamSupport.intStream
603 :     (new RandomIntsSpliterator
604 :     (this, 0L, streamSize, Integer.MAX_VALUE, 0),
605 :     false);
606 :     }
607 :    
608 :     /**
609 :     * Returns an effectively unlimited stream of pseudorandom {@code int}
610 : dl 1.16 * values from this generator and/or one split from it.
611 : dl 1.1 *
612 :     * @implNote This method is implemented to be equivalent to {@code
613 :     * ints(Long.MAX_VALUE)}.
614 :     *
615 :     * @return a stream of pseudorandom {@code int} values
616 :     */
617 :     public IntStream ints() {
618 :     return StreamSupport.intStream
619 :     (new RandomIntsSpliterator
620 :     (this, 0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0),
621 :     false);
622 :     }
623 :    
624 :     /**
625 : dl 1.16 * Returns a stream producing the given {@code streamSize} number
626 : dl 1.18 * of pseudorandom {@code int} values from this generator and/or one split
627 :     * from it; each value conforms to the given origin (inclusive) and bound
628 :     * (exclusive).
629 : dl 1.1 *
630 :     * @param streamSize the number of values to generate
631 : dl 1.18 * @param randomNumberOrigin the origin (inclusive) of each random value
632 :     * @param randomNumberBound the bound (exclusive) of each random value
633 : dl 1.1 * @return a stream of pseudorandom {@code int} values,
634 : dl 1.18 * each with the given origin (inclusive) and bound (exclusive)
635 : dl 1.1 * @throws IllegalArgumentException if {@code streamSize} is
636 : dl 1.7 * less than zero, or {@code randomNumberOrigin}
637 : dl 1.1 * is greater than or equal to {@code randomNumberBound}
638 :     */
639 :     public IntStream ints(long streamSize, int randomNumberOrigin,
640 :     int randomNumberBound) {
641 :     if (streamSize < 0L)
642 : dl 1.15 throw new IllegalArgumentException(BadSize);
643 : dl 1.1 if (randomNumberOrigin >= randomNumberBound)
644 : dl 1.15 throw new IllegalArgumentException(BadRange);
645 : dl 1.1 return StreamSupport.intStream
646 :     (new RandomIntsSpliterator
647 :     (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
648 :     false);
649 :     }
650 :    
651 :     /**
652 :     * Returns an effectively unlimited stream of pseudorandom {@code
653 : dl 1.18 * int} values from this generator and/or one split from it; each value
654 :     * conforms to the given origin (inclusive) and bound (exclusive).
655 : dl 1.1 *
656 :     * @implNote This method is implemented to be equivalent to {@code
657 :     * ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
658 :     *
659 : dl 1.18 * @param randomNumberOrigin the origin (inclusive) of each random value
660 :     * @param randomNumberBound the bound (exclusive) of each random value
661 : dl 1.1 * @return a stream of pseudorandom {@code int} values,
662 : dl 1.18 * each with the given origin (inclusive) and bound (exclusive)
663 : dl 1.1 * @throws IllegalArgumentException if {@code randomNumberOrigin}
664 :     * is greater than or equal to {@code randomNumberBound}
665 :     */
666 :     public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
667 :     if (randomNumberOrigin >= randomNumberBound)
668 : dl 1.15 throw new IllegalArgumentException(BadRange);
669 : dl 1.1 return StreamSupport.intStream
670 :     (new RandomIntsSpliterator
671 :     (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
672 :     false);
673 :     }
674 :    
675 :     /**
676 : dl 1.16 * Returns a stream producing the given {@code streamSize} number
677 :     * of pseudorandom {@code long} values from this generator and/or
678 :     * one split from it.
679 : dl 1.1 *
680 :     * @param streamSize the number of values to generate
681 : dl 1.7 * @return a stream of pseudorandom {@code long} values
682 : dl 1.1 * @throws IllegalArgumentException if {@code streamSize} is
683 : dl 1.7 * less than zero
684 : dl 1.1 */
685 :     public LongStream longs(long streamSize) {
686 :     if (streamSize < 0L)
687 : dl 1.15 throw new IllegalArgumentException(BadSize);
688 : dl 1.1 return StreamSupport.longStream
689 :     (new RandomLongsSpliterator
690 :     (this, 0L, streamSize, Long.MAX_VALUE, 0L),
691 :     false);
692 :     }
693 :    
694 :     /**
695 : dl 1.16 * Returns an effectively unlimited stream of pseudorandom {@code
696 :     * long} values from this generator and/or one split from it.
697 : dl 1.1 *
698 :     * @implNote This method is implemented to be equivalent to {@code
699 :     * longs(Long.MAX_VALUE)}.
700 :     *
701 :     * @return a stream of pseudorandom {@code long} values
702 :     */
703 :     public LongStream longs() {
704 :     return StreamSupport.longStream
705 :     (new RandomLongsSpliterator
706 :     (this, 0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L),
707 :     false);
708 :     }
709 :    
710 :     /**
711 : dl 1.7 * Returns a stream producing the given {@code streamSize} number of
712 : dl 1.18 * pseudorandom {@code long} values from this generator and/or one split
713 :     * from it; each value conforms to the given origin (inclusive) and bound
714 :     * (exclusive).
715 : dl 1.1 *
716 :     * @param streamSize the number of values to generate
717 : dl 1.18 * @param randomNumberOrigin the origin (inclusive) of each random value
718 :     * @param randomNumberBound the bound (exclusive) of each random value
719 : dl 1.1 * @return a stream of pseudorandom {@code long} values,
720 : dl 1.18 * each with the given origin (inclusive) and bound (exclusive)
721 : dl 1.1 * @throws IllegalArgumentException if {@code streamSize} is
722 : dl 1.7 * less than zero, or {@code randomNumberOrigin}
723 : dl 1.1 * is greater than or equal to {@code randomNumberBound}
724 :     */
725 :     public LongStream longs(long streamSize, long randomNumberOrigin,
726 :     long randomNumberBound) {
727 :     if (streamSize < 0L)
728 : dl 1.15 throw new IllegalArgumentException(BadSize);
729 : dl 1.1 if (randomNumberOrigin >= randomNumberBound)
730 : dl 1.15 throw new IllegalArgumentException(BadRange);
731 : dl 1.1 return StreamSupport.longStream
732 :     (new RandomLongsSpliterator
733 :     (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
734 :     false);
735 :     }
736 :    
737 :     /**
738 :     * Returns an effectively unlimited stream of pseudorandom {@code
739 : dl 1.18 * long} values from this generator and/or one split from it; each value
740 :     * conforms to the given origin (inclusive) and bound (exclusive).
741 : dl 1.1 *
742 :     * @implNote This method is implemented to be equivalent to {@code
743 :     * longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
744 :     *
745 : dl 1.18 * @param randomNumberOrigin the origin (inclusive) of each random value
746 :     * @param randomNumberBound the bound (exclusive) of each random value
747 : dl 1.1 * @return a stream of pseudorandom {@code long} values,
748 : dl 1.18 * each with the given origin (inclusive) and bound (exclusive)
749 : dl 1.1 * @throws IllegalArgumentException if {@code randomNumberOrigin}
750 :     * is greater than or equal to {@code randomNumberBound}
751 :     */
752 :     public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
753 :     if (randomNumberOrigin >= randomNumberBound)
754 : dl 1.15 throw new IllegalArgumentException(BadRange);
755 : dl 1.1 return StreamSupport.longStream
756 :     (new RandomLongsSpliterator
757 :     (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
758 :     false);
759 :     }
760 :    
761 :     /**
762 : dl 1.7 * Returns a stream producing the given {@code streamSize} number of
763 : dl 1.18 * pseudorandom {@code double} values from this generator and/or one split
764 :     * from it; each value is between zero (inclusive) and one (exclusive).
765 : dl 1.1 *
766 :     * @param streamSize the number of values to generate
767 :     * @return a stream of {@code double} values
768 :     * @throws IllegalArgumentException if {@code streamSize} is
769 : dl 1.7 * less than zero
770 : dl 1.1 */
771 :     public DoubleStream doubles(long streamSize) {
772 :     if (streamSize < 0L)
773 : dl 1.15 throw new IllegalArgumentException(BadSize);
774 : dl 1.1 return StreamSupport.doubleStream
775 :     (new RandomDoublesSpliterator
776 :     (this, 0L, streamSize, Double.MAX_VALUE, 0.0),
777 :     false);
778 :     }
779 :    
780 :     /**
781 :     * Returns an effectively unlimited stream of pseudorandom {@code
782 : dl 1.18 * double} values from this generator and/or one split from it; each value
783 :     * is between zero (inclusive) and one (exclusive).
784 : dl 1.1 *
785 :     * @implNote This method is implemented to be equivalent to {@code
786 :     * doubles(Long.MAX_VALUE)}.
787 :     *
788 :     * @return a stream of pseudorandom {@code double} values
789 :     */
790 :     public DoubleStream doubles() {
791 :     return StreamSupport.doubleStream
792 :     (new RandomDoublesSpliterator
793 :     (this, 0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0),
794 :     false);
795 :     }
796 :    
797 :     /**
798 : dl 1.7 * Returns a stream producing the given {@code streamSize} number of
799 : dl 1.18 * pseudorandom {@code double} values from this generator and/or one split
800 :     * from it; each value conforms to the given origin (inclusive) and bound
801 :     * (exclusive).
802 : dl 1.1 *
803 :     * @param streamSize the number of values to generate
804 : dl 1.18 * @param randomNumberOrigin the origin (inclusive) of each random value
805 :     * @param randomNumberBound the bound (exclusive) of each random value
806 : dl 1.1 * @return a stream of pseudorandom {@code double} values,
807 : dl 1.18 * each with the given origin (inclusive) and bound (exclusive)
808 : dl 1.1 * @throws IllegalArgumentException if {@code streamSize} is
809 : dl 1.18 * less than zero
810 : dl 1.1 * @throws IllegalArgumentException if {@code randomNumberOrigin}
811 :     * is greater than or equal to {@code randomNumberBound}
812 :     */
813 :     public DoubleStream doubles(long streamSize, double randomNumberOrigin,
814 :     double randomNumberBound) {
815 :     if (streamSize < 0L)
816 : dl 1.15 throw new IllegalArgumentException(BadSize);
817 : dl 1.7 if (!(randomNumberOrigin < randomNumberBound))
818 : dl 1.15 throw new IllegalArgumentException(BadRange);
819 : dl 1.1 return StreamSupport.doubleStream
820 :     (new RandomDoublesSpliterator
821 :     (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
822 :     false);
823 :     }
824 :    
825 :     /**
826 :     * Returns an effectively unlimited stream of pseudorandom {@code
827 : dl 1.18 * double} values from this generator and/or one split from it; each value
828 :     * conforms to the given origin (inclusive) and bound (exclusive).
829 : dl 1.1 *
830 :     * @implNote This method is implemented to be equivalent to {@code
831 :     * doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
832 :     *
833 : dl 1.18 * @param randomNumberOrigin the origin (inclusive) of each random value
834 :     * @param randomNumberBound the bound (exclusive) of each random value
835 : dl 1.1 * @return a stream of pseudorandom {@code double} values,
836 : dl 1.18 * each with the given origin (inclusive) and bound (exclusive)
837 : dl 1.1 * @throws IllegalArgumentException if {@code randomNumberOrigin}
838 :     * is greater than or equal to {@code randomNumberBound}
839 :     */
840 :     public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
841 : dl 1.7 if (!(randomNumberOrigin < randomNumberBound))
842 : dl 1.15 throw new IllegalArgumentException(BadRange);
843 : dl 1.1 return StreamSupport.doubleStream
844 :     (new RandomDoublesSpliterator
845 :     (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
846 :     false);
847 :     }
848 :    
849 :     /**
850 :     * Spliterator for int streams. We multiplex the four int
851 : dl 1.7 * versions into one class by treating a bound less than origin as
852 : dl 1.1 * unbounded, and also by treating "infinite" as equivalent to
853 :     * Long.MAX_VALUE. For splits, it uses the standard divide-by-two
854 :     * approach. The long and double versions of this class are
855 :     * identical except for types.
856 :     */
857 : dl 1.11 static final class RandomIntsSpliterator implements Spliterator.OfInt {
858 : dl 1.1 final SplittableRandom rng;
859 :     long index;
860 :     final long fence;
861 :     final int origin;
862 :     final int bound;
863 :     RandomIntsSpliterator(SplittableRandom rng, long index, long fence,
864 :     int origin, int bound) {
865 :     this.rng = rng; this.index = index; this.fence = fence;
866 :     this.origin = origin; this.bound = bound;
867 :     }
868 :    
869 :     public RandomIntsSpliterator trySplit() {
870 :     long i = index, m = (i + fence) >>> 1;
871 :     return (m <= i) ? null :
872 :     new RandomIntsSpliterator(rng.split(), i, index = m, origin, bound);
873 :     }
874 :    
875 :     public long estimateSize() {
876 :     return fence - index;
877 :     }
878 :    
879 :     public int characteristics() {
880 :     return (Spliterator.SIZED | Spliterator.SUBSIZED |
881 : dl 1.4 Spliterator.NONNULL | Spliterator.IMMUTABLE);
882 : dl 1.1 }
883 :    
884 :     public boolean tryAdvance(IntConsumer consumer) {
885 :     if (consumer == null) throw new NullPointerException();
886 :     long i = index, f = fence;
887 :     if (i < f) {
888 :     consumer.accept(rng.internalNextInt(origin, bound));
889 :     index = i + 1;
890 :     return true;
891 :     }
892 :     return false;
893 :     }
894 :    
895 :     public void forEachRemaining(IntConsumer consumer) {
896 :     if (consumer == null) throw new NullPointerException();
897 :     long i = index, f = fence;
898 :     if (i < f) {
899 :     index = f;
900 : dl 1.15 SplittableRandom r = rng;
901 : dl 1.1 int o = origin, b = bound;
902 :     do {
903 : dl 1.15 consumer.accept(r.internalNextInt(o, b));
904 : dl 1.1 } while (++i < f);
905 :     }
906 :     }
907 :     }
908 :    
909 :     /**
910 :     * Spliterator for long streams.
911 :     */
912 : dl 1.11 static final class RandomLongsSpliterator implements Spliterator.OfLong {
913 : dl 1.1 final SplittableRandom rng;
914 :     long index;
915 :     final long fence;
916 :     final long origin;
917 :     final long bound;
918 :     RandomLongsSpliterator(SplittableRandom rng, long index, long fence,
919 :     long origin, long bound) {
920 :     this.rng = rng; this.index = index; this.fence = fence;
921 :     this.origin = origin; this.bound = bound;
922 :     }
923 :    
924 :     public RandomLongsSpliterator trySplit() {
925 :     long i = index, m = (i + fence) >>> 1;
926 :     return (m <= i) ? null :
927 :     new RandomLongsSpliterator(rng.split(), i, index = m, origin, bound);
928 :     }
929 :    
930 :     public long estimateSize() {
931 :     return fence - index;
932 :     }
933 :    
934 :     public int characteristics() {
935 :     return (Spliterator.SIZED | Spliterator.SUBSIZED |
936 : dl 1.4 Spliterator.NONNULL | Spliterator.IMMUTABLE);
937 : dl 1.1 }
938 :    
939 :     public boolean tryAdvance(LongConsumer consumer) {
940 :     if (consumer == null) throw new NullPointerException();
941 :     long i = index, f = fence;
942 :     if (i < f) {
943 :     consumer.accept(rng.internalNextLong(origin, bound));
944 :     index = i + 1;
945 :     return true;
946 :     }
947 :     return false;
948 :     }
949 :    
950 :     public void forEachRemaining(LongConsumer consumer) {
951 :     if (consumer == null) throw new NullPointerException();
952 :     long i = index, f = fence;
953 :     if (i < f) {
954 :     index = f;
955 : dl 1.15 SplittableRandom r = rng;
956 : dl 1.1 long o = origin, b = bound;
957 :     do {
958 : dl 1.15 consumer.accept(r.internalNextLong(o, b));
959 : dl 1.1 } while (++i < f);
960 :     }
961 :     }
962 :    
963 :     }
964 :    
965 :     /**
966 :     * Spliterator for double streams.
967 :     */
968 : dl 1.11 static final class RandomDoublesSpliterator implements Spliterator.OfDouble {
969 : dl 1.1 final SplittableRandom rng;
970 :     long index;
971 :     final long fence;
972 :     final double origin;
973 :     final double bound;
974 :     RandomDoublesSpliterator(SplittableRandom rng, long index, long fence,
975 :     double origin, double bound) {
976 :     this.rng = rng; this.index = index; this.fence = fence;
977 :     this.origin = origin; this.bound = bound;
978 :     }
979 :    
980 :     public RandomDoublesSpliterator trySplit() {
981 :     long i = index, m = (i + fence) >>> 1;
982 :     return (m <= i) ? null :
983 :     new RandomDoublesSpliterator(rng.split(), i, index = m, origin, bound);
984 :     }
985 :    
986 :     public long estimateSize() {
987 :     return fence - index;
988 :     }
989 :    
990 :     public int characteristics() {
991 :     return (Spliterator.SIZED | Spliterator.SUBSIZED |
992 : dl 1.4 Spliterator.NONNULL | Spliterator.IMMUTABLE);
993 : dl 1.1 }
994 :    
995 :     public boolean tryAdvance(DoubleConsumer consumer) {
996 :     if (consumer == null) throw new NullPointerException();
997 :     long i = index, f = fence;
998 :     if (i < f) {
999 :     consumer.accept(rng.internalNextDouble(origin, bound));
1000 :     index = i + 1;
1001 :     return true;
1002 :     }
1003 :     return false;
1004 :     }
1005 :    
1006 :     public void forEachRemaining(DoubleConsumer consumer) {
1007 :     if (consumer == null) throw new NullPointerException();
1008 :     long i = index, f = fence;
1009 :     if (i < f) {
1010 :     index = f;
1011 : dl 1.15 SplittableRandom r = rng;
1012 : dl 1.1 double o = origin, b = bound;
1013 :     do {
1014 : dl 1.15 consumer.accept(r.internalNextDouble(o, b));
1015 : dl 1.1 } while (++i < f);
1016 :     }
1017 :     }
1018 :     }
1019 :    
1020 :     }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8