--- jsr166/src/main/java/util/SplittableRandom.java 2013/07/12 19:45:19 1.8
+++ jsr166/src/main/java/util/SplittableRandom.java 2013/07/16 12:32:05 1.11
@@ -41,7 +41,9 @@ import java.util.stream.DoubleStream;
* generate subtasks. Class 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:
+ * {@link java.util.Random} but differs in the following ways:
+ *
+ *
*
* - Series of generated values pass the DieHarder suite testing
* independence and uniformity properties of random number generators.
@@ -49,7 +51,9 @@ import java.util.stream.DoubleStream;
* href="http://www.phy.duke.edu/~rgb/General/dieharder.php"> version
* 3.31.1.) These tests validate only the methods for certain
* types and ranges, but similar properties are expected to hold, at
- * least approximately, for others as well.
+ * least approximately, for others as well. The period
+ * (length of any series of generated values before it repeats) is at
+ * least 264.
*
* - Method {@link #split} constructs and returns a new
* SplittableRandom instance that shares no mutable state with the
@@ -154,13 +158,16 @@ public class SplittableRandom {
* SplittableRandom. Unlike other cases, this split must be
* performed in a thread-safe manner. We use
* AtomicLong.compareAndSet as the (typically) most efficient
- * mechanism. To bootstrap, we start off using System.nanotime(),
- * and update using another "genuinely random" constant
- * DEFAULT_SEED_GAMMA. The default constructor uses GAMMA_GAMMA,
- * not 0, for its splitSeed argument (addGammaModGeorge(0,
- * GAMMA_GAMMA) == GAMMA_GAMMA) to reflect that each is split from
- * this root generator, even though the root is not explicitly
- * represented as a SplittableRandom.
+ * mechanism. To bootstrap, we start off using a function of the
+ * current System time as seed, and update using another
+ * "genuinely random" constant DEFAULT_SEED_GAMMA. The default
+ * constructor uses GAMMA_GAMMA, not 0, for its splitSeed argument
+ * (addGammaModGeorge(0, GAMMA_GAMMA) == GAMMA_GAMMA) to reflect
+ * that each is split from this root generator, even though the
+ * root is not explicitly represented as a SplittableRandom. When
+ * establishing the initial seed, we use both
+ * System.currentTimeMillis and System.nanoTime(), to avoid
+ * regularities that may occur if using either alone.
*/
/**
@@ -178,6 +185,12 @@ public class SplittableRandom {
private static final long DEFAULT_SEED_GAMMA = 0xBD24B73A95FB84D9L;
/**
+ * The value 13 with 64bit sign bit set. Used in the signed
+ * comparison in addGammaModGeorge.
+ */
+ private static final long BOTTOM13 = 0x800000000000000DL;
+
+ /**
* The least non-zero value returned by nextDouble(). This value
* is scaled by a random value of 53 bits to produce a result.
*/
@@ -187,7 +200,8 @@ public class SplittableRandom {
* The next seed for default constructors.
*/
private static final AtomicLong defaultSeedGenerator =
- new AtomicLong(System.nanoTime());
+ new AtomicLong(mix64(System.currentTimeMillis()) ^
+ mix64(System.nanoTime()));
/**
* The seed, updated only via method nextSeed.
@@ -215,15 +229,16 @@ public class SplittableRandom {
* George < 2^64; thus we need only a conditional, not a loop,
* to be sure of getting a representable value.
*
- * @param s a seed value
+ * Because Java comparison operators are signed, we implement this
+ * by conceptually offsetting seed values downwards by 2^63, so
+ * 0..13 is represented as Long.MIN_VALUE..BOTTOM13.
+ *
+ * @param s a seed value, viewed as a signed long
* @param g a gamma value, 13 <= g (as unsigned)
*/
private static long addGammaModGeorge(long s, long g) {
long p = s + g;
- if (Long.compareUnsigned(p, g) >= 0)
- return p;
- long q = p - 13L;
- return (Long.compareUnsigned(p, 13L) >= 0) ? q : (q + g);
+ return (p >= s) ? p : ((p >= BOTTOM13) ? p : p + g) - 13L;
}
/**
@@ -262,7 +277,7 @@ public class SplittableRandom {
do { // ensure gamma >= 13, considered as an unsigned integer
s = addGammaModGeorge(s, GAMMA_GAMMA);
g = mix64(s);
- } while (Long.compareUnsigned(g, 13L) < 0);
+ } while (g >= 0L && g < 13L);
this.gamma = g;
this.nextSplit = s;
}
@@ -401,7 +416,7 @@ public class SplittableRandom {
/**
* Creates a new SplittableRandom instance using the specified
* initial seed. SplittableRandom instances created with the same
- * seed generate identical sequences of values.
+ * seed in the same program generate identical sequences of values.
*
* @param seed the initial seed
*/
@@ -453,7 +468,7 @@ public class SplittableRandom {
* @param bound the bound on the random number to be returned. Must be
* positive.
* @return a pseudorandom {@code int} value between zero
- * (inclusive) and the bound (exclusive).
+ * (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if the bound is less than zero
*/
public int nextInt(int bound) {
@@ -480,7 +495,7 @@ public class SplittableRandom {
* @param origin the least value returned
* @param bound the upper bound (exclusive)
* @return a pseudorandom {@code int} value between the origin
- * (inclusive) and the bound (exclusive).
+ * (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if {@code origin} is greater than
* or equal to {@code bound}
*/
@@ -506,7 +521,7 @@ public class SplittableRandom {
* @param bound the bound on the random number to be returned. Must be
* positive.
* @return a pseudorandom {@code long} value between zero
- * (inclusive) and the bound (exclusive).
+ * (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if {@code bound} is less than zero
*/
public long nextLong(long bound) {
@@ -533,7 +548,7 @@ public class SplittableRandom {
* @param origin the least value returned
* @param bound the upper bound (exclusive)
* @return a pseudorandom {@code long} value between the origin
- * (inclusive) and the bound (exclusive).
+ * (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if {@code origin} is greater than
* or equal to {@code bound}
*/
@@ -551,7 +566,7 @@ public class SplittableRandom {
* (inclusive) and one (exclusive)
*/
public double nextDouble() {
- return (nextLong() >>> 11) * DOUBLE_UNIT;
+ return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT;
}
/**
@@ -561,13 +576,13 @@ public class SplittableRandom {
* @param bound the bound on the random number to be returned. Must be
* positive.
* @return a pseudorandom {@code double} value between zero
- * (inclusive) and the bound (exclusive).
+ * (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if {@code bound} is less than zero
*/
public double nextDouble(double bound) {
if (!(bound > 0.0))
throw new IllegalArgumentException("bound must be positive");
- double result = nextDouble() * bound;
+ double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound;
return (result < bound) ? result : // correct for rounding
Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
}
@@ -579,7 +594,7 @@ public class SplittableRandom {
* @param origin the least value returned
* @param bound the upper bound
* @return a pseudorandom {@code double} value between the origin
- * (inclusive) and the bound (exclusive).
+ * (inclusive) and the bound (exclusive)
* @throws IllegalArgumentException if {@code origin} is greater than
* or equal to {@code bound}
*/
@@ -589,6 +604,15 @@ public class SplittableRandom {
return internalNextDouble(origin, bound);
}
+ /**
+ * Returns a pseudorandom {@code boolean} value.
+ *
+ * @return a pseudorandom {@code boolean} value
+ */
+ public boolean nextBoolean() {
+ return mix32(nextSeed()) < 0;
+ }
+
// stream methods, coded in a way intended to better isolate for
// maintenance purposes the small differences across forms.
@@ -612,7 +636,7 @@ public class SplittableRandom {
/**
* Returns an effectively unlimited stream of pseudorandom {@code int}
- * values
+ * values.
*
* @implNote This method is implemented to be equivalent to {@code
* ints(Long.MAX_VALUE)}.
@@ -635,7 +659,7 @@ public class SplittableRandom {
* @param randomNumberOrigin the origin of each random value
* @param randomNumberBound the bound of each random value
* @return a stream of pseudorandom {@code int} values,
- * each with the given origin and bound.
+ * each with the given origin and bound
* @throws IllegalArgumentException if {@code streamSize} is
* less than zero, or {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
@@ -662,7 +686,7 @@ public class SplittableRandom {
* @param randomNumberOrigin the origin of each random value
* @param randomNumberBound the bound of each random value
* @return a stream of pseudorandom {@code int} values,
- * each with the given origin and bound.
+ * each with the given origin and bound
* @throws IllegalArgumentException if {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
*/
@@ -718,7 +742,7 @@ public class SplittableRandom {
* @param randomNumberOrigin the origin of each random value
* @param randomNumberBound the bound of each random value
* @return a stream of pseudorandom {@code long} values,
- * each with the given origin and bound.
+ * each with the given origin and bound
* @throws IllegalArgumentException if {@code streamSize} is
* less than zero, or {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
@@ -745,7 +769,7 @@ public class SplittableRandom {
* @param randomNumberOrigin the origin of each random value
* @param randomNumberBound the bound of each random value
* @return a stream of pseudorandom {@code long} values,
- * each with the given origin and bound.
+ * each with the given origin and bound
* @throws IllegalArgumentException if {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
*/
@@ -803,9 +827,9 @@ public class SplittableRandom {
* @param randomNumberOrigin the origin of each random value
* @param randomNumberBound the bound of each random value
* @return a stream of pseudorandom {@code double} values,
- * each with the given origin and bound.
+ * each with the given origin and bound
* @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}
*/
@@ -831,7 +855,7 @@ public class SplittableRandom {
* @param randomNumberOrigin the origin of each random value
* @param randomNumberBound the bound of each random value
* @return a stream of pseudorandom {@code double} values,
- * each with the given origin and bound.
+ * each with the given origin and bound
* @throws IllegalArgumentException if {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
*/
@@ -852,7 +876,7 @@ public class SplittableRandom {
* approach. The long and double versions of this class are
* identical except for types.
*/
- static class RandomIntsSpliterator implements Spliterator.OfInt {
+ static final class RandomIntsSpliterator implements Spliterator.OfInt {
final SplittableRandom rng;
long index;
final long fence;
@@ -906,7 +930,7 @@ public class SplittableRandom {
/**
* Spliterator for long streams.
*/
- static class RandomLongsSpliterator implements Spliterator.OfLong {
+ static final class RandomLongsSpliterator implements Spliterator.OfLong {
final SplittableRandom rng;
long index;
final long fence;
@@ -961,7 +985,7 @@ public class SplittableRandom {
/**
* Spliterator for double streams.
*/
- static class RandomDoublesSpliterator implements Spliterator.OfDouble {
+ static final class RandomDoublesSpliterator implements Spliterator.OfDouble {
final SplittableRandom rng;
long index;
final long fence;