25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
+ |
import java.security.SecureRandom; |
29 |
|
import java.util.concurrent.atomic.AtomicLong; |
30 |
|
import java.util.Spliterator; |
31 |
|
import java.util.function.IntConsumer; |
36 |
|
import java.util.stream.LongStream; |
37 |
|
import java.util.stream.DoubleStream; |
38 |
|
|
38 |
– |
|
39 |
|
/** |
40 |
|
* A generator of uniform pseudorandom values applicable for use in |
41 |
|
* (among other contexts) isolated parallel computations that may |
42 |
|
* generate subtasks. Class SplittableRandom supports methods for |
43 |
|
* producing pseudorandom numbers of type {@code int}, {@code long}, |
44 |
|
* and {@code double} with similar usages as for class |
45 |
< |
* {@link java.util.Random} but differs in the following ways: <ul> |
45 |
> |
* {@link java.util.Random} but differs in the following ways: |
46 |
> |
* |
47 |
> |
* <ul> |
48 |
|
* |
49 |
|
* <li>Series of generated values pass the DieHarder suite testing |
50 |
|
* independence and uniformity properties of random number generators. |
52 |
|
* href="http://www.phy.duke.edu/~rgb/General/dieharder.php"> version |
53 |
|
* 3.31.1</a>.) These tests validate only the methods for certain |
54 |
|
* types and ranges, but similar properties are expected to hold, at |
55 |
< |
* least approximately, for others as well. </li> |
55 |
> |
* least approximately, for others as well. The <em>period</em> |
56 |
> |
* (length of any series of generated values before it repeats) is at |
57 |
> |
* least 2<sup>64</sup>. </li> |
58 |
|
* |
59 |
|
* <li> Method {@link #split} constructs and returns a new |
60 |
|
* SplittableRandom instance that shares no mutable state with the |
61 |
< |
* current instance. However, with very high probability, the set of |
62 |
< |
* values collectively generated by the two objects has the same |
61 |
> |
* current instance. However, with very high probability, the |
62 |
> |
* values collectively generated by the two objects have the same |
63 |
|
* statistical properties as if the same quantity of values were |
64 |
|
* generated by a single thread using a single {@code |
65 |
|
* SplittableRandom} object. </li> |
101 |
|
* Random-Number Generation for Dynamic-Multithreading Platforms", |
102 |
|
* PPoPP 2012, but improves and extends it in several ways. |
103 |
|
* |
104 |
< |
* The primary update step is simply to add a constant ("gamma") |
105 |
< |
* to the current seed, modulo a prime ("George"). However, the |
106 |
< |
* nextLong and nextInt methods do not return this value, but |
107 |
< |
* instead the results of bit-mixing transformations that produce |
108 |
< |
* more uniformly distributed sequences. |
104 |
> |
* The primary update step (see method nextSeed()) is simply to |
105 |
> |
* add a constant ("gamma") to the current seed, modulo a prime |
106 |
> |
* ("George"). However, the nextLong and nextInt methods do not |
107 |
> |
* return this value, but instead the results of bit-mixing |
108 |
> |
* transformations that produce more uniformly distributed |
109 |
> |
* sequences. |
110 |
|
* |
111 |
|
* "George" is the otherwise nameless (because it cannot be |
112 |
|
* represented) prime number 2^64+13. Using a prime number larger |
122 |
|
* this generator as nextSplit, and uses mix64(nextSplit) as its |
123 |
|
* own gamma value. Computations of gammas themselves use a fixed |
124 |
|
* constant as the second argument to the addGammaModGeorge |
125 |
< |
* function, GAMMA_GAMMA, a "genuinely random" number from a |
126 |
< |
* radioactive decay reading (obtained from |
127 |
< |
* http://www.fourmilab.ch/hotbits/) meeting the above range |
128 |
< |
* constraint. Using a fixed constant maintains the invariant that |
124 |
< |
* the value of gamma is the same for every instance that is at |
125 |
< |
* the same split-distance from their common root. (Note: there is |
126 |
< |
* nothing especially magic about obtaining this constant from a |
127 |
< |
* "truly random" physical source rather than just choosing one |
128 |
< |
* arbitrarily; using "hotbits" was merely an aesthetically pleasing |
129 |
< |
* choice. In either case, good statistical behavior of the |
130 |
< |
* algorithm should be, and was, verified by using the DieHarder |
131 |
< |
* test suite.) |
125 |
> |
* function, GAMMA_GAMMA. The value of GAMMA_GAMMA is arbitrary |
126 |
> |
* (except must be at least 13), but because it serves as the base |
127 |
> |
* of split sequences, should be subject to validation of |
128 |
> |
* consequent random number quality metrics. |
129 |
|
* |
130 |
|
* The mix64 bit-mixing function called by nextLong and other |
131 |
|
* methods computes the same value as the "64-bit finalizer" |
151 |
|
* SplittableRandom. Unlike other cases, this split must be |
152 |
|
* performed in a thread-safe manner. We use |
153 |
|
* AtomicLong.compareAndSet as the (typically) most efficient |
154 |
< |
* mechanism. To bootstrap, we start off using System.nanotime(), |
155 |
< |
* and update using another "genuinely random" constant |
154 |
> |
* mechanism. To bootstrap, we start off using a SecureRandom |
155 |
> |
* initial default seed, and update using a fixed |
156 |
|
* DEFAULT_SEED_GAMMA. The default constructor uses GAMMA_GAMMA, |
157 |
|
* not 0, for its splitSeed argument (addGammaModGeorge(0, |
158 |
|
* GAMMA_GAMMA) == GAMMA_GAMMA) to reflect that each is split from |
161 |
|
*/ |
162 |
|
|
163 |
|
/** |
164 |
< |
* The "genuinely random" value for producing new gamma values. |
165 |
< |
* The value is arbitrary, subject to the requirement that it be |
166 |
< |
* greater or equal to 13. |
164 |
> |
* The value for producing new gamma values. Must be greater or |
165 |
> |
* equal to 13. Otherwise, the value is arbitrary subject to |
166 |
> |
* validation of the resulting statistical quality of splits. |
167 |
|
*/ |
168 |
|
private static final long GAMMA_GAMMA = 0xF2281E2DBA6606F3L; |
169 |
|
|
170 |
|
/** |
171 |
< |
* The "genuinely random" seed update value for default constructors. |
172 |
< |
* The value is arbitrary, subject to the requirement that it be |
176 |
< |
* greater or equal to 13. |
171 |
> |
* The seed update value for default constructors. Must be |
172 |
> |
* greater or equal to 13. Otherwise, the value is arbitrary. |
173 |
|
*/ |
174 |
|
private static final long DEFAULT_SEED_GAMMA = 0xBD24B73A95FB84D9L; |
175 |
|
|
176 |
|
/** |
177 |
+ |
* The value 13 with 64bit sign bit set. Used in the signed |
178 |
+ |
* comparison in addGammaModGeorge. |
179 |
+ |
*/ |
180 |
+ |
private static final long BOTTOM13 = 0x800000000000000DL; |
181 |
+ |
|
182 |
+ |
/** |
183 |
+ |
* The least non-zero value returned by nextDouble(). This value |
184 |
+ |
* is scaled by a random value of 53 bits to produce a result. |
185 |
+ |
*/ |
186 |
+ |
private static final double DOUBLE_UNIT = 1.0 / (1L << 53); |
187 |
+ |
|
188 |
+ |
/** |
189 |
|
* The next seed for default constructors. |
190 |
|
*/ |
191 |
|
private static final AtomicLong defaultSeedGenerator = |
192 |
< |
new AtomicLong(System.nanoTime()); |
192 |
> |
new AtomicLong(getInitialDefaultSeed()); |
193 |
|
|
194 |
|
/** |
195 |
|
* The seed, updated only via method nextSeed. |
208 |
|
private final long nextSplit; |
209 |
|
|
210 |
|
/** |
203 |
– |
* Internal constructor used by all other constructors and by |
204 |
– |
* method split. Establishes the initial seed for this instance, |
205 |
– |
* and uses the given splitSeed to establish gamma, as well as the |
206 |
– |
* nextSplit to use by this instance. |
207 |
– |
*/ |
208 |
– |
private SplittableRandom(long seed, long splitSeed) { |
209 |
– |
this.seed = seed; |
210 |
– |
long s = splitSeed, g; |
211 |
– |
do { // ensure gamma >= 13, considered as an unsigned integer |
212 |
– |
s = addGammaModGeorge(s, GAMMA_GAMMA); |
213 |
– |
g = mix64(s); |
214 |
– |
} while (Long.compareUnsigned(g, 13L) < 0); |
215 |
– |
this.gamma = g; |
216 |
– |
this.nextSplit = s; |
217 |
– |
} |
218 |
– |
|
219 |
– |
/** |
211 |
|
* Adds the given gamma value, g, to the given seed value s, mod |
212 |
|
* George (2^64+13). We regard s and g as unsigned values |
213 |
|
* (ranging from 0 to 2^64-1). We add g to s either once or twice |
217 |
|
* George < 2^64; thus we need only a conditional, not a loop, |
218 |
|
* to be sure of getting a representable value. |
219 |
|
* |
220 |
< |
* @param s a seed value |
220 |
> |
* Because Java comparison operators are signed, we implement this |
221 |
> |
* by conceptually offsetting seed values downwards by 2^63, so |
222 |
> |
* 0..13 is represented as Long.MIN_VALUE..BOTTOM13. |
223 |
> |
* |
224 |
> |
* @param s a seed value, viewed as a signed long |
225 |
|
* @param g a gamma value, 13 <= g (as unsigned) |
226 |
|
*/ |
227 |
|
private static long addGammaModGeorge(long s, long g) { |
228 |
|
long p = s + g; |
229 |
< |
if (Long.compareUnsigned(p, g) >= 0) |
235 |
< |
return p; |
236 |
< |
long q = p - 13L; |
237 |
< |
return (Long.compareUnsigned(p, 13L) >= 0) ? q : (q + g); |
238 |
< |
} |
239 |
< |
|
240 |
< |
/** |
241 |
< |
* Updates in-place and returns seed. |
242 |
< |
* See above for explanation. |
243 |
< |
*/ |
244 |
< |
private long nextSeed() { |
245 |
< |
return seed = addGammaModGeorge(seed, gamma); |
229 |
> |
return (p >= s) ? p : ((p >= BOTTOM13) ? p : p + g) - 13L; |
230 |
|
} |
231 |
|
|
232 |
|
/** |
253 |
|
} |
254 |
|
|
255 |
|
/** |
256 |
< |
* Atomically updates and returns next seed for default constructor |
256 |
> |
* Internal constructor used by all other constructors and by |
257 |
> |
* method split. Establishes the initial seed for this instance, |
258 |
> |
* and uses the given splitSeed to establish gamma, as well as the |
259 |
> |
* nextSplit to use by this instance. The loop to skip ineligible |
260 |
> |
* gammas very rarely iterates, and does so at most 13 times. |
261 |
> |
*/ |
262 |
> |
private SplittableRandom(long seed, long splitSeed) { |
263 |
> |
this.seed = seed; |
264 |
> |
long s = splitSeed, g; |
265 |
> |
do { // ensure gamma >= 13, considered as an unsigned integer |
266 |
> |
s = addGammaModGeorge(s, GAMMA_GAMMA); |
267 |
> |
g = mix64(s); |
268 |
> |
} while (g >= 0L && g < 13L); |
269 |
> |
this.gamma = g; |
270 |
> |
this.nextSplit = s; |
271 |
> |
} |
272 |
> |
|
273 |
> |
/** |
274 |
> |
* Updates in-place and returns seed. |
275 |
> |
* See above for explanation. |
276 |
> |
*/ |
277 |
> |
private long nextSeed() { |
278 |
> |
return seed = addGammaModGeorge(seed, gamma); |
279 |
> |
} |
280 |
> |
|
281 |
> |
/** |
282 |
> |
* Atomically updates and returns next seed for default constructor. |
283 |
|
*/ |
284 |
|
private static long nextDefaultSeed() { |
285 |
|
long oldSeed, newSeed; |
290 |
|
return mix64(newSeed); |
291 |
|
} |
292 |
|
|
293 |
+ |
/** |
294 |
+ |
* Returns an initial default seed. |
295 |
+ |
*/ |
296 |
+ |
private static long getInitialDefaultSeed() { |
297 |
+ |
byte[] seedBytes = java.security.SecureRandom.getSeed(8); |
298 |
+ |
long s = (long)(seedBytes[0]) & 0xffL; |
299 |
+ |
for (int i = 1; i < 8; ++i) |
300 |
+ |
s = (s << 8) | ((long)(seedBytes[i]) & 0xffL); |
301 |
+ |
return s; |
302 |
+ |
} |
303 |
+ |
|
304 |
|
/* |
305 |
|
* Internal versions of nextX methods used by streams, as well as |
306 |
|
* the public nextX(origin, bound) methods. These exist mainly to |
332 |
|
* evenly divisible by the range. The loop rejects candidates |
333 |
|
* computed from otherwise over-represented values. The |
334 |
|
* expected number of iterations under an ideal generator |
335 |
< |
* varies from 1 to 2, depending on the bound. |
335 |
> |
* varies from 1 to 2, depending on the bound. The loop itself |
336 |
> |
* takes an unlovable form. Because the first candidate is |
337 |
> |
* already available, we need a break-in-the-middle |
338 |
> |
* construction, which is concisely but cryptically performed |
339 |
> |
* within the while-condition of a body-less for loop. |
340 |
|
* |
341 |
|
* 4. Otherwise, the range cannot be represented as a positive |
342 |
< |
* long. Repeatedly generate unbounded longs until obtaining |
343 |
< |
* a candidate meeting constraints (with an expected number of |
344 |
< |
* iterations of less than two). |
342 |
> |
* long. The loop repeatedly generates unbounded longs until |
343 |
> |
* obtaining a candidate meeting constraints (with an expected |
344 |
> |
* number of iterations of less than two). |
345 |
|
*/ |
346 |
|
|
347 |
|
long r = mix64(nextSeed()); |
348 |
|
if (origin < bound) { |
349 |
|
long n = bound - origin, m = n - 1; |
350 |
< |
if ((n & m) == 0L) // power of two |
350 |
> |
if ((n & m) == 0L) // power of two |
351 |
|
r = (r & m) + origin; |
352 |
< |
else if (n > 0) { // reject over-represented candidates |
352 |
> |
else if (n > 0L) { // reject over-represented candidates |
353 |
|
for (long u = r >>> 1; // ensure nonnegative |
354 |
< |
u + m - (r = u % n) < 0L; // reject |
354 |
> |
u + m - (r = u % n) < 0L; // rejection check |
355 |
|
u = mix64(nextSeed()) >>> 1) // retry |
356 |
|
; |
357 |
|
r += origin; |
358 |
|
} |
359 |
< |
else { // range not representable as long |
359 |
> |
else { // range not representable as long |
360 |
|
while (r < origin || r >= bound) |
361 |
|
r = mix64(nextSeed()); |
362 |
|
} |
380 |
|
r = (r & m) + origin; |
381 |
|
else if (n > 0) { |
382 |
|
for (int u = r >>> 1; |
383 |
< |
u + m - (r = u % n) < 0L; |
383 |
> |
u + m - (r = u % n) < 0; |
384 |
|
u = mix32(nextSeed()) >>> 1) |
385 |
|
; |
386 |
|
r += origin; |
401 |
|
* @return a pseudorandom value |
402 |
|
*/ |
403 |
|
final double internalNextDouble(double origin, double bound) { |
404 |
< |
long bits = (1023L << 52) | (nextLong() >>> 12); |
380 |
< |
double r = Double.longBitsToDouble(bits) - 1.0; |
404 |
> |
double r = (nextLong() >>> 11) * DOUBLE_UNIT; |
405 |
|
if (origin < bound) { |
406 |
|
r = r * (bound - origin) + origin; |
407 |
< |
if (r == bound) // correct for rounding |
407 |
> |
if (r >= bound) // correct for rounding |
408 |
|
r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1); |
409 |
|
} |
410 |
|
return r; |
413 |
|
/* ---------------- public methods ---------------- */ |
414 |
|
|
415 |
|
/** |
416 |
< |
* Creates a new SplittableRandom instance using the given initial |
417 |
< |
* seed. Two SplittableRandom instances created with the same seed |
418 |
< |
* generate identical sequences of values. |
416 |
> |
* Creates a new SplittableRandom instance using the specified |
417 |
> |
* initial seed. SplittableRandom instances created with the same |
418 |
> |
* seed in the same program generate identical sequences of values. |
419 |
|
* |
420 |
|
* @param seed the initial seed |
421 |
|
*/ |
454 |
|
/** |
455 |
|
* Returns a pseudorandom {@code int} value. |
456 |
|
* |
457 |
< |
* @return a pseudorandom value |
457 |
> |
* @return a pseudorandom {@code int} value |
458 |
|
*/ |
459 |
|
public int nextInt() { |
460 |
|
return mix32(nextSeed()); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
< |
* Returns a pseudorandom {@code int} value between 0 (inclusive) |
464 |
> |
* Returns a pseudorandom {@code int} value between zero (inclusive) |
465 |
|
* and the specified bound (exclusive). |
466 |
|
* |
467 |
|
* @param bound the bound on the random number to be returned. Must be |
468 |
|
* positive. |
469 |
< |
* @return a pseudorandom {@code int} value between {@code 0} |
470 |
< |
* (inclusive) and the bound (exclusive). |
471 |
< |
* @exception IllegalArgumentException if the bound is not positive |
469 |
> |
* @return a pseudorandom {@code int} value between zero |
470 |
> |
* (inclusive) and the bound (exclusive) |
471 |
> |
* @throws IllegalArgumentException if the bound is less than zero |
472 |
|
*/ |
473 |
|
public int nextInt(int bound) { |
474 |
|
if (bound <= 0) |
480 |
|
r &= m; |
481 |
|
else { // reject over-represented candidates |
482 |
|
for (int u = r >>> 1; |
483 |
< |
u + m - (r = u % bound) < 0L; |
483 |
> |
u + m - (r = u % bound) < 0; |
484 |
|
u = mix32(nextSeed()) >>> 1) |
485 |
|
; |
486 |
|
} |
494 |
|
* @param origin the least value returned |
495 |
|
* @param bound the upper bound (exclusive) |
496 |
|
* @return a pseudorandom {@code int} value between the origin |
497 |
< |
* (inclusive) and the bound (exclusive). |
498 |
< |
* @exception IllegalArgumentException if {@code origin} is greater than |
497 |
> |
* (inclusive) and the bound (exclusive) |
498 |
> |
* @throws IllegalArgumentException if {@code origin} is greater than |
499 |
|
* or equal to {@code bound} |
500 |
|
*/ |
501 |
|
public int nextInt(int origin, int bound) { |
507 |
|
/** |
508 |
|
* Returns a pseudorandom {@code long} value. |
509 |
|
* |
510 |
< |
* @return a pseudorandom value |
510 |
> |
* @return a pseudorandom {@code long} value |
511 |
|
*/ |
512 |
|
public long nextLong() { |
513 |
|
return mix64(nextSeed()); |
514 |
|
} |
515 |
|
|
516 |
|
/** |
517 |
< |
* Returns a pseudorandom {@code long} value between 0 (inclusive) |
517 |
> |
* Returns a pseudorandom {@code long} value between zero (inclusive) |
518 |
|
* and the specified bound (exclusive). |
519 |
|
* |
520 |
|
* @param bound the bound on the random number to be returned. Must be |
521 |
|
* positive. |
522 |
< |
* @return a pseudorandom {@code long} value between {@code 0} |
523 |
< |
* (inclusive) and the bound (exclusive). |
524 |
< |
* @exception IllegalArgumentException if the bound is not positive |
522 |
> |
* @return a pseudorandom {@code long} value between zero |
523 |
> |
* (inclusive) and the bound (exclusive) |
524 |
> |
* @throws IllegalArgumentException if {@code bound} is less than zero |
525 |
|
*/ |
526 |
|
public long nextLong(long bound) { |
527 |
|
if (bound <= 0) |
547 |
|
* @param origin the least value returned |
548 |
|
* @param bound the upper bound (exclusive) |
549 |
|
* @return a pseudorandom {@code long} value between the origin |
550 |
< |
* (inclusive) and the bound (exclusive). |
551 |
< |
* @exception IllegalArgumentException if {@code origin} is greater than |
550 |
> |
* (inclusive) and the bound (exclusive) |
551 |
> |
* @throws IllegalArgumentException if {@code origin} is greater than |
552 |
|
* or equal to {@code bound} |
553 |
|
*/ |
554 |
|
public long nextLong(long origin, long bound) { |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
< |
* Returns a pseudorandom {@code double} value between {@code 0.0} |
562 |
< |
* (inclusive) and {@code 1.0} (exclusive). |
561 |
> |
* Returns a pseudorandom {@code double} value between zero |
562 |
> |
* (inclusive) and one (exclusive). |
563 |
|
* |
564 |
< |
* @return a pseudorandom value between {@code 0.0} |
565 |
< |
* (inclusive) and {@code 1.0} (exclusive) |
564 |
> |
* @return a pseudorandom {@code double} value between zero |
565 |
> |
* (inclusive) and one (exclusive) |
566 |
|
*/ |
567 |
|
public double nextDouble() { |
568 |
< |
long bits = (1023L << 52) | (nextLong() >>> 12); |
545 |
< |
return Double.longBitsToDouble(bits) - 1.0; |
568 |
> |
return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT; |
569 |
|
} |
570 |
|
|
571 |
|
/** |
574 |
|
* |
575 |
|
* @param bound the bound on the random number to be returned. Must be |
576 |
|
* positive. |
577 |
< |
* @return a pseudorandom {@code double} value between {@code 0.0} |
578 |
< |
* (inclusive) and the bound (exclusive). |
579 |
< |
* @throws IllegalArgumentException if {@code bound} is not positive |
577 |
> |
* @return a pseudorandom {@code double} value between zero |
578 |
> |
* (inclusive) and the bound (exclusive) |
579 |
> |
* @throws IllegalArgumentException if {@code bound} is less than zero |
580 |
|
*/ |
581 |
|
public double nextDouble(double bound) { |
582 |
< |
if (bound <= 0.0) |
582 |
> |
if (!(bound > 0.0)) |
583 |
|
throw new IllegalArgumentException("bound must be positive"); |
584 |
< |
double result = nextDouble() * bound; |
584 |
> |
double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound; |
585 |
|
return (result < bound) ? result : // correct for rounding |
586 |
|
Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1); |
587 |
|
} |
588 |
|
|
589 |
|
/** |
590 |
< |
* Returns a pseudorandom {@code double} value between the given |
590 |
> |
* Returns a pseudorandom {@code double} value between the specified |
591 |
|
* origin (inclusive) and bound (exclusive). |
592 |
|
* |
593 |
|
* @param origin the least value returned |
594 |
|
* @param bound the upper bound |
595 |
|
* @return a pseudorandom {@code double} value between the origin |
596 |
< |
* (inclusive) and the bound (exclusive). |
596 |
> |
* (inclusive) and the bound (exclusive) |
597 |
|
* @throws IllegalArgumentException if {@code origin} is greater than |
598 |
|
* or equal to {@code bound} |
599 |
|
*/ |
600 |
|
public double nextDouble(double origin, double bound) { |
601 |
< |
if (origin >= bound) |
601 |
> |
if (!(origin < bound)) |
602 |
|
throw new IllegalArgumentException("bound must be greater than origin"); |
603 |
|
return internalNextDouble(origin, bound); |
604 |
|
} |
605 |
|
|
606 |
+ |
/** |
607 |
+ |
* Returns a pseudorandom {@code boolean} value. |
608 |
+ |
* |
609 |
+ |
* @return a pseudorandom {@code boolean} value |
610 |
+ |
*/ |
611 |
+ |
public boolean nextBoolean() { |
612 |
+ |
return mix32(nextSeed()) < 0; |
613 |
+ |
} |
614 |
+ |
|
615 |
|
// stream methods, coded in a way intended to better isolate for |
616 |
|
// maintenance purposes the small differences across forms. |
617 |
|
|
618 |
|
/** |
619 |
< |
* Returns a stream with the given {@code streamSize} number of |
619 |
> |
* Returns a stream producing the given {@code streamSize} number of |
620 |
|
* pseudorandom {@code int} values. |
621 |
|
* |
622 |
|
* @param streamSize the number of values to generate |
623 |
|
* @return a stream of pseudorandom {@code int} values |
624 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
625 |
< |
* less than zero |
625 |
> |
* less than zero |
626 |
|
*/ |
627 |
|
public IntStream ints(long streamSize) { |
628 |
|
if (streamSize < 0L) |
635 |
|
|
636 |
|
/** |
637 |
|
* Returns an effectively unlimited stream of pseudorandom {@code int} |
638 |
< |
* values |
638 |
> |
* values. |
639 |
|
* |
640 |
|
* @implNote This method is implemented to be equivalent to {@code |
641 |
|
* ints(Long.MAX_VALUE)}. |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
< |
* Returns a stream with the given {@code streamSize} number of |
653 |
> |
* Returns a stream producing the given {@code streamSize} number of |
654 |
|
* pseudorandom {@code int} values, each conforming to the given |
655 |
|
* origin and bound. |
656 |
|
* |
658 |
|
* @param randomNumberOrigin the origin of each random value |
659 |
|
* @param randomNumberBound the bound of each random value |
660 |
|
* @return a stream of pseudorandom {@code int} values, |
661 |
< |
* each with the given origin and bound. |
661 |
> |
* each with the given origin and bound |
662 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
663 |
< |
* less than zero. |
632 |
< |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
663 |
> |
* less than zero, or {@code randomNumberOrigin} |
664 |
|
* is greater than or equal to {@code randomNumberBound} |
665 |
|
*/ |
666 |
|
public IntStream ints(long streamSize, int randomNumberOrigin, |
685 |
|
* @param randomNumberOrigin the origin of each random value |
686 |
|
* @param randomNumberBound the bound of each random value |
687 |
|
* @return a stream of pseudorandom {@code int} values, |
688 |
< |
* each with the given origin and bound. |
688 |
> |
* each with the given origin and bound |
689 |
|
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
690 |
|
* is greater than or equal to {@code randomNumberBound} |
691 |
|
*/ |
699 |
|
} |
700 |
|
|
701 |
|
/** |
702 |
< |
* Returns a stream with the given {@code streamSize} number of |
702 |
> |
* Returns a stream producing the given {@code streamSize} number of |
703 |
|
* pseudorandom {@code long} values. |
704 |
|
* |
705 |
|
* @param streamSize the number of values to generate |
706 |
< |
* @return a stream of {@code long} values |
706 |
> |
* @return a stream of pseudorandom {@code long} values |
707 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
708 |
< |
* less than zero |
708 |
> |
* less than zero |
709 |
|
*/ |
710 |
|
public LongStream longs(long streamSize) { |
711 |
|
if (streamSize < 0L) |
733 |
|
} |
734 |
|
|
735 |
|
/** |
736 |
< |
* Returns a stream with the given {@code streamSize} number of |
736 |
> |
* Returns a stream producing the given {@code streamSize} number of |
737 |
|
* pseudorandom {@code long} values, each conforming to the |
738 |
|
* given origin and bound. |
739 |
|
* |
741 |
|
* @param randomNumberOrigin the origin of each random value |
742 |
|
* @param randomNumberBound the bound of each random value |
743 |
|
* @return a stream of pseudorandom {@code long} values, |
744 |
< |
* each with the given origin and bound. |
744 |
> |
* each with the given origin and bound |
745 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
746 |
< |
* less than zero. |
716 |
< |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
746 |
> |
* less than zero, or {@code randomNumberOrigin} |
747 |
|
* is greater than or equal to {@code randomNumberBound} |
748 |
|
*/ |
749 |
|
public LongStream longs(long streamSize, long randomNumberOrigin, |
768 |
|
* @param randomNumberOrigin the origin of each random value |
769 |
|
* @param randomNumberBound the bound of each random value |
770 |
|
* @return a stream of pseudorandom {@code long} values, |
771 |
< |
* each with the given origin and bound. |
771 |
> |
* each with the given origin and bound |
772 |
|
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
773 |
|
* is greater than or equal to {@code randomNumberBound} |
774 |
|
*/ |
782 |
|
} |
783 |
|
|
784 |
|
/** |
785 |
< |
* Returns a stream with the given {@code streamSize} number of |
786 |
< |
* pseudorandom {@code double} values, each between {@code 0.0} |
787 |
< |
* (inclusive) and {@code 1.0} (exclusive). |
785 |
> |
* Returns a stream producing the given {@code streamSize} number of |
786 |
> |
* pseudorandom {@code double} values, each between zero |
787 |
> |
* (inclusive) and one (exclusive). |
788 |
|
* |
789 |
|
* @param streamSize the number of values to generate |
790 |
|
* @return a stream of {@code double} values |
791 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
792 |
< |
* less than zero |
792 |
> |
* less than zero |
793 |
|
*/ |
794 |
|
public DoubleStream doubles(long streamSize) { |
795 |
|
if (streamSize < 0L) |
802 |
|
|
803 |
|
/** |
804 |
|
* Returns an effectively unlimited stream of pseudorandom {@code |
805 |
< |
* double} values, each between {@code 0.0} (inclusive) and {@code |
806 |
< |
* 1.0} (exclusive). |
805 |
> |
* double} values, each between zero (inclusive) and one |
806 |
> |
* (exclusive). |
807 |
|
* |
808 |
|
* @implNote This method is implemented to be equivalent to {@code |
809 |
|
* doubles(Long.MAX_VALUE)}. |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
< |
* Returns a stream with the given {@code streamSize} number of |
821 |
> |
* Returns a stream producing the given {@code streamSize} number of |
822 |
|
* pseudorandom {@code double} values, each conforming to the |
823 |
|
* given origin and bound. |
824 |
|
* |
826 |
|
* @param randomNumberOrigin the origin of each random value |
827 |
|
* @param randomNumberBound the bound of each random value |
828 |
|
* @return a stream of pseudorandom {@code double} values, |
829 |
< |
* each with the given origin and bound. |
829 |
> |
* each with the given origin and bound |
830 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
831 |
< |
* less than zero. |
831 |
> |
* less than zero |
832 |
|
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
833 |
|
* is greater than or equal to {@code randomNumberBound} |
834 |
|
*/ |
836 |
|
double randomNumberBound) { |
837 |
|
if (streamSize < 0L) |
838 |
|
throw new IllegalArgumentException("negative Stream size"); |
839 |
< |
if (randomNumberOrigin >= randomNumberBound) |
839 |
> |
if (!(randomNumberOrigin < randomNumberBound)) |
840 |
|
throw new IllegalArgumentException("bound must be greater than origin"); |
841 |
|
return StreamSupport.doubleStream |
842 |
|
(new RandomDoublesSpliterator |
854 |
|
* @param randomNumberOrigin the origin of each random value |
855 |
|
* @param randomNumberBound the bound of each random value |
856 |
|
* @return a stream of pseudorandom {@code double} values, |
857 |
< |
* each with the given origin and bound. |
857 |
> |
* each with the given origin and bound |
858 |
|
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
859 |
|
* is greater than or equal to {@code randomNumberBound} |
860 |
|
*/ |
861 |
|
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { |
862 |
< |
if (randomNumberOrigin >= randomNumberBound) |
862 |
> |
if (!(randomNumberOrigin < randomNumberBound)) |
863 |
|
throw new IllegalArgumentException("bound must be greater than origin"); |
864 |
|
return StreamSupport.doubleStream |
865 |
|
(new RandomDoublesSpliterator |
869 |
|
|
870 |
|
/** |
871 |
|
* Spliterator for int streams. We multiplex the four int |
872 |
< |
* versions into one class by treating and bound < origin as |
872 |
> |
* versions into one class by treating a bound less than origin as |
873 |
|
* unbounded, and also by treating "infinite" as equivalent to |
874 |
|
* Long.MAX_VALUE. For splits, it uses the standard divide-by-two |
875 |
|
* approach. The long and double versions of this class are |
876 |
|
* identical except for types. |
877 |
|
*/ |
878 |
< |
static class RandomIntsSpliterator implements Spliterator.OfInt { |
878 |
> |
static final class RandomIntsSpliterator implements Spliterator.OfInt { |
879 |
|
final SplittableRandom rng; |
880 |
|
long index; |
881 |
|
final long fence; |
899 |
|
|
900 |
|
public int characteristics() { |
901 |
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
902 |
< |
Spliterator.ORDERED | Spliterator.NONNULL | |
873 |
< |
Spliterator.IMMUTABLE); |
902 |
> |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
903 |
|
} |
904 |
|
|
905 |
|
public boolean tryAdvance(IntConsumer consumer) { |
929 |
|
/** |
930 |
|
* Spliterator for long streams. |
931 |
|
*/ |
932 |
< |
static class RandomLongsSpliterator implements Spliterator.OfLong { |
932 |
> |
static final class RandomLongsSpliterator implements Spliterator.OfLong { |
933 |
|
final SplittableRandom rng; |
934 |
|
long index; |
935 |
|
final long fence; |
953 |
|
|
954 |
|
public int characteristics() { |
955 |
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
956 |
< |
Spliterator.ORDERED | Spliterator.NONNULL | |
928 |
< |
Spliterator.IMMUTABLE); |
956 |
> |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
957 |
|
} |
958 |
|
|
959 |
|
public boolean tryAdvance(LongConsumer consumer) { |
984 |
|
/** |
985 |
|
* Spliterator for double streams. |
986 |
|
*/ |
987 |
< |
static class RandomDoublesSpliterator implements Spliterator.OfDouble { |
987 |
> |
static final class RandomDoublesSpliterator implements Spliterator.OfDouble { |
988 |
|
final SplittableRandom rng; |
989 |
|
long index; |
990 |
|
final long fence; |
1008 |
|
|
1009 |
|
public int characteristics() { |
1010 |
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
1011 |
< |
Spliterator.ORDERED | Spliterator.NONNULL | |
984 |
< |
Spliterator.IMMUTABLE); |
1011 |
> |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
1012 |
|
} |
1013 |
|
|
1014 |
|
public boolean tryAdvance(DoubleConsumer consumer) { |
1036 |
|
} |
1037 |
|
|
1038 |
|
} |
1012 |
– |
|