--- jsr166/src/main/java/util/SplittableRandom.java 2013/08/13 17:13:57 1.16
+++ jsr166/src/main/java/util/SplittableRandom.java 2016/11/12 00:12:05 1.33
@@ -25,21 +25,19 @@
package java.util;
-import java.net.InetAddress;
import java.util.concurrent.atomic.AtomicLong;
-import java.util.Spliterator;
+import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;
-import java.util.function.DoubleConsumer;
-import java.util.stream.StreamSupport;
+import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
-import java.util.stream.DoubleStream;
+import java.util.stream.StreamSupport;
/**
* A generator of uniform pseudorandom values applicable for use in
* (among other contexts) isolated parallel computations that may
- * generate subtasks. Class SplittableRandom supports methods for
+ * generate subtasks. Class {@code SplittableRandom} supports methods for
* producing pseudorandom numbers of type {@code int}, {@code long},
* and {@code double} with similar usages as for class
* {@link java.util.Random} but differs in the following ways:
@@ -54,15 +52,15 @@ import java.util.stream.DoubleStream;
* types and ranges, but similar properties are expected to hold, at
* least approximately, for others as well. The period
* (length of any series of generated values before it repeats) is at
- * least 264.
+ * least 264.
*
- *
Method {@link #split} constructs and returns a new
+ * Method {@link #split} constructs and returns a new
* SplittableRandom instance that shares no mutable state with the
* current instance. However, with very high probability, the
* values collectively generated by the two objects have the same
* statistical properties as if the same quantity of values were
* generated by a single thread using a single {@code
- * SplittableRandom} object.
+ * SplittableRandom} object.
*
* Instances of SplittableRandom are not thread-safe.
* They are designed to be split, not shared, across threads. For
@@ -73,15 +71,22 @@ import java.util.stream.DoubleStream;
*
* This class provides additional methods for generating random
* streams, that employ the above techniques when used in {@code
- * stream.parallel()} mode.
+ * stream.parallel()} mode.
*
*
*
+ * Instances of {@code SplittableRandom} are not cryptographically
+ * secure. Consider instead using {@link java.security.SecureRandom}
+ * in security-sensitive applications. Additionally,
+ * default-constructed instances do not use a cryptographically random
+ * seed unless the {@linkplain System#getProperty system property}
+ * {@code java.util.secureRandomSeed} is set to {@code true}.
+ *
* @author Guy Steele
* @author Doug Lea
* @since 1.8
*/
-public class SplittableRandom {
+public final class SplittableRandom {
/*
* Implementation Overview.
@@ -101,29 +106,25 @@ public class SplittableRandom {
* Methods nextLong, nextInt, and derivatives do not return the
* sequence (seed) values, but instead a hash-like bit-mix of
* their bits, producing more independently distributed sequences.
- * For nextLong, the mix64 bit-mixing function computes the same
- * value as the "64-bit finalizer" function in Austin Appleby's
- * MurmurHash3 algorithm. See
- * http://code.google.com/p/smhasher/wiki/MurmurHash3 , which
- * comments: "The constants for the finalizers were generated by a
- * simple simulated-annealing algorithm, and both avalanche all
- * bits of 'h' to within 0.25% bias." The mix32 function is
- * equivalent to (int)(mix64(seed) >>> 32), but faster because it
- * omits a step that doesn't contribute to result.
+ * For nextLong, the mix64 function is based on David Stafford's
+ * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
+ * "Mix13" variant of the "64-bit finalizer" function in Austin
+ * Appleby's MurmurHash3 algorithm (see
+ * http://code.google.com/p/smhasher/wiki/MurmurHash3). The mix32
+ * function is based on Stafford's Mix04 mix function, but returns
+ * the upper 32 bits cast as int.
*
* The split operation uses the current generator to form the seed
* and gamma for another SplittableRandom. To conservatively
* avoid potential correlations between seed and value generation,
- * gamma selection (method nextGamma) uses the "Mix13" constants
- * for MurmurHash3 described by David Stafford
- * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
- * To avoid potential weaknesses in bit-mixing transformations, we
- * restrict gammas to odd values with at least 12 and no more than
- * 52 bits set. Rather than rejecting candidates with too few or
- * too many bits set, method nextGamma flips some bits (which has
- * the effect of mapping at most 4 to any given gamma value).
- * This reduces the effective set of 64bit odd gamma values by
- * about 214, a very tiny percentage, and serves as an
+ * gamma selection (method mixGamma) uses different
+ * (Murmurhash3's) mix constants. To avoid potential weaknesses
+ * in bit-mixing transformations, we restrict gammas to odd values
+ * with at least 24 0-1 or 1-0 bit transitions. Rather than
+ * rejecting candidates with too few or too many bits set, method
+ * mixGamma flips some bits (which has the effect of mapping at
+ * most 4 to any given gamma value). This reduces the effective
+ * set of 64bit odd gamma values by about 2%, and serves as an
* automated screening for sequence constant selection that is
* left as an empirical decision in some other hashing and crypto
* algorithms.
@@ -134,13 +135,14 @@ public class SplittableRandom {
* avalanching.
*
* The default (no-argument) constructor, in essence, invokes
- * split() for a common "seeder" SplittableRandom. Unlike other
- * cases, this split must be performed in a thread-safe manner, so
- * we use an AtomicLong to represent the seed rather than use an
- * explicit SplittableRandom. To bootstrap the seeder, we start
- * off using a seed based on current time and host. This serves as
- * a slimmed-down (and insecure) variant of SecureRandom that also
- * avoids stalls that may occur when using /dev/random.
+ * split() for a common "defaultGen" SplittableRandom. Unlike
+ * other cases, this split must be performed in a thread-safe
+ * manner, so we use an AtomicLong to represent the seed rather
+ * than use an explicit SplittableRandom. To bootstrap the
+ * defaultGen, we start off using a seed based on current time
+ * unless the java.util.secureRandomSeed property is set. This
+ * serves as a slimmed-down (and insecure) variant of SecureRandom
+ * that also avoids stalls that may occur when using /dev/random.
*
* It is a relatively simple matter to apply the basic design here
* to use 128 bit seeds. However, emulating 128bit arithmetic and
@@ -153,17 +155,16 @@ public class SplittableRandom {
*/
/**
- * The initial gamma value for (unsplit) SplittableRandoms. Must
- * be odd with at least 12 and no more than 52 bits set. Currently
- * set to the golden ratio scaled to 64bits.
+ * The golden ratio scaled to 64bits, used as the initial gamma
+ * value for (unsplit) SplittableRandoms.
*/
- private static final long INITIAL_GAMMA = 0x9e3779b97f4a7c15L;
+ private static final long GOLDEN_GAMMA = 0x9e3779b97f4a7c15L;
/**
* The least non-zero value returned by nextDouble(). This value
* is scaled by a random value of 53 bits to produce a result.
*/
- private static final double DOUBLE_UNIT = 1.0 / (1L << 53);
+ private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53);
/**
* The seed. Updated only via method nextSeed.
@@ -184,31 +185,31 @@ public class SplittableRandom {
}
/**
- * Computes MurmurHash3 64bit mix function.
+ * Computes Stafford variant 13 of 64bit mix function.
*/
private static long mix64(long z) {
- z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
- z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
- return z ^ (z >>> 33);
+ z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
+ z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
+ return z ^ (z >>> 31);
}
/**
- * Returns the 32 high bits of mix64(z) as int.
+ * Returns the 32 high bits of Stafford variant 4 mix64 function as int.
*/
private static int mix32(long z) {
- z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
- return (int)(((z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L) >>> 32);
+ z = (z ^ (z >>> 33)) * 0x62a9d9ed799705f5L;
+ return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32);
}
/**
* Returns the gamma value to use for a new split instance.
*/
- private static long nextGamma(long z) {
- z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L; // Stafford "Mix13"
- z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
- z = (z ^ (z >>> 31)) | 1L; // force to be odd
- int n = Long.bitCount(z); // ensure enough 0 and 1 bits
- return (n < 12 || n > 52) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
+ private static long mixGamma(long z) {
+ z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
+ z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
+ z = (z ^ (z >>> 33)) | 1L; // force to be odd
+ int n = Long.bitCount(z ^ (z >>> 1)); // ensure enough transitions
+ return (n < 24) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
}
/**
@@ -218,30 +219,33 @@ public class SplittableRandom {
return seed += gamma;
}
+ // IllegalArgumentException messages
+ static final String BAD_BOUND = "bound must be positive";
+ static final String BAD_RANGE = "bound must be greater than origin";
+ static final String BAD_SIZE = "size must be non-negative";
+
/**
* The seed generator for default constructors.
*/
- private static final AtomicLong seeder =
- new AtomicLong(mix64((((long)hashedHostAddress()) << 32) ^
- System.currentTimeMillis()) ^
- mix64(System.nanoTime()));
-
- /**
- * Returns hash of local host IP address, if available; else 0.
- */
- private static int hashedHostAddress() {
- try {
- return InetAddress.getLocalHost().hashCode();
- } catch (Exception ex) {
- return 0;
- }
+ private static final AtomicLong defaultGen
+ = new AtomicLong(mix64(System.currentTimeMillis()) ^
+ mix64(System.nanoTime()));
+
+ // at end of to survive static initialization circularity
+ static {
+ if (java.security.AccessController.doPrivileged(
+ new java.security.PrivilegedAction() {
+ public Boolean run() {
+ return Boolean.getBoolean("java.util.secureRandomSeed");
+ }})) {
+ byte[] seedBytes = java.security.SecureRandom.getSeed(8);
+ long s = (long)seedBytes[0] & 0xffL;
+ for (int i = 1; i < 8; ++i)
+ s = (s << 8) | ((long)seedBytes[i] & 0xffL);
+ defaultGen.set(s);
+ }
}
- // IllegalArgumentException messages
- static final String BadBound = "bound must be positive";
- static final String BadRange = "bound must be greater than origin";
- static final String BadSize = "size must be non-negative";
-
/*
* Internal versions of nextX methods used by streams, as well as
* the public nextX(origin, bound) methods. These exist mainly to
@@ -361,7 +365,7 @@ public class SplittableRandom {
* @param seed the initial seed
*/
public SplittableRandom(long seed) {
- this(seed, INITIAL_GAMMA);
+ this(seed, GOLDEN_GAMMA);
}
/**
@@ -370,8 +374,10 @@ public class SplittableRandom {
* of those of any other instances in the current program; and
* may, and typically does, vary across program invocations.
*/
- public SplittableRandom() { // emulate seeder.split()
- this.gamma = nextGamma(this.seed = seeder.addAndGet(INITIAL_GAMMA));
+ public SplittableRandom() { // emulate defaultGen.split()
+ long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA);
+ this.seed = mix64(s);
+ this.gamma = mixGamma(s + GOLDEN_GAMMA);
}
/**
@@ -389,8 +395,7 @@ public class SplittableRandom {
* @return the new SplittableRandom instance
*/
public SplittableRandom split() {
- long s = nextSeed();
- return new SplittableRandom(s, nextGamma(s));
+ return new SplittableRandom(nextLong(), mixGamma(nextSeed()));
}
/**
@@ -406,15 +411,14 @@ public class SplittableRandom {
* Returns a pseudorandom {@code int} value between zero (inclusive)
* and the specified bound (exclusive).
*
- * @param bound the bound on the random number to be returned. Must be
- * positive.
+ * @param bound the upper bound (exclusive). Must be positive.
* @return a pseudorandom {@code int} value between zero
* (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if {@code bound} is not positive
*/
public int nextInt(int bound) {
if (bound <= 0)
- throw new IllegalArgumentException(BadBound);
+ throw new IllegalArgumentException(BAD_BOUND);
// Specialize internalNextInt for origin 0
int r = mix32(nextSeed());
int m = bound - 1;
@@ -442,7 +446,7 @@ public class SplittableRandom {
*/
public int nextInt(int origin, int bound) {
if (origin >= bound)
- throw new IllegalArgumentException(BadRange);
+ throw new IllegalArgumentException(BAD_RANGE);
return internalNextInt(origin, bound);
}
@@ -459,15 +463,14 @@ public class SplittableRandom {
* Returns a pseudorandom {@code long} value between zero (inclusive)
* and the specified bound (exclusive).
*
- * @param bound the bound on the random number to be returned. Must be
- * positive.
+ * @param bound the upper bound (exclusive). Must be positive.
* @return a pseudorandom {@code long} value between zero
* (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if {@code bound} is not positive
*/
public long nextLong(long bound) {
if (bound <= 0)
- throw new IllegalArgumentException(BadBound);
+ throw new IllegalArgumentException(BAD_BOUND);
// Specialize internalNextLong for origin 0
long r = mix64(nextSeed());
long m = bound - 1;
@@ -495,7 +498,7 @@ public class SplittableRandom {
*/
public long nextLong(long origin, long bound) {
if (origin >= bound)
- throw new IllegalArgumentException(BadRange);
+ throw new IllegalArgumentException(BAD_RANGE);
return internalNextLong(origin, bound);
}
@@ -504,7 +507,7 @@ public class SplittableRandom {
* (inclusive) and one (exclusive).
*
* @return a pseudorandom {@code double} value between zero
- * (inclusive) and one (exclusive)
+ * (inclusive) and one (exclusive)
*/
public double nextDouble() {
return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT;
@@ -514,26 +517,39 @@ public class SplittableRandom {
* Returns a pseudorandom {@code double} value between 0.0
* (inclusive) and the specified bound (exclusive).
*
- * @param bound the bound on the random number to be returned. Must be
- * positive.
+ * @param bound the upper bound (exclusive). Must be positive.
* @return a pseudorandom {@code double} value between zero
* (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if {@code bound} is not positive
*/
public double nextDouble(double bound) {
if (!(bound > 0.0))
- throw new IllegalArgumentException(BadBound);
+ throw new IllegalArgumentException(BAD_BOUND);
double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound;
return (result < bound) ? result : // correct for rounding
Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
}
+ /**
+ * Generates a pseudorandom number with the indicated number of
+ * bits. Unlike in superclass @{link Random}, this method is never
+ * internally called or used by any other publicly accessible
+ * method.
+ *
+ * @param bits random bits
+ * @return the next pseudorandom value from this random number
+ * generator's sequence
+ */
+ protected int next(int bits) {
+ return (int)(nextLong() >>> (64 - bits));
+ }
+
/**
* Returns a pseudorandom {@code double} value between the specified
* origin (inclusive) and bound (exclusive).
*
* @param origin the least value returned
- * @param bound the upper bound
+ * @param bound the upper bound (exclusive)
* @return a pseudorandom {@code double} value between the origin
* (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if {@code origin} is greater than
@@ -541,7 +557,7 @@ public class SplittableRandom {
*/
public double nextDouble(double origin, double bound) {
if (!(origin < bound))
- throw new IllegalArgumentException(BadRange);
+ throw new IllegalArgumentException(BAD_RANGE);
return internalNextDouble(origin, bound);
}
@@ -569,7 +585,7 @@ public class SplittableRandom {
*/
public IntStream ints(long streamSize) {
if (streamSize < 0L)
- throw new IllegalArgumentException(BadSize);
+ throw new IllegalArgumentException(BAD_SIZE);
return StreamSupport.intStream
(new RandomIntsSpliterator
(this, 0L, streamSize, Integer.MAX_VALUE, 0),
@@ -594,14 +610,15 @@ public class SplittableRandom {
/**
* Returns a stream producing the given {@code streamSize} number
- * of pseudorandom {@code int} values, each conforming to the
- * given origin and bound.
+ * of pseudorandom {@code int} values from this generator and/or one split
+ * from it; each value conforms to the given origin (inclusive) and bound
+ * (exclusive).
*
* @param streamSize the number of values to generate
- * @param randomNumberOrigin the origin of each random value
- * @param randomNumberBound the bound of each random value
+ * @param randomNumberOrigin the origin (inclusive) of each random value
+ * @param randomNumberBound the bound (exclusive) of each random value
* @return a stream of pseudorandom {@code int} values,
- * each with the given origin and bound
+ * each with the given origin (inclusive) and bound (exclusive)
* @throws IllegalArgumentException if {@code streamSize} is
* less than zero, or {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
@@ -609,9 +626,9 @@ public class SplittableRandom {
public IntStream ints(long streamSize, int randomNumberOrigin,
int randomNumberBound) {
if (streamSize < 0L)
- throw new IllegalArgumentException(BadSize);
+ throw new IllegalArgumentException(BAD_SIZE);
if (randomNumberOrigin >= randomNumberBound)
- throw new IllegalArgumentException(BadRange);
+ throw new IllegalArgumentException(BAD_RANGE);
return StreamSupport.intStream
(new RandomIntsSpliterator
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
@@ -620,21 +637,22 @@ public class SplittableRandom {
/**
* Returns an effectively unlimited stream of pseudorandom {@code
- * int} values, each conforming to the given origin and bound.
+ * int} values from this generator and/or one split from it; each value
+ * conforms to the given origin (inclusive) and bound (exclusive).
*
* @implNote This method is implemented to be equivalent to {@code
* ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
*
- * @param randomNumberOrigin the origin of each random value
- * @param randomNumberBound the bound of each random value
+ * @param randomNumberOrigin the origin (inclusive) of each random value
+ * @param randomNumberBound the bound (exclusive) of each random value
* @return a stream of pseudorandom {@code int} values,
- * each with the given origin and bound
+ * each with the given origin (inclusive) and bound (exclusive)
* @throws IllegalArgumentException if {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
*/
public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
if (randomNumberOrigin >= randomNumberBound)
- throw new IllegalArgumentException(BadRange);
+ throw new IllegalArgumentException(BAD_RANGE);
return StreamSupport.intStream
(new RandomIntsSpliterator
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
@@ -653,7 +671,7 @@ public class SplittableRandom {
*/
public LongStream longs(long streamSize) {
if (streamSize < 0L)
- throw new IllegalArgumentException(BadSize);
+ throw new IllegalArgumentException(BAD_SIZE);
return StreamSupport.longStream
(new RandomLongsSpliterator
(this, 0L, streamSize, Long.MAX_VALUE, 0L),
@@ -678,14 +696,15 @@ public class SplittableRandom {
/**
* Returns a stream producing the given {@code streamSize} number of
- * pseudorandom {@code long} values, each conforming to the
- * given origin and bound.
+ * pseudorandom {@code long} values from this generator and/or one split
+ * from it; each value conforms to the given origin (inclusive) and bound
+ * (exclusive).
*
* @param streamSize the number of values to generate
- * @param randomNumberOrigin the origin of each random value
- * @param randomNumberBound the bound of each random value
+ * @param randomNumberOrigin the origin (inclusive) of each random value
+ * @param randomNumberBound the bound (exclusive) of each random value
* @return a stream of pseudorandom {@code long} values,
- * each with the given origin and bound
+ * each with the given origin (inclusive) and bound (exclusive)
* @throws IllegalArgumentException if {@code streamSize} is
* less than zero, or {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
@@ -693,9 +712,9 @@ public class SplittableRandom {
public LongStream longs(long streamSize, long randomNumberOrigin,
long randomNumberBound) {
if (streamSize < 0L)
- throw new IllegalArgumentException(BadSize);
+ throw new IllegalArgumentException(BAD_SIZE);
if (randomNumberOrigin >= randomNumberBound)
- throw new IllegalArgumentException(BadRange);
+ throw new IllegalArgumentException(BAD_RANGE);
return StreamSupport.longStream
(new RandomLongsSpliterator
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
@@ -704,21 +723,22 @@ public class SplittableRandom {
/**
* Returns an effectively unlimited stream of pseudorandom {@code
- * long} values, each conforming to the given origin and bound.
+ * long} values from this generator and/or one split from it; each value
+ * conforms to the given origin (inclusive) and bound (exclusive).
*
* @implNote This method is implemented to be equivalent to {@code
* longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
*
- * @param randomNumberOrigin the origin of each random value
- * @param randomNumberBound the bound of each random value
+ * @param randomNumberOrigin the origin (inclusive) of each random value
+ * @param randomNumberBound the bound (exclusive) of each random value
* @return a stream of pseudorandom {@code long} values,
- * each with the given origin and bound
+ * each with the given origin (inclusive) and bound (exclusive)
* @throws IllegalArgumentException if {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
*/
public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
if (randomNumberOrigin >= randomNumberBound)
- throw new IllegalArgumentException(BadRange);
+ throw new IllegalArgumentException(BAD_RANGE);
return StreamSupport.longStream
(new RandomLongsSpliterator
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
@@ -727,8 +747,8 @@ public class SplittableRandom {
/**
* Returns a stream producing the given {@code streamSize} number of
- * pseudorandom {@code double} values, each between zero
- * (inclusive) and one (exclusive).
+ * pseudorandom {@code double} values from this generator and/or one split
+ * from it; each value is between zero (inclusive) and one (exclusive).
*
* @param streamSize the number of values to generate
* @return a stream of {@code double} values
@@ -737,7 +757,7 @@ public class SplittableRandom {
*/
public DoubleStream doubles(long streamSize) {
if (streamSize < 0L)
- throw new IllegalArgumentException(BadSize);
+ throw new IllegalArgumentException(BAD_SIZE);
return StreamSupport.doubleStream
(new RandomDoublesSpliterator
(this, 0L, streamSize, Double.MAX_VALUE, 0.0),
@@ -746,8 +766,8 @@ public class SplittableRandom {
/**
* Returns an effectively unlimited stream of pseudorandom {@code
- * double} values, each between zero (inclusive) and one
- * (exclusive).
+ * double} values from this generator and/or one split from it; each value
+ * is between zero (inclusive) and one (exclusive).
*
* @implNote This method is implemented to be equivalent to {@code
* doubles(Long.MAX_VALUE)}.
@@ -763,25 +783,26 @@ public class SplittableRandom {
/**
* Returns a stream producing the given {@code streamSize} number of
- * pseudorandom {@code double} values, each conforming to the
- * given origin and bound.
+ * pseudorandom {@code double} values from this generator and/or one split
+ * from it; each value conforms to the given origin (inclusive) and bound
+ * (exclusive).
*
* @param streamSize the number of values to generate
- * @param randomNumberOrigin the origin of each random value
- * @param randomNumberBound the bound of each random value
+ * @param randomNumberOrigin the origin (inclusive) of each random value
+ * @param randomNumberBound the bound (exclusive) of each random value
* @return a stream of pseudorandom {@code double} values,
- * each with the given origin and bound
+ * each with the given origin (inclusive) and bound (exclusive)
* @throws IllegalArgumentException if {@code streamSize} is
- * less than zero
+ * less than zero
* @throws IllegalArgumentException if {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
*/
public DoubleStream doubles(long streamSize, double randomNumberOrigin,
double randomNumberBound) {
if (streamSize < 0L)
- throw new IllegalArgumentException(BadSize);
+ throw new IllegalArgumentException(BAD_SIZE);
if (!(randomNumberOrigin < randomNumberBound))
- throw new IllegalArgumentException(BadRange);
+ throw new IllegalArgumentException(BAD_RANGE);
return StreamSupport.doubleStream
(new RandomDoublesSpliterator
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
@@ -790,21 +811,22 @@ public class SplittableRandom {
/**
* Returns an effectively unlimited stream of pseudorandom {@code
- * double} values, each conforming to the given origin and bound.
+ * double} values from this generator and/or one split from it; each value
+ * conforms to the given origin (inclusive) and bound (exclusive).
*
* @implNote This method is implemented to be equivalent to {@code
* doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
*
- * @param randomNumberOrigin the origin of each random value
- * @param randomNumberBound the bound of each random value
+ * @param randomNumberOrigin the origin (inclusive) of each random value
+ * @param randomNumberBound the bound (exclusive) of each random value
* @return a stream of pseudorandom {@code double} values,
- * each with the given origin and bound
+ * each with the given origin (inclusive) and bound (exclusive)
* @throws IllegalArgumentException if {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
*/
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
if (!(randomNumberOrigin < randomNumberBound))
- throw new IllegalArgumentException(BadRange);
+ throw new IllegalArgumentException(BAD_RANGE);
return StreamSupport.doubleStream
(new RandomDoublesSpliterator
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
@@ -819,7 +841,8 @@ public class SplittableRandom {
* approach. The long and double versions of this class are
* identical except for types.
*/
- static final class RandomIntsSpliterator implements Spliterator.OfInt {
+ private static final class RandomIntsSpliterator
+ implements Spliterator.OfInt {
final SplittableRandom rng;
long index;
final long fence;
@@ -874,7 +897,8 @@ public class SplittableRandom {
/**
* Spliterator for long streams.
*/
- static final class RandomLongsSpliterator implements Spliterator.OfLong {
+ private static final class RandomLongsSpliterator
+ implements Spliterator.OfLong {
final SplittableRandom rng;
long index;
final long fence;
@@ -930,7 +954,8 @@ public class SplittableRandom {
/**
* Spliterator for double streams.
*/
- static final class RandomDoublesSpliterator implements Spliterator.OfDouble {
+ private static final class RandomDoublesSpliterator
+ implements Spliterator.OfDouble {
final SplittableRandom rng;
long index;
final long fence;