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 nunmbers of type {@code int}, {@code long}, |
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> |
46 |
|
* |
74 |
|
* </ul> |
75 |
|
* |
76 |
|
* @author Guy Steele |
77 |
+ |
* @author Doug Lea |
78 |
|
* @since 1.8 |
79 |
|
*/ |
80 |
|
public class SplittableRandom { |
113 |
|
* The value of gamma differs for each instance across a series of |
114 |
|
* splits, and is generated using a slightly stripped-down variant |
115 |
|
* of the same algorithm, but operating across calls to split(), |
116 |
< |
* not calls to nextLong(): Each instance carries the state of |
116 |
> |
* not calls to nextSeed(): Each instance carries the state of |
117 |
|
* this generator as nextSplit, and uses mix64(nextSplit) as its |
118 |
|
* own gamma value. Computations of gammas themselves use a fixed |
119 |
|
* constant as the second argument to the addGammaModGeorge |
311 |
|
* evenly divisible by the range. The loop rejects candidates |
312 |
|
* computed from otherwise over-represented values. The |
313 |
|
* expected number of iterations under an ideal generator |
314 |
< |
* varies from 1 to 2, depending on the bound. |
314 |
> |
* varies from 1 to 2, depending on the bound. The loop itself |
315 |
> |
* takes an unlovable form. Because the first candidate is |
316 |
> |
* already available, we need a break-in-the-middle |
317 |
> |
* construction, which is concisely but cryptically performed |
318 |
> |
* within the while-condition of a body-less for loop. |
319 |
|
* |
320 |
|
* 4. Otherwise, the range cannot be represented as a positive |
321 |
< |
* long. Repeatedly generate unbounded longs until obtaining |
322 |
< |
* a candidate meeting constraints (with an expected number of |
323 |
< |
* iterations of less than two). |
321 |
> |
* long. The loop repeatedly generates unbounded longs until |
322 |
> |
* obtaining a candidate meeting constraints (with an expected |
323 |
> |
* number of iterations of less than two). |
324 |
|
*/ |
325 |
|
|
326 |
|
long r = mix64(nextSeed()); |
757 |
|
|
758 |
|
/** |
759 |
|
* Returns a stream with the given {@code streamSize} number of |
760 |
< |
* pseudorandom {@code double} values. |
760 |
> |
* pseudorandom {@code double} values, each between {@code 0.0} |
761 |
> |
* (inclusive) and {@code 1.0} (exclusive). |
762 |
|
* |
763 |
|
* @param streamSize the number of values to generate |
764 |
|
* @return a stream of {@code double} values |
776 |
|
|
777 |
|
/** |
778 |
|
* Returns an effectively unlimited stream of pseudorandom {@code |
779 |
< |
* double} values. |
779 |
> |
* double} values, each between {@code 0.0} (inclusive) and {@code |
780 |
> |
* 1.0} (exclusive). |
781 |
|
* |
782 |
|
* @implNote This method is implemented to be equivalent to {@code |
783 |
|
* doubles(Long.MAX_VALUE)}. |
873 |
|
|
874 |
|
public int characteristics() { |
875 |
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
876 |
< |
Spliterator.ORDERED | Spliterator.NONNULL | |
870 |
< |
Spliterator.IMMUTABLE); |
876 |
> |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
877 |
|
} |
878 |
|
|
879 |
|
public boolean tryAdvance(IntConsumer consumer) { |
927 |
|
|
928 |
|
public int characteristics() { |
929 |
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
930 |
< |
Spliterator.ORDERED | Spliterator.NONNULL | |
925 |
< |
Spliterator.IMMUTABLE); |
930 |
> |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
931 |
|
} |
932 |
|
|
933 |
|
public boolean tryAdvance(LongConsumer consumer) { |
982 |
|
|
983 |
|
public int characteristics() { |
984 |
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
985 |
< |
Spliterator.ORDERED | Spliterator.NONNULL | |
981 |
< |
Spliterator.IMMUTABLE); |
985 |
> |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
986 |
|
} |
987 |
|
|
988 |
|
public boolean tryAdvance(DoubleConsumer consumer) { |