--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/AbstractArbitrarilyJumpableRng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,543 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.util.Spliterator;
+import java.util.function.Consumer;
+import java.util.function.IntConsumer;
+import java.util.function.LongConsumer;
+import java.util.function.DoubleConsumer;
+import java.util.stream.StreamSupport;
+import java.util.stream.Stream;
+
+/**
+ * This class provides much of the implementation of the
+ * {@code ArbitrarilyJumpableRng} interface, to minimize the effort
+ * required to implement that interface.
+ *
+ * To implement a pseudorandom number generator, the programmer needs
+ * only to extend this class and provide implementations for the
+ * methods {@code nextInt()}, {@code nextLong()}, {@code copy()},
+ * {@code jump(distance)}, {@code jumpPowerOfTwo(distance)},
+ * {@code defaultJumpDistance()}, and {@code defaultLeapDistance()}.
+ *
+ * (If the pseudorandom number generator also has the ability to split,
+ * then the programmer may wish to consider instead extending
+ * {@code AbstractSplittableArbitrarilyJumpableRng}.)
+ *
+ * The programmer should generally provide at least three constructors:
+ * one that takes no arguments, one that accepts a {@code long}
+ * seed value, and one that accepts an array of seed {@code byte} values.
+ * This class provides a public {@code initialSeed()} method that may
+ * be useful in initializing some static state from which to derive
+ * defaults seeds for use by the no-argument constructor.
+ *
+ * For the stream methods (such as {@code ints()} and {@code splits()}),
+ * this class provides {@code Spliterator}-based implementations that
+ * allow parallel execution when appropriate. In this respect
+ * {@code ArbitrarilyJumpableRng} differs from {@code JumpableRng},
+ * which provides very simple implementations that produce
+ * sequential streams only.
+ *
+ * <p>An implementation of the {@code AbstractArbitrarilyJumpableRng} class
+ * must provide concrete definitions for the methods {@code nextInt()},
+ * {@code nextLong}, {@code period()}, {@code copy()}, {@code jump(double)},
+ * {@code defaultJumpDistance()}, and {@code defaultLeapDistance()}.
+ * Default implementations are provided for all other methods.
+ *
+ * The documentation for each non-abstract method in this class
+ * describes its implementation in detail. Each of these methods may
+ * be overridden if the pseudorandom number generator being
+ * implemented admits a more efficient implementation.
+ *
+ * @author Guy Steele
+ * @since 1.9
+ */
+public abstract class AbstractArbitrarilyJumpableRng
+ extends AbstractSpliteratorRng implements ArbitrarilyJumpableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * This class provides most of the "user API" methods needed to satisfy
+ * the interface java.util.ArbitrarilyJumpableRng. Most of these methods
+ * are in turn inherited from AbstractRng and the non-public class
+ * AbstractSpliteratorRng; this file implements four versions of the
+ * jumps method and defines the spliterators necessary to support them.
+ *
+ * File organization: First the non-public methods needed by the class
+ * AbstractSpliteratorRng, then the main public methods, followed by some
+ * custom spliterator classes needed for stream methods.
+ */
+
+ // IllegalArgumentException messages
+ static final String BadLogDistance = "logDistance must be non-negative";
+
+ // Methods required by class AbstractSpliteratorRng
+ Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
+ return new RandomIntsSpliterator(this, index, fence, origin, bound);
+ }
+ Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
+ return new RandomLongsSpliterator(this, index, fence, origin, bound);
+ }
+ Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
+ return new RandomDoublesSpliterator(this, index, fence, origin, bound);
+ }
+
+ // Similar methods used by this class
+ Spliterator<Rng> makeJumpsSpliterator(long index, long fence, double distance) {
+ return new RandomJumpsSpliterator(this, index, fence, distance);
+ }
+ Spliterator<JumpableRng> makeLeapsSpliterator(long index, long fence, double distance) {
+ return new RandomLeapsSpliterator(this, index, fence, distance);
+ }
+ Spliterator<ArbitrarilyJumpableRng> makeArbitraryJumpsSpliterator(long index, long fence, double distance) {
+ return new RandomArbitraryJumpsSpliterator(this, index, fence, distance);
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Returns a new generator whose internal state is an exact copy
+ * of this generator (therefore their future behavior should be
+ * identical if subjected to the same series of operations).
+ *
+ * @return a new object that is a copy of this generator
+ */
+ public abstract AbstractArbitrarilyJumpableRng copy();
+
+ // Stream methods for jumping
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code Rng}
+ * interface, produced by jumping copies of this generator
+ * by different integer multiples of the default jump distance.
+ *
+ * @implNote This method is implemented to be equivalent to
+ * {@code jumps(Long.MAX_VALUE)}.
+ *
+ * @return a stream of objects that implement the {@code Rng} interface
+ */
+ public Stream<Rng> jumps() {
+ return StreamSupport.stream
+ (makeJumpsSpliterator(0L, Long.MAX_VALUE, defaultJumpDistance()),
+ false);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code Rng} interface, produced by jumping copies of this generator
+ * by different integer multiples of the default jump distance.
+ *
+ * @param streamSize the number of generators to generate
+ * @return a stream of objects that implement the {@code Rng} interface
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ public Stream<Rng> jumps(long streamSize) {
+ return StreamSupport.stream
+ (makeJumpsSpliterator(0L, streamSize, defaultJumpDistance()),
+ false);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code Rng}
+ * interface, produced by jumping copies of this generator
+ * by different integer multiples of the specified jump distance.
+ *
+ * @implNote This method is implemented to be equivalent to
+ * {@code jumps(Long.MAX_VALUE)}.
+ *
+ * @param distance a distance to jump forward within the state cycle
+ * @return a stream of objects that implement the {@code Rng} interface
+ */
+ public Stream<ArbitrarilyJumpableRng> jumps(double distance) {
+ return StreamSupport.stream
+ (makeArbitraryJumpsSpliterator(0L, Long.MAX_VALUE, distance),
+ false);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code Rng} interface, produced by jumping copies of this generator
+ * by different integer multiples of the specified jump distance.
+ *
+ * @param streamSize the number of generators to generate
+ * @param distance a distance to jump forward within the state cycle
+ * @return a stream of objects that implement the {@code Rng} interface
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ public Stream<ArbitrarilyJumpableRng> jumps(long streamSize, double distance) {
+ RngSupport.checkStreamSize(streamSize);
+ return StreamSupport.stream
+ (makeArbitraryJumpsSpliterator(0L, streamSize, distance),
+ false);
+ }
+
+ /**
+ * Alter the state of this pseudorandom number generator so as to
+ * jump forward a very large, fixed distance (typically 2<sup>128</sup>
+ * or more) within its state cycle. The distance used is that
+ * returned by method {@code defaultLeapDistance()}.
+ */
+ public void leap() { jump(defaultLeapDistance()); }
+
+ // Stream methods for leaping
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code Rng}
+ * interface, produced by jumping copies of this generator
+ * by different integer multiples of the default leap distance.
+ *
+ * @implNote This method is implemented to be equivalent to
+ * {@code leaps(Long.MAX_VALUE)}.
+ *
+ * @return a stream of objects that implement the {@code Rng} interface
+ */
+ public Stream<JumpableRng> leaps() {
+ return StreamSupport.stream
+ (makeLeapsSpliterator(0L, Long.MAX_VALUE, defaultLeapDistance()),
+ false);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code Rng} interface, produced by jumping copies of this generator
+ * by different integer multiples of the default leap distance.
+ *
+ * @param streamSize the number of generators to generate
+ * @return a stream of objects that implement the {@code Rng} interface
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ public Stream<JumpableRng> leaps(long streamSize) {
+ return StreamSupport.stream
+ (makeLeapsSpliterator(0L, streamSize, defaultLeapDistance()),
+ false);
+ }
+
+
+ /**
+ * Spliterator for int streams. We multiplex the four int
+ * versions into one class by treating a bound less than origin as
+ * unbounded, and also by treating "infinite" as equivalent to
+ * Long.MAX_VALUE. For splits, we choose to override the method
+ * {@code trySplit()} to try to optimize execution speed: instead of
+ * dividing a range in half, it breaks off the largest possible chunk
+ * whose size is a power of two such that the remaining chunk is not
+ * empty. In this way, the necessary jump distances will tend to be
+ * powers of two. The long and double versions of this class are
+ * identical except for types.
+ */
+ static class RandomIntsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfInt {
+ final ArbitrarilyJumpableRng generatingRng;
+ final int origin;
+ final int bound;
+
+ RandomIntsSpliterator(ArbitrarilyJumpableRng generatingRng, long index, long fence, int origin, int bound) {
+ super(index, fence);
+ this.origin = origin; this.bound = bound;
+ this.generatingRng = generatingRng;
+ }
+
+ public Spliterator.OfInt trySplit() {
+ long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
+ if (m <= i) return null;
+ index = m;
+ ArbitrarilyJumpableRng r = (ArbitrarilyJumpableRng) generatingRng;
+ return new RandomIntsSpliterator(r.copyAndJump((double)delta), i, m, origin, bound);
+ }
+
+ public boolean tryAdvance(IntConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(RngSupport.boundedNextInt(generatingRng, origin, bound));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(IntConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ ArbitrarilyJumpableRng r = generatingRng;
+ int o = origin, b = bound;
+ do {
+ consumer.accept(RngSupport.boundedNextInt(r, o, b));
+ } while (++i < f);
+ }
+ }
+ }
+
+ /**
+ * Spliterator for long streams.
+ */
+ static class RandomLongsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfLong {
+ final ArbitrarilyJumpableRng generatingRng;
+ final long origin;
+ final long bound;
+
+ RandomLongsSpliterator(ArbitrarilyJumpableRng generatingRng, long index, long fence, long origin, long bound) {
+ super(index, fence);
+ this.generatingRng = generatingRng;
+ this.origin = origin; this.bound = bound;
+ }
+
+ public Spliterator.OfLong trySplit() {
+ long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
+ if (m <= i) return null;
+ index = m;
+ ArbitrarilyJumpableRng r = (ArbitrarilyJumpableRng) generatingRng;
+ return new RandomLongsSpliterator(r.copyAndJump((double)delta), i, m, origin, bound);
+ }
+
+ public boolean tryAdvance(LongConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(RngSupport.boundedNextLong(generatingRng, origin, bound));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(LongConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ ArbitrarilyJumpableRng r = generatingRng;
+ long o = origin, b = bound;
+ do {
+ consumer.accept(RngSupport.boundedNextLong(r, o, b));
+ } while (++i < f);
+ }
+ }
+ }
+
+ /**
+ * Spliterator for double streams.
+ */
+ static class RandomDoublesSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfDouble {
+ final ArbitrarilyJumpableRng generatingRng;
+ final double origin;
+ final double bound;
+
+ RandomDoublesSpliterator(ArbitrarilyJumpableRng generatingRng, long index, long fence, double origin, double bound) {
+ super(index, fence);
+ this.generatingRng = generatingRng;
+ this.origin = origin; this.bound = bound;
+ }
+
+ public Spliterator.OfDouble trySplit() {
+
+ long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
+ if (m <= i) return null;
+ index = m;
+ ArbitrarilyJumpableRng r = (ArbitrarilyJumpableRng) generatingRng;
+ return new RandomDoublesSpliterator(r.copyAndJump((double)delta), i, m, origin, bound);
+ }
+
+ public boolean tryAdvance(DoubleConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(RngSupport.boundedNextDouble(generatingRng, origin, bound));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(DoubleConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ ArbitrarilyJumpableRng r = generatingRng;
+ double o = origin, b = bound;
+ do {
+ consumer.accept(RngSupport.boundedNextDouble(r, o, b));
+ } while (++i < f);
+ }
+ }
+ }
+
+ // Spliterators for producing new generators by jumping or leaping. The
+ // complete implementation of each of these spliterators is right here.
+ // In the same manner as for the preceding spliterators, the method trySplit() is
+ // coded to optimize execution speed: instead of dividing a range
+ // in half, it breaks off the largest possible chunk whose
+ // size is a power of two such that the remaining chunk is not
+ // empty. In this way, the necessary jump distances will tend to be
+ // powers of two.
+
+ /**
+ * Spliterator for stream of generators of type Rng produced by jumps.
+ */
+ static class RandomJumpsSpliterator extends RngSupport.RandomSpliterator implements Spliterator<Rng> {
+ ArbitrarilyJumpableRng generatingRng;
+ final double distance;
+
+ RandomJumpsSpliterator(ArbitrarilyJumpableRng generatingRng, long index, long fence, double distance) {
+ super(index, fence);
+ this.generatingRng = generatingRng; this.distance = distance;
+ }
+
+ public Spliterator<Rng> trySplit() {
+ long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
+ if (m <= i) return null;
+ index = m;
+ ArbitrarilyJumpableRng r = (ArbitrarilyJumpableRng) generatingRng;
+ // Because delta is a power of two, (distance * (double)delta) can always be computed exactly.
+ return new RandomJumpsSpliterator(r.copyAndJump(distance * (double)delta), i, m, distance);
+ }
+
+ public boolean tryAdvance(Consumer<? super Rng> consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(generatingRng.copyAndJump(distance));
+ index = i + 1;
+ return true;
+ }
+ return false;
+ }
+
+ public void forEachRemaining(Consumer<? super Rng> consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ ArbitrarilyJumpableRng r = generatingRng;
+ do {
+ consumer.accept(r.copyAndJump(distance));
+ } while (++i < f);
+ }
+ }
+ }
+
+ /**
+ * Spliterator for stream of generators of type Rng produced by leaps.
+ */
+ static class RandomLeapsSpliterator extends RngSupport.RandomSpliterator implements Spliterator<JumpableRng> {
+ ArbitrarilyJumpableRng generatingRng;
+ final double distance;
+
+ RandomLeapsSpliterator(ArbitrarilyJumpableRng generatingRng, long index, long fence, double distance) {
+ super(index, fence);
+ this.generatingRng = generatingRng; this.distance = distance;
+ }
+
+ public Spliterator<JumpableRng> trySplit() {
+ long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
+ if (m <= i) return null;
+ index = m;
+ // Because delta is a power of two, (distance * (double)delta) can always be computed exactly.
+ return new RandomLeapsSpliterator(generatingRng.copyAndJump(distance * (double)delta), i, m, distance);
+ }
+
+ public boolean tryAdvance(Consumer<? super JumpableRng> consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(generatingRng.copyAndJump(distance));
+ index = i + 1;
+ return true;
+ }
+ return false;
+ }
+
+ public void forEachRemaining(Consumer<? super JumpableRng> consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ ArbitrarilyJumpableRng r = generatingRng;
+ do {
+ consumer.accept(r.copyAndJump(distance));
+ } while (++i < f);
+ }
+ }
+ }
+
+ /**
+ * Spliterator for stream of generators of type Rng produced by arbitrary jumps.
+ */
+ static class RandomArbitraryJumpsSpliterator extends RngSupport.RandomSpliterator implements Spliterator<ArbitrarilyJumpableRng> {
+ ArbitrarilyJumpableRng generatingRng;
+ final double distance;
+
+ RandomArbitraryJumpsSpliterator(ArbitrarilyJumpableRng generatingRng, long index, long fence, double distance) {
+ super(index, fence);
+ this.generatingRng = generatingRng; this.distance = distance;
+ }
+
+ public Spliterator<ArbitrarilyJumpableRng> trySplit() {
+ long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
+ if (m <= i) return null;
+ index = m;
+ // Because delta is a power of two, (distance * (double)delta) can always be computed exactly.
+ return new RandomArbitraryJumpsSpliterator(generatingRng.copyAndJump(distance * (double)delta), i, m, distance);
+ }
+
+ public boolean tryAdvance(Consumer<? super ArbitrarilyJumpableRng> consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(generatingRng.copyAndJump(distance));
+ index = i + 1;
+ return true;
+ }
+ return false;
+ }
+
+ public void forEachRemaining(Consumer<? super ArbitrarilyJumpableRng> consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ ArbitrarilyJumpableRng r = generatingRng;
+ do {
+ consumer.accept(r.copyAndJump(distance));
+ } while (++i < f);
+ }
+ }
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/AbstractSharedRng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,245 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.util.Spliterator;
+import java.util.function.Consumer;
+import java.util.function.IntConsumer;
+import java.util.function.LongConsumer;
+import java.util.function.DoubleConsumer;
+import java.util.stream.StreamSupport;
+import java.util.stream.Stream;
+
+/**
+ * This class provides much of the implementation of the {@code Rng}
+ * interface, to minimize the effort required to implement that interface.
+ *
+ * To implement a pseudorandom number generator, the programmer needs
+ * only to extend this class and provide implementations for the
+ * {@code nextInt()} and {@code nextLong()} methods. In order for
+ * the implementations of other methods in this class to operate
+ * correctly, it must be safe for multiple threads to call these
+ * methods on that same object. The principal purpose of this class
+ * is to support the implementations of {@code java.util.Random}
+ * and {@code java.util.concurrent.ThreadLocalRandom}, but it could
+ * in principle be used to implement others as well.
+ *
+ * (If the pseudorandom number generator has the ability to split or
+ * jump, then the programmer may wish to consider instead extending
+ * another abstract class, such as {@code AbstractSplittableRng},
+ * {@code AbstractJumpableRng}, {@code AbstractArbitrarilyJumpableRng},
+ * {@code AbstractSplittableJumpableRng}, or
+ * {@code AbstractSplittableArbitrarilyJumpableRng}.)
+ *
+ * The programmer should generally provide at least three constructors:
+ * one that takes no arguments, one that accepts a {@code long}
+ * seed value, and one that accepts an array of seed {@code byte} values.
+ * This class provides a public {@code initialSeed()} method that may
+ * be useful in initializing some static state from which to derive
+ * defaults seeds for use by the no-argument constructor.
+ *
+ * For the stream methods (such as {@code ints()} and {@code splits()}),
+ * this class provides {@code Spliterator}-based implementations that
+ * allow parallel execution when appropriate.
+ *
+ * The documentation for each non-abstract method in this class
+ * describes its implementation in detail. Each of these methods may
+ * be overridden if the pseudorandom number generator being
+ * implemented admits a more efficient implementation.
+ *
+ * @author Guy Steele
+ * @author Doug Lea
+ * @since 1.9
+ */
+public abstract class AbstractSharedRng extends AbstractSpliteratorRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * This class provides most of the "user API" methods needed to
+ * satisfy the interface java.util.Rng. Most of these methods
+ * are in turn inherited from AbstractRng and the non-public class
+ * AbstractSpliteratorRng; this file implements methods and spliterators
+ * necessary to support the latter.
+ *
+ * File organization: First some non-public methods, followed by
+ * some custom spliterator classes needed for stream methods.
+ */
+
+ // Methods required by class AbstractSpliteratorRng
+ Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
+ return new RandomIntsSpliterator(this, index, fence, origin, bound);
+ }
+ Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
+ return new RandomLongsSpliterator(this, index, fence, origin, bound);
+ }
+ Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
+ return new RandomDoublesSpliterator(this, index, fence, origin, bound);
+ }
+
+ // Spliterators for producing streams. These are based on abstract
+ // spliterator classes provided by class AbstractSpliteratorRng.
+ // Each one needs to define only a constructor and two methods.
+
+ static class RandomIntsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfInt {
+ final AbstractSharedRng generatingRng;
+ final int origin;
+ final int bound;
+
+ RandomIntsSpliterator(AbstractSharedRng generatingRng, long index, long fence, int origin, int bound) {
+ super(index, fence);
+ this.generatingRng = generatingRng;
+ this.origin = origin; this.bound = bound;
+ }
+
+ public Spliterator.OfInt trySplit() {
+ long i = index, m = (i + fence) >>> 1;
+ if (m <= i) return null;
+ index = m;
+ // The same generatingRng is used, with no splitting or copying.
+ return new RandomIntsSpliterator(generatingRng, i, m, origin, bound);
+ }
+
+ public boolean tryAdvance(IntConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(RngSupport.boundedNextInt(generatingRng, origin, bound));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(IntConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ Rng r = generatingRng;
+ int o = origin, b = bound;
+ do {
+ consumer.accept(RngSupport.boundedNextInt(r, o, b));
+ } while (++i < f);
+ }
+ }
+ }
+
+ /**
+ * Spliterator for long streams.
+ */
+ static class RandomLongsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfLong {
+ final AbstractSharedRng generatingRng;
+ final long origin;
+ final long bound;
+
+ RandomLongsSpliterator(AbstractSharedRng generatingRng, long index, long fence, long origin, long bound) {
+ super(index, fence);
+ this.generatingRng = generatingRng;
+ this.origin = origin; this.bound = bound;
+ }
+
+ public Spliterator.OfLong trySplit() {
+ long i = index, m = (i + fence) >>> 1;
+ if (m <= i) return null;
+ index = m;
+ // The same generatingRng is used, with no splitting or copying.
+ return new RandomLongsSpliterator(generatingRng, i, m, origin, bound);
+ }
+
+ public boolean tryAdvance(LongConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(RngSupport.boundedNextLong(generatingRng, origin, bound));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(LongConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ Rng r = generatingRng;
+ long o = origin, b = bound;
+ do {
+ consumer.accept(RngSupport.boundedNextLong(r, o, b));
+ } while (++i < f);
+ }
+ }
+ }
+
+ /**
+ * Spliterator for double streams.
+ */
+ static class RandomDoublesSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfDouble {
+ final AbstractSharedRng generatingRng;
+ final double origin;
+ final double bound;
+
+ RandomDoublesSpliterator(AbstractSharedRng generatingRng, long index, long fence, double origin, double bound) {
+ super(index, fence);
+ this.generatingRng = generatingRng;
+ this.origin = origin; this.bound = bound;
+ }
+
+ public Spliterator.OfDouble trySplit() {
+ long i = index, m = (i + fence) >>> 1;
+ if (m <= i) return null;
+ index = m;
+ // The same generatingRng is used, with no splitting or copying.
+ return new RandomDoublesSpliterator(generatingRng, i, m, origin, bound);
+ }
+
+ public boolean tryAdvance(DoubleConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(RngSupport.boundedNextDouble(generatingRng, origin, bound));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(DoubleConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ Rng r = generatingRng;
+ double o = origin, b = bound;
+ do {
+ consumer.accept(RngSupport.boundedNextDouble(r, o, b));
+ } while (++i < f);
+ }
+ }
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/AbstractSpliteratorRng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,318 @@
+/*
+ * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.util.Spliterator;
+import java.util.function.Consumer;
+import java.util.function.IntConsumer;
+import java.util.function.LongConsumer;
+import java.util.function.DoubleConsumer;
+import java.util.stream.StreamSupport;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.DoubleStream;
+
+/**
+ * This class overrides the stream-producing methods (such as {@code ints()})
+ * in class {@code AbstractRng} to provide {@code Spliterator}-based
+ * implmentations that support potentially parallel execution.
+ *
+ * To implement a pseudorandom number generator, the programmer needs
+ * only to extend this class and provide implementations for the methods
+ * {@code nextInt()}, {@code nextLong()}, {@code makeIntsSpliterator},
+ * {@code makeLongsSpliterator}, and {@code makeDoublesSpliterator}.
+ *
+ * This class is not public; it provides shared code to the public
+ * classes {@code AbstractSplittableRng}, {@code AbstractSharedRng},
+ * and {@code AbstractArbitrarilyJumpableRng}.
+ *
+ * @author Guy Steele
+ * @author Doug Lea
+ * @since 1.9
+ */
+
+abstract class AbstractSpliteratorRng implements Rng {
+ /*
+ * Implementation Overview.
+ *
+ * This class provides most of the "user API" methods needed to
+ * satisfy the interface java.util.Rng. An implementation of this
+ * interface need only extend this class and provide implementations
+ * of six methods: nextInt, nextLong, and nextDouble (the versions
+ * that take no arguments) and makeIntsSpliterator,
+ * makeLongsSpliterator, and makeDoublesSpliterator.
+ *
+ * File organization: First the non-public abstract methods needed
+ * to create spliterators, then the main public methods.
+ */
+
+ abstract Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound);
+ abstract Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound);
+ abstract Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound);
+
+ /* ---------------- public methods ---------------- */
+
+ // stream methods, coded in a way intended to better isolate for
+ // maintenance purposes the small differences across forms.
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number
+ * of pseudorandom {@code int} values from this generator and/or
+ * one split from it.
+ *
+ * @param streamSize the number of values to generate
+ * @return a stream of pseudorandom {@code int} values
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ public IntStream ints(long streamSize) {
+ RngSupport.checkStreamSize(streamSize);
+ return StreamSupport.intStream
+ (makeIntsSpliterator(0L, streamSize, Integer.MAX_VALUE, 0),
+ false);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandomly chosen
+ * {@code int} values.
+ *
+ * @implNote The implementation of this method is effectively
+ * equivalent to {@code ints(Long.MAX_VALUE)}.
+ *
+ * @return a stream of pseudorandomly chosen {@code int} values
+ */
+
+ public IntStream ints() {
+ return StreamSupport.intStream
+ (makeIntsSpliterator(0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0),
+ false);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number
+ * 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 (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 (inclusive) and bound (exclusive)
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero, or {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ public IntStream ints(long streamSize, int randomNumberOrigin,
+ int randomNumberBound) {
+ RngSupport.checkStreamSize(streamSize);
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return StreamSupport.intStream
+ (makeIntsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound),
+ false);
+ }
+
+ /**
+ * Returns an effectively unlimited stream 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).
+ *
+ * @implNote This method is implemented to be equivalent to {@code
+ * ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
+ *
+ * @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 (inclusive) and bound (exclusive)
+ * @throws IllegalArgumentException if {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return StreamSupport.intStream
+ (makeIntsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
+ false);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number
+ * of pseudorandom {@code long} values from this generator and/or
+ * one split from it.
+ *
+ * @param streamSize the number of values to generate
+ * @return a stream of pseudorandom {@code long} values
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ public LongStream longs(long streamSize) {
+ RngSupport.checkStreamSize(streamSize);
+ return StreamSupport.longStream
+ (makeLongsSpliterator(0L, streamSize, Long.MAX_VALUE, 0L),
+ false);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandom {@code
+ * long} values from this generator and/or one split from it.
+ *
+ * @implNote This method is implemented to be equivalent to {@code
+ * longs(Long.MAX_VALUE)}.
+ *
+ * @return a stream of pseudorandom {@code long} values
+ */
+ public LongStream longs() {
+ return StreamSupport.longStream
+ (makeLongsSpliterator(0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L),
+ false);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * 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 (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 (inclusive) and bound (exclusive)
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero, or {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ public LongStream longs(long streamSize, long randomNumberOrigin,
+ long randomNumberBound) {
+ RngSupport.checkStreamSize(streamSize);
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return StreamSupport.longStream
+ (makeLongsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound),
+ false);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandom {@code
+ * 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 (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 (inclusive) and bound (exclusive)
+ * @throws IllegalArgumentException if {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return StreamSupport.longStream
+ (makeLongsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
+ false);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * 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
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ public DoubleStream doubles(long streamSize) {
+ RngSupport.checkStreamSize(streamSize);
+ return StreamSupport.doubleStream
+ (makeDoublesSpliterator(0L, streamSize, Double.MAX_VALUE, 0.0),
+ false);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandom {@code
+ * 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)}.
+ *
+ * @return a stream of pseudorandom {@code double} values
+ */
+ public DoubleStream doubles() {
+ return StreamSupport.doubleStream
+ (makeDoublesSpliterator(0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0),
+ false);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * 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 (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 (inclusive) and bound (exclusive)
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * 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) {
+ RngSupport.checkStreamSize(streamSize);
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return StreamSupport.doubleStream
+ (makeDoublesSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound),
+ false);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandom {@code
+ * 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 (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 (inclusive) and bound (exclusive)
+ * @throws IllegalArgumentException if {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return StreamSupport.doubleStream
+ (makeDoublesSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
+ false);
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/AbstractSplittableRng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,379 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.util.function.Consumer;
+import java.util.function.IntConsumer;
+import java.util.function.LongConsumer;
+import java.util.function.DoubleConsumer;
+import java.util.Spliterator;
+import java.util.stream.StreamSupport;
+import java.util.stream.Stream;
+
+/**
+ * This class provides much of the implementation of the {@code SplittableRng}
+ * interface, to minimize the effort required to implement this interface.
+ *
+ * To implement a pseudorandom number generator, the programmer needs
+ * only to extend this class and provide implementations for the
+ * methods {@code nextInt()}, {@code nextLong()}, {@code period()},
+ * and {@code split(SplittableRng)}.
+ *
+ * (If the pseudorandom number generator also has the ability to jump,
+ * then the programmer may wish to consider instead extending
+ * the class {@code AbstractSplittableJumpableRng} or (if it can also leap)
+ * {@code AbstractSplittableLeapableRng}. But if the pseudorandom number
+ * generator furthermore has the ability to jump an arbitrary specified
+ * distance, then the programmer may wish to consider instead extending
+ * the class {@code * AbstractSplittableArbitrarilyJumpableRng}.)
+ *
+ * The programmer should generally provide at least three constructors:
+ * one that takes no arguments, one that accepts a {@code long}
+ * seed value, and one that accepts an array of seed {@code byte} values.
+ * This class provides a public {@code initialSeed()} method that may
+ * be useful in initializing some static state from which to derive
+ * defaults seeds for use by the no-argument constructor.
+ *
+ * For the stream methods (such as {@code ints()} and {@code splits()}),
+ * this class provides {@code Spliterator}-based implementations that
+ * allow parallel execution when appropriate.
+ *
+ * The documentation for each non-abstract method in this class
+ * describes its implementation in detail. Each of these methods may
+ * be overridden if the pseudorandom number generator being
+ * implemented admits a more efficient implementation.
+ *
+ * @author Guy Steele
+ * @author Doug Lea
+ * @since 1.9
+ */
+public abstract class AbstractSplittableRng extends AbstractSpliteratorRng implements SplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * This class provides most of the "user API" methods needed to
+ * satisfy the interface java.util.JumpableRng. Most of these methods
+ * are in turn inherited from AbstractRng and the non-public class
+ * AbstractSpliteratorRng; this file implements two versions of the
+ * splits method and defines the spliterators necessary to support
+ * them.
+ *
+ * The abstract split() method from interface SplittableRng is redeclared
+ * here so as to narrow the return type to AbstractSplittableRng.
+ *
+ * File organization: First the non-public methods needed by the class
+ * AbstractSpliteratorRng, then the main public methods, followed by some
+ * custom spliterator classes.
+ */
+
+ Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
+ return new RandomIntsSpliterator(this, index, fence, origin, bound);
+ }
+
+ Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
+ return new RandomLongsSpliterator(this, index, fence, origin, bound);
+ }
+
+ Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
+ return new RandomDoublesSpliterator(this, index, fence, origin, bound);
+ }
+
+ Spliterator<SplittableRng> makeSplitsSpliterator(long index, long fence, SplittableRng source) {
+ return new RandomSplitsSpliterator(source, index, fence, this);
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Implements the @code{split()} method as {@code this.split(this) }.
+ *
+ * @return the new {@code AbstractSplittableRng} instance
+ */
+ public SplittableRng split() { return this.split(this); }
+
+ // Stream methods for splittings
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code SplittableRng}
+ * interface.
+ *
+ * This pseudorandom number generator provides the
+ * entropy used to seed the new ones.
+ *
+ * @implNote This method is implemented to be equivalent to
+ * {@code splits(Long.MAX_VALUE)}.
+ *
+ * @return a stream of {@code SplittableRng} objects
+ */
+ public Stream<SplittableRng> splits() {
+ return this.splits(Long.MAX_VALUE, this);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code SplittableRng} interface.
+ *
+ * This pseudorandom number generator provides the
+ * entropy used to seed the new ones.
+ *
+ * @param streamSize the number of values to generate
+ * @return a stream of {@code SplittableRng} objects
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ public Stream<SplittableRng> splits(long streamSize) {
+ return this.splits(streamSize, this);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code SplittableRng}
+ * interface.
+ *
+ * @implNote This method is implemented to be equivalent to
+ * {@code splits(Long.MAX_VALUE)}.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a stream of {@code SplittableRng} objects
+ */
+ public Stream<SplittableRng> splits(SplittableRng source) {
+ return this.splits(Long.MAX_VALUE, source);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code SplittableRng} interface.
+ *
+ * @param streamSize the number of values to generate
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a stream of {@code SplittableRng} objects
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ public Stream<SplittableRng> splits(long streamSize, SplittableRng source) {
+ RngSupport.checkStreamSize(streamSize);
+ return StreamSupport.stream(makeSplitsSpliterator(0L, streamSize, source), false);
+ }
+
+ /**
+ * Spliterator for int streams. We multiplex the four int
+ * versions into one class by treating a bound less than origin as
+ * unbounded, and also by treating "infinite" as equivalent to
+ * Long.MAX_VALUE. For splits, it uses the standard divide-by-two
+ * approach. The long and double versions of this class are
+ * identical except for types.
+ */
+ static class RandomIntsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfInt {
+ final SplittableRng generatingRng;
+ final int origin;
+ final int bound;
+
+ RandomIntsSpliterator(SplittableRng generatingRng, long index, long fence, int origin, int bound) {
+ super(index, fence);
+ this.generatingRng = generatingRng;
+ this.origin = origin; this.bound = bound;
+ }
+
+ public Spliterator.OfInt trySplit() {
+ long i = index, m = (i + fence) >>> 1;
+ if (m <= i) return null;
+ index = m;
+ return new RandomIntsSpliterator(generatingRng.split(), i, m, origin, bound);
+ }
+
+ public boolean tryAdvance(IntConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(RngSupport.boundedNextInt(generatingRng, origin, bound));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(IntConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ Rng r = generatingRng;
+ int o = origin, b = bound;
+ do {
+ consumer.accept(RngSupport.boundedNextInt(r, o, b));
+ } while (++i < f);
+ }
+ }
+ }
+
+ /**
+ * Spliterator for long streams.
+ */
+ static class RandomLongsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfLong {
+ final SplittableRng generatingRng;
+ final long origin;
+ final long bound;
+
+ RandomLongsSpliterator(SplittableRng generatingRng, long index, long fence, long origin, long bound) {
+ super(index, fence);
+ this.generatingRng = generatingRng;
+ this.origin = origin; this.bound = bound;
+ }
+
+ public Spliterator.OfLong trySplit() {
+ long i = index, m = (i + fence) >>> 1;
+ if (m <= i) return null;
+ index = m;
+ return new RandomLongsSpliterator(generatingRng.split(), i, m, origin, bound);
+ }
+
+ public boolean tryAdvance(LongConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(RngSupport.boundedNextLong(generatingRng, origin, bound));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(LongConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ Rng r = generatingRng;
+ long o = origin, b = bound;
+ do {
+ consumer.accept(RngSupport.boundedNextLong(r, o, b));
+ } while (++i < f);
+ }
+ }
+ }
+
+ /**
+ * Spliterator for double streams.
+ */
+ static class RandomDoublesSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfDouble {
+ final SplittableRng generatingRng;
+ final double origin;
+ final double bound;
+
+ RandomDoublesSpliterator(SplittableRng generatingRng, long index, long fence, double origin, double bound) {
+ super(index, fence);
+ this.generatingRng = generatingRng;
+ this.origin = origin; this.bound = bound;
+ }
+
+ public Spliterator.OfDouble trySplit() {
+ long i = index, m = (i + fence) >>> 1;
+ if (m <= i) return null;
+ index = m;
+ return new RandomDoublesSpliterator(generatingRng.split(), i, m, origin, bound);
+ }
+
+ public boolean tryAdvance(DoubleConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(RngSupport.boundedNextDouble(generatingRng, origin, bound));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(DoubleConsumer consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ Rng r = generatingRng;
+ double o = origin, b = bound;
+ do {
+ consumer.accept(RngSupport.boundedNextDouble(r, o, b));
+ } while (++i < f);
+ }
+ }
+ }
+
+ /**
+ * Spliterator for stream of generators of type SplittableRng. We multiplex the two
+ * versions into one class by treating "infinite" as equivalent to Long.MAX_VALUE.
+ * For splits, it uses the standard divide-by-two approach.
+ */
+ static class RandomSplitsSpliterator extends RngSupport.RandomSpliterator implements Spliterator<SplittableRng> {
+ final SplittableRng generatingRng;
+ final SplittableRng constructingRng;
+
+ RandomSplitsSpliterator(SplittableRng generatingRng, long index, long fence, SplittableRng constructingRng) {
+ super(index, fence);
+ this.generatingRng = generatingRng;
+ this.constructingRng = constructingRng;
+ }
+
+ public Spliterator<SplittableRng> trySplit() {
+ long i = index, m = (i + fence) >>> 1;
+ if (m <= i) return null;
+ index = m;
+ return new RandomSplitsSpliterator(generatingRng.split(), i, m, constructingRng);
+ }
+
+ public boolean tryAdvance(Consumer<? super SplittableRng> consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ consumer.accept(constructingRng.split(generatingRng));
+ index = i + 1;
+ return true;
+ }
+ else return false;
+ }
+
+ public void forEachRemaining(Consumer<? super SplittableRng> consumer) {
+ if (consumer == null) throw new NullPointerException();
+ long i = index, f = fence;
+ if (i < f) {
+ index = f;
+ SplittableRng c = constructingRng;
+ SplittableRng r = generatingRng;
+ do {
+ consumer.accept(c.split(r));
+ } while (++i < f);
+ }
+ }
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/ArbitrarilyJumpableRng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,175 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+// package java.util;
+
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+/**
+ * This interface is designed to provide a common protocol for objects
+ * that generate sequences of pseudorandom numbers (or Boolean values)
+ * and furthermore can easily <it>jump</it> to an arbitrarily specified
+ * distant point in the state cycle.
+ *
+ * <p>Ideally, all {@code ArbitrarilyJumpableRng} objects produced by
+ * iterative jumping from a single original {@code ArbtrarilyJumpableRng}
+ * object are statistically independent of one another and
+ * individually uniform, provided that they do not traverse
+ * overlapping portions of the state cycle. In practice, one must
+ * settle for some approximation to independence and uniformity. In
+ * particular, a specific implementation may assume that each
+ * generator in a stream produced by the {@code jumps} method is used
+ * to produce a number of values no larger than the jump distance
+ * specified. Implementors are advised to use algorithms whose period
+ * is at least 2<sup>127</sup>.
+ *
+ * <p>For many applications, it suffices to jump forward by a power of
+ * two or some small multiple of a power of two, but this power of two
+ * may not be representable as a {@code long} value. To avoid the
+ * use of {@code BigInteger} values as jump distances, {@code double}
+ * values are used instead.
+ *
+ * <p>Methods are provided to perform a single jump operation and also
+ * to produce a stream of generators produced from the original by
+ * iterative copying and jumping of internal state. A typical
+ * strategy for a multithreaded application is to create a single
+ * {@code ArbitrarilyJumpableRng} object, call its {@code jumps}
+ * method exactly once, and then parcel out generators from the
+ * resulting stream, one to each thread. However, each generator
+ * produced also has type {@code ArbitrarilyJumpableRng}; with care,
+ * different jump distances can be used to traverse the entire
+ * state cycle in various ways.
+ *
+ * <p>An implementation of the {@code ArbitrarilyJumpableRng} interface must
+ * provide concrete definitions for the methods {@code nextInt()},
+ * {@code nextLong}, {@code period()}, {@code copy()}, {@code jump(double)},
+ * {@code defaultJumpDistance()}, and {@code defaultLeapDistance()}.
+ * Default implementations are provided for all other methods.
+ * Perhaps the most convenient
+ * way to implement this interface is to extend the abstract class
+ * {@link java.util.AbstractArbitrarilyJumpableRng}, which provides
+ * spliterator-based implementations of the methods {@code ints}, {@code longs},
+ * {@code doubles}, {@code rngs}, {@code jumps}, and {@code leaps}.
+ *
+ * <p>Objects that implement {@code java.util.ArbitrarilyJumpableRng}
+ * are typically not cryptographically secure. Consider instead using
+ * {@link java.security.SecureRandom} to get a cryptographically
+ * secure pseudo-random number generator for use by
+ * security-sensitive applications.
+ *
+ * @author Guy Steele
+ * @since 1.9
+ */
+interface ArbitrarilyJumpableRng extends LeapableRng {
+ /**
+ * Returns a new generator whose internal state is an exact copy
+ * of this generator (therefore their future behavior should be
+ * identical if subjected to the same series of operations).
+ *
+ * @return a new object that is a copy of this generator
+ */
+ ArbitrarilyJumpableRng copy();
+
+ /**
+ * Alter the state of this pseudorandom number generator so as to
+ * jump forward a distance equal to 2<sup>{@code logDistance}</sup>
+ * within its state cycle.
+ *
+ * @param logDistance the base-2 logarithm of the distance to jump
+ * forward within the state cycle
+ * @throws IllegalArgumentException if {@code logDistance} is NaN
+ * or negative, or if 2<sup>{@code logDistance}</sup> is
+ * greater than the period of this generator
+ */
+ void jumpPowerOfTwo(int logDistance);
+
+ /**
+ * Alter the state of this pseudorandom number generator so as to
+ * jump forward a specified distance within its state cycle.
+ *
+ * @param distance the distance to jump forward within the state cycle
+ * @throws IllegalArgumentException if {@code distance} is Nan,
+ * negative, or greater than the period of this generator
+ */
+ void jump(double distance);
+
+ /**
+ * Alter the state of this pseudorandom number generator so as to
+ * jump forward a large, fixed distance (typically 2<sup>64</sup>
+ * or more) within its state cycle. The distance used is that
+ * returned by method {@code defaultJumpDistance()}.
+ */
+ default void jump() { jump(defaultJumpDistance()); }
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code ArbitrarilyJumpableRng}
+ * interface, produced by jumping copies of this generator
+ * by different integer multiples of the specified jump distance.
+ *
+ * @implNote This method is implemented to be equivalent to
+ * {@code jumps(Long.MAX_VALUE)}.
+ *
+ * @param distance a distance to jump forward within the state cycle
+ * @return a stream of objects that implement the {@code Rng} interface
+ */
+ default Stream<ArbitrarilyJumpableRng> jumps(double distance) {
+ return Stream.generate(() -> copyAndJump(distance)).sequential();
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code ArbitrarilyJumpableRng} interface, produced by jumping copies of this generator
+ * by different integer multiples of the specified jump distance.
+ *
+ * @param streamSize the number of generators to generate
+ * @param distance a distance to jump forward within the state cycle
+ * @return a stream of objects that implement the {@code Rng} interface
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ default Stream<ArbitrarilyJumpableRng> jumps(long streamSize, double distance) {
+ return jumps(distance).limit(streamSize);
+ }
+
+ /**
+ * Alter the state of this pseudorandom number generator so as to
+ * jump forward a very large, fixed distance (typically 2<sup>128</sup>
+ * or more) within its state cycle. The distance used is that
+ * returned by method {@code defaultJLeapDistance()}.
+ */
+ default void leap() { jump(defaultLeapDistance()); }
+
+ /**
+ * Copy this generator, jump this generator forward, then return the copy.
+ */
+ default ArbitrarilyJumpableRng copyAndJump(double distance) {
+ ArbitrarilyJumpableRng result = copy();
+ jump(distance);
+ return result;
+ }
+
+}
Binary file newrandom/Class Hierarchy.pdf has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/DoubleZigguratTables.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,510 @@
+// This Java source file is generated automatically by the program `create_ziggurat_tables.c`.
+
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ */
+
+class DoubleZigguratTables {
+
+ // Implementation support for modified-ziggurat implementation of nextExponential()
+
+ // Fraction of the area under the curve that lies outside the layer boxes: 0.0156
+ // Fraction of non-box area that lies in the tail of the distribution: 0.0330
+
+ static final int exponentialNumberOfLayers = 252;
+ static final int exponentialLayerMask = 0xff;
+ static final int exponentialAliasMask = 0xff;
+ static final int exponentialSignCorrectionMask = 0xff;
+ static final double exponentialX0 = 7.56927469414806264;
+ static final long exponentialConvexMargin = 853965788476313645L; // unscaled convex margin = 0.0926
+
+ // exponential_X[i] = length of ziggurat layer i for exponential distribution, scaled by 2**(-63)
+ static final double[] exponentialX = { // 253 entries, which is exponential_number_of_layers+1
+ 8.2066240675348816e-19, 7.3973732351607284e-19, 6.9133313377915293e-19, 6.5647358820964533e-19,
+ 6.2912539959818508e-19, 6.0657224129604964e-19, 5.8735276103737269e-19, 5.7058850528536941e-19,
+ 5.5570945691622390e-19, 5.4232438903743953e-19, 5.3015297696508776e-19, 5.1898739257708062e-19,
+ 5.0866922617998330e-19, 4.9907492938796469e-19, 4.9010625894449536e-19, 4.8168379010649187e-19,
+ 4.7374238653644714e-19, 4.6622795807196824e-19, 4.5909509017784048e-19, 4.5230527790658154e-19,
+ 4.4582558816353960e-19, 4.3962763126368381e-19, 4.3368675967106470e-19, 4.2798143618469714e-19,
+ 4.2249273027064889e-19, 4.1720391253464110e-19, 4.1210012522465616e-19, 4.0716811225869233e-19,
+ 4.0239599631006903e-19, 3.9777309342877357e-19, 3.9328975785334499e-19, 3.8893725129310323e-19,
+ 3.8470763218720385e-19, 3.8059366138180143e-19, 3.7658872138544730e-19, 3.7268674692030177e-19,
+ 3.6888216492248162e-19, 3.6516984248800068e-19, 3.6154504153287473e-19, 3.5800337915318032e-19,
+ 3.5454079284533432e-19, 3.5115350988784242e-19, 3.4783802030030962e-19, 3.4459105288907336e-19,
+ 3.4140955396563316e-19, 3.3829066838741162e-19, 3.3523172262289001e-19, 3.3223020958685874e-19,
+ 3.2928377502804472e-19, 3.2639020528202049e-19, 3.2354741622810815e-19, 3.2075344331080789e-19,
+ 3.1800643250478609e-19, 3.1530463211820845e-19, 3.1264638534265134e-19, 3.1003012346934211e-19,
+ 3.0745435970137301e-19, 3.0491768350005559e-19, 3.0241875541094565e-19, 2.9995630232144550e-19,
+ 2.9752911310742592e-19, 2.9513603463113224e-19, 2.9277596805684267e-19, 2.9044786545442563e-19,
+ 2.8815072666416712e-19, 2.8588359639906928e-19, 2.8364556156331615e-19, 2.8143574876779799e-19,
+ 2.7925332202553125e-19, 2.7709748061152879e-19, 2.7496745707320232e-19, 2.7286251537873397e-19,
+ 2.7078194919206054e-19, 2.6872508026419050e-19, 2.6669125693153442e-19, 2.6467985271278891e-19,
+ 2.6269026499668434e-19, 2.6072191381359757e-19, 2.5877424068465143e-19, 2.5684670754248168e-19,
+ 2.5493879571835479e-19, 2.5305000499077481e-19, 2.5117985269112710e-19, 2.4932787286227806e-19,
+ 2.4749361546638660e-19, 2.4567664563848669e-19, 2.4387654298267842e-19, 2.4209290090801527e-19,
+ 2.4032532600140538e-19, 2.3857343743505147e-19, 2.3683686640614648e-19, 2.3511525560671253e-19,
+ 2.3340825872163284e-19, 2.3171553995306794e-19, 2.3003677356958333e-19, 2.2837164347843482e-19,
+ 2.2671984281957174e-19, 2.2508107358001938e-19, 2.2345504622739592e-19, 2.2184147936140775e-19,
+ 2.2024009938224424e-19, 2.1865064017486842e-19, 2.1707284280826716e-19, 2.1550645524878675e-19,
+ 2.1395123208673778e-19, 2.1240693427550640e-19, 2.1087332888245875e-19, 2.0935018885097035e-19,
+ 2.0783729277295508e-19, 2.0633442467130712e-19, 2.0484137379170616e-19, 2.0335793440326865e-19,
+ 2.0188390560756090e-19, 2.0041909115551697e-19, 1.9896329927183254e-19, 1.9751634248643090e-19,
+ 1.9607803747261946e-19, 1.9464820489157862e-19, 1.9322666924284314e-19, 1.9181325872045647e-19,
+ 1.9040780507449479e-19, 1.8901014347767504e-19, 1.8762011239677479e-19, 1.8623755346860768e-19,
+ 1.8486231138030984e-19, 1.8349423375370566e-19, 1.8213317103353295e-19, 1.8077897637931708e-19,
+ 1.7943150556069476e-19, 1.7809061685599652e-19, 1.7675617095390567e-19, 1.7542803085801941e-19,
+ 1.7410606179414531e-19, 1.7279013112017240e-19, 1.7148010823836362e-19, 1.7017586450992059e-19,
+ 1.6887727317167824e-19, 1.6758420925479093e-19, 1.6629654950527621e-19, 1.6501417230628659e-19,
+ 1.6373695760198277e-19, 1.6246478682288560e-19, 1.6119754281258616e-19, 1.5993510975569615e-19,
+ 1.5867737310692309e-19, 1.5742421952115544e-19, 1.5617553678444595e-19, 1.5493121374578016e-19,
+ 1.5369114024951992e-19, 1.5245520706841019e-19, 1.5122330583703858e-19, 1.4999532898563561e-19,
+ 1.4877116967410352e-19, 1.4755072172615974e-19, 1.4633387956347966e-19, 1.4512053813972103e-19,
+ 1.4391059287430991e-19, 1.4270393958586506e-19, 1.4150047442513381e-19, 1.4030009380730888e-19,
+ 1.3910269434359025e-19, 1.3790817277185197e-19, 1.3671642588626657e-19, 1.3552735046573446e-19,
+ 1.3434084320095729e-19, 1.3315680061998685e-19, 1.3197511901207148e-19, 1.3079569434961214e-19,
+ 1.2961842220802957e-19, 1.2844319768333099e-19, 1.2726991530715219e-19, 1.2609846895903523e-19,
+ 1.2492875177568625e-19, 1.2376065605693940e-19, 1.2259407316813331e-19, 1.2142889343858445e-19,
+ 1.2026500605581765e-19, 1.1910229895518744e-19, 1.1794065870449425e-19, 1.1677997038316715e-19,
+ 1.1562011745554883e-19, 1.1446098163777869e-19, 1.1330244275772562e-19, 1.1214437860737343e-19,
+ 1.1098666478700728e-19, 1.0982917454048923e-19, 1.0867177858084351e-19, 1.0751434490529747e-19,
+ 1.0635673859884002e-19, 1.0519882162526621e-19, 1.0404045260457141e-19, 1.0288148657544097e-19,
+ 1.0172177474144965e-19, 1.0056116419943559e-19, 9.9399497648346677e-20, 9.8236613076667446e-20,
+ 9.7072343426320094e-20, 9.5906516230690634e-20, 9.4738953224154196e-20, 9.3569469920159036e-20,
+ 9.2397875154569468e-20, 9.1223970590556472e-20, 9.0047550180852874e-20, 8.8868399582647627e-20,
+ 8.7686295519767450e-20, 8.6501005086071005e-20, 8.5312284983141187e-20, 8.4119880684385214e-20,
+ 8.2923525516513420e-20, 8.1722939648034506e-20, 8.0517828972839211e-20, 7.9307883875099226e-20,
+ 7.8092777859524425e-20, 7.6872166028429042e-20, 7.5645683383965122e-20, 7.4412942930179128e-20,
+ 7.3173533545093332e-20, 7.1927017587631075e-20, 7.0672928197666785e-20, 6.9410766239500362e-20,
+ 6.8139996829256425e-20, 6.6860045374610234e-20, 6.5570293040210081e-20, 6.4270071533368528e-20,
+ 6.2958657080923559e-20, 6.1635263438143136e-20, 6.0299033732151700e-20, 5.8949030892850181e-20,
+ 5.7584226359885930e-20, 5.6203486669597397e-20, 5.4805557413499315e-20, 5.3389043909003295e-20,
+ 5.1952387717989917e-20, 5.0493837866338355e-20, 4.9011415222629489e-20, 4.7502867933366117e-20,
+ 4.5965615001265455e-20, 4.4396673897997565e-20, 4.2792566302148588e-20, 4.1149193273430015e-20,
+ 3.9461666762606287e-20, 3.7724077131401685e-20, 3.5929164086204360e-20, 3.4067836691100565e-20,
+ 3.2128447641564046e-20, 3.0095646916399994e-20, 2.7948469455598328e-20, 2.5656913048718645e-20,
+ 2.3175209756803909e-20, 2.0426695228251291e-20, 1.7261770330213485e-20, 1.3281889259442578e-20,
+ 0.0000000000000000e+00 };
+
+ // exponential_Y[i] = value of the exponential distribution function at exponential_X[i], scaled by 2**(-63)
+ static final double[] exponentialY = { // 253 entries, which is exponential_number_of_layers+1
+ 5.5952054951127360e-23, 1.1802509982703313e-22, 1.8444423386735829e-22, 2.5439030466698309e-22,
+ 3.2737694311509334e-22, 4.0307732132706715e-22, 4.8125478319495115e-22, 5.6172914896583308e-22,
+ 6.4435820540443526e-22, 7.2902662343463681e-22, 8.1563888456321941e-22, 9.0411453683482223e-22,
+ 9.9438488486399206e-22, 1.0863906045969114e-21, 1.1800799775461269e-21, 1.2754075534831208e-21,
+ 1.3723331176377290e-21, 1.4708208794375214e-21, 1.5708388257440445e-21, 1.6723581984374566e-21,
+ 1.7753530675030514e-21, 1.8797999785104595e-21, 1.9856776587832504e-21, 2.0929667704053244e-21,
+ 2.2016497009958240e-21, 2.3117103852306179e-21, 2.4231341516125464e-21, 2.5359075901420891e-21,
+ 2.6500184374170538e-21, 2.7654554763660391e-21, 2.8822084483468604e-21, 3.0002679757547711e-21,
+ 3.1196254936130377e-21, 3.2402731888801749e-21, 3.3622039464187092e-21, 3.4854113007409036e-21,
+ 3.6098893927859475e-21, 3.7356329310971768e-21, 3.8626371568620053e-21, 3.9908978123552837e-21,
+ 4.1204111123918948e-21, 4.2511737184488913e-21, 4.3831827151633737e-21, 4.5164355889510656e-21,
+ 4.6509302085234806e-21, 4.7866648071096003e-21, 4.9236379662119969e-21, 5.0618486007478993e-21,
+ 5.2012959454434732e-21, 5.3419795423648946e-21, 5.4838992294830959e-21, 5.6270551301806347e-21,
+ 5.7714476436191935e-21, 5.9170774358950678e-21, 6.0639454319177027e-21, 6.2120528079531677e-21,
+ 6.3614009847804375e-21, 6.5119916214136427e-21, 6.6638266093481696e-21, 6.8169080672926277e-21,
+ 6.9712383363524377e-21, 7.1268199756340822e-21, 7.2836557582420336e-21, 7.4417486676430174e-21,
+ 7.6011018943746355e-21, 7.7617188330775411e-21, 7.9236030798322572e-21, 8.0867584297834842e-21,
+ 8.2511888750363333e-21, 8.4168986028103258e-21, 8.5838919938383098e-21, 8.7521736209986459e-21,
+ 8.9217482481700712e-21, 9.0926208292996504e-21, 9.2647965076751277e-21, 9.4382806153938292e-21,
+ 9.6130786730210328e-21, 9.7891963894314161e-21, 9.9666396618278840e-21, 1.0145414575932636e-20,
+ 1.0325527406345955e-20, 1.0506984617068672e-20, 1.0689792862184811e-20, 1.0873958986701341e-20,
+ 1.1059490027542400e-20, 1.1246393214695825e-20, 1.1434675972510121e-20, 1.1624345921140471e-20,
+ 1.1815410878142659e-20, 1.2007878860214202e-20, 1.2201758085082226e-20, 1.2397056973538040e-20,
+ 1.2593784151618565e-20, 1.2791948452935152e-20, 1.2991558921150600e-20, 1.3192624812605428e-20,
+ 1.3395155599094805e-20, 1.3599160970797774e-20, 1.3804650839360727e-20, 1.4011635341137284e-20,
+ 1.4220124840587164e-20, 1.4430129933836705e-20, 1.4641661452404201e-20, 1.4854730467093280e-20,
+ 1.5069348292058084e-20, 1.5285526489044050e-20, 1.5503276871808626e-20, 1.5722611510726402e-20,
+ 1.5943542737583543e-20, 1.6166083150566702e-20, 1.6390245619451956e-20, 1.6616043290999594e-20,
+ 1.6843489594561079e-20, 1.7072598247904713e-20, 1.7303383263267072e-20, 1.7535858953637607e-20,
+ 1.7770039939284241e-20, 1.8005941154528286e-20, 1.8243577854777398e-20, 1.8482965623825808e-20,
+ 1.8724120381431627e-20, 1.8967058391181452e-20, 1.9211796268653192e-20, 1.9458350989888484e-20,
+ 1.9706739900186868e-20, 1.9956980723234356e-20, 2.0209091570579904e-20, 2.0463090951473895e-20,
+ 2.0718997783083593e-20, 2.0976831401101350e-20, 2.1236611570762130e-20, 2.1498358498287976e-20,
+ 2.1762092842777868e-20, 2.2027835728562592e-20, 2.2295608758045219e-20, 2.2565434025049041e-20,
+ 2.2837334128696004e-20, 2.3111332187840010e-20, 2.3387451856080863e-20, 2.3665717337386111e-20,
+ 2.3946153402349610e-20, 2.4228785405117410e-20, 2.4513639301013211e-20, 2.4800741664897764e-20,
+ 2.5090119710298442e-20, 2.5381801309347597e-20, 2.5675815013570500e-20, 2.5972190075566336e-20,
+ 2.6270956471628253e-20, 2.6572144925351523e-20, 2.6875786932281841e-20, 2.7181914785659148e-20,
+ 2.7490561603315974e-20, 2.7801761355793055e-20, 2.8115548895739172e-20, 2.8431959988666534e-20,
+ 2.8751031345137833e-20, 2.9072800654466307e-20, 2.9397306620015486e-20, 2.9724588996191657e-20,
+ 3.0054688627228112e-20, 3.0387647487867642e-20, 3.0723508726057078e-20, 3.1062316707775905e-20,
+ 3.1404117064129991e-20, 3.1748956740850969e-20, 3.2096884050352357e-20, 3.2447948726504914e-20,
+ 3.2802201982306013e-20, 3.3159696570631373e-20, 3.3520486848272230e-20, 3.3884628843476888e-20,
+ 3.4252180327233346e-20, 3.4623200888548644e-20, 3.4997752014001677e-20, 3.5375897171869060e-20,
+ 3.5757701901149035e-20, 3.6143233905835799e-20, 3.6532563154827400e-20, 3.6925761987883572e-20,
+ 3.7322905228086981e-20, 3.7724070301302117e-20, 3.8129337363171041e-20, 3.8538789434235234e-20,
+ 3.8952512543827862e-20, 3.9370595883442399e-20, 3.9793131970351439e-20, 4.0220216822325769e-20,
+ 4.0651950144388133e-20, 4.1088435528630944e-20, 4.1529780668232712e-20, 4.1976097586926582e-20,
+ 4.2427502885307452e-20, 4.2884118005513604e-20, 4.3346069515987453e-20, 4.3813489418210257e-20,
+ 4.4286515477520838e-20, 4.4765291580372353e-20, 4.5249968120658306e-20, 4.5740702418054417e-20,
+ 4.6237659171683015e-20, 4.6741010952818368e-20, 4.7250938740823415e-20, 4.7767632507051219e-20,
+ 4.8291291852069895e-20, 4.8822126702292804e-20, 4.9360358072933852e-20, 4.9906218905182021e-20,
+ 5.0459954986625539e-20, 5.1021825965285324e-20, 5.1592106469178258e-20, 5.2171087345169234e-20,
+ 5.2759077033045284e-20, 5.3356403093325858e-20, 5.3963413910399511e-20, 5.4580480596259246e-20,
+ 5.5207999124535584e-20, 5.5846392729873830e-20, 5.6496114614193770e-20, 5.7157651009290713e-20,
+ 5.7831524654956632e-20, 5.8518298763794323e-20, 5.9218581558791713e-20, 5.9933031488338700e-20,
+ 6.0662363246796887e-20, 6.1407354758435000e-20, 6.2168855320499763e-20, 6.2947795150103727e-20,
+ 6.3745196643214394e-20, 6.4562187737537985e-20, 6.5400017881889097e-20, 6.6260077263309343e-20,
+ 6.7143920145146620e-20, 6.8053293447301698e-20, 6.8990172088133000e-20, 6.9956803158564498e-20,
+ 7.0955761794878430e-20, 7.1990022788945080e-20, 7.3063053739105458e-20, 7.4178938266266893e-20,
+ 7.5342542134173124e-20, 7.6559742171142969e-20, 7.7837749863412850e-20, 7.9185582674029512e-20,
+ 8.0614775537353300e-20, 8.2140502769818073e-20, 8.3783445978280519e-20, 8.5573129249678161e-20,
+ 8.7554459669590100e-20, 8.9802388057706877e-20, 9.2462471421151086e-20, 9.5919641344951721e-20,
+ 1.0842021724855044e-19 };
+
+ // alias_threshold[j] is a threshold for the probability mass function that has been
+ // scaled by (2**64 - 1), translated by -(2**63), and represented as a long value;
+ // in this way it can be directly compared to a randomly chosen long value.
+ static final long[] exponentialAliasThreshold = { // 256 entries
+ 9223372036854775807L, 1623796909450829958L, 2664290944894281002L, 7387971354164055035L,
+ 6515064486552722205L, 8840508362680707094L, 6099647593382923818L, 7673130333659514446L,
+ 6220332867583438718L, 5045979640552814279L, 4075305837223956071L, 3258413672162525964L,
+ 2560664887087763045L, 1957224924672900129L, 1429800935350578000L, 964606309710808688L,
+ 551043923599587587L, 180827629096889062L, -152619738120023316L, -454588624410291246L,
+ -729385126147774679L, -980551509819444511L, -1211029700667463575L, -1423284293868546830L,
+ -1619396356369066372L, -1801135830956194794L, -1970018048575634032L, -2127348289059688469L,
+ -2274257249303687482L, -2411729520096654942L, -2540626634159182211L, -2661705860113406183L,
+ -2775635634532464842L, -2883008316030448462L, -2984350790383654449L, -3080133339198118132L,
+ -3170777096303105047L, -3256660348483802362L, -3338123885075135933L, -3415475560473298752L,
+ -3488994201966444258L, -3558932970354456420L, -3625522261068040742L, -3688972217741991689L,
+ -3749474917563779627L, -3807206277531072172L, -3862327722496826830L, -3914987649156779312L,
+ -3965322714631864882L, -4013458973776911635L, -4059512885612766613L, -4103592206186240662L,
+ -4145796782586127736L, -4186219260694346585L, -4224945717447274810L, -4262056226866285147L,
+ -4297625367836519229L, -4331722680528536958L, -4364413077437472159L, -4395757214229421760L,
+ -4425811824915119137L, -4454630025296932322L, -4482261588141294467L, -4508753193105275908L,
+ -4534148654077813412L, -4558489126279965349L, -4581813295192216486L, -4604157549138252679L,
+ -4625556137145250151L, -4646041313519109096L, -4665643470413305673L, -4684391259530342697L,
+ -4702311703971745066L, -4719430301145102986L, -4735771117539946027L, -4751356876102086987L,
+ -4766209036859150188L, -4780347871385996716L, -4793792531638885869L, -4806561113635132333L,
+ -4818670716409312334L, -4830137496634465358L, -4840976719260854030L, -4851202804490332239L,
+ -4860829371376476047L, -4869869278311650511L, -4878334660640770576L, -4886236965617426832L,
+ -4893586984900802224L, -4900394884772702384L, -4906670234238884945L, -4912422031164489009L,
+ -4917658726580135697L, -4922388247283531793L, -4926618016851042065L, -4930354975163351025L,
+ -4933605596540650674L, -4936375906575303186L, -4938671497741357106L, -4940497543854583186L,
+ -4941858813449628882L, -4942759682136114354L, -4943204143989086194L, -4943195822025527282L,
+ -4942737977813222130L, -4941833520255011698L, -4940485013586759090L, -4938694684624342322L,
+ -4936464429291795314L, -4933795818458824946L, -4930690103114057265L, -4927148218896863345L,
+ -4923170790008291569L, -4918758132519196401L, -4913910257091661489L, -4908626871126522161L,
+ -4902907380349538608L, -4896750889844272240L, -4890156204540530416L, -4883121829162570096L,
+ -4875645967641780528L, -4867726521994909999L, -4859361090668119087L, -4850546966345102383L,
+ -4841281133215538414L, -4831560263698491374L, -4821380714613452974L, -4810738522790065581L,
+ -4799629400105481389L, -4788048727936296621L, -4775991551010524588L, -4763452570642113772L,
+ -4750426137329493931L, -4736906242696388587L, -4722886510751367403L, -4708360188440104938L,
+ -4693320135461420394L, -4677758813316098089L, -4661668273553495721L, -4645040145179234152L,
+ -4627865621182771687L, -4610135444140936871L, -4591839890849352486L, -4572968755929944934L,
+ -4553511334358213029L, -4533456402849109028L, -4512792200036270244L, -4491506405372580067L,
+ -4469586116675401954L, -4447017826233099938L, -4423787395382284961L, -4399880027458416864L,
+ -4375280239014124063L, -4349971829190464606L, -4323937847117722654L, -4297160557210942813L,
+ -4269621402214950684L, -4241300963840750107L, -4212178920821854874L, -4182234004204445017L,
+ -4151443949668869272L, -4119785446662323159L, -4087234084103169942L, -4053764292396165205L,
+ -4019349281473092435L, -3983960974549686930L, -3947569937258414993L, -3910145301787337104L,
+ -3871654685619049615L, -3832064104425389837L, -3791337878631529676L, -3749438533114328651L,
+ -3706326689447979465L, -3661960950051859912L, -3616297773528542022L, -3569291340409179909L,
+ -3520893408440947267L, -3471053156460649921L, -3419717015797783872L, -3366828488034801534L,
+ -3312327947826461820L, -3256152429334023226L, -3198235394669709240L, -3138506482563174262L,
+ -3076891235255164340L, -3013310801389715890L, -2947681612411392816L, -2879915029671670702L,
+ -2809916959107519276L, -2737587429961855017L, -2662820133571332903L, -2585501917733374884L,
+ -2505512231579392929L, -2422722515205190175L, -2336995527534106140L, -2248184604988712345L,
+ -2156132842510782614L, -2060672187261016979L, -1961622433929380112L, -1858790108950090508L,
+ -1751967229002904073L, -1640929916937134981L, -1525436855617591297L, -1405227557075245821L,
+ -1280020420662651897L, -1149510549536605301L, -1013367289578706928L, -871231448632089708L,
+ -722712146453677415L, -567383236774421729L, -404779231966956764L, -234390647591531478L,
+ -55658667960121553L, 132030985907831093L, 329355128892817467L, 537061298001091010L,
+ 755977262693561929L, 987022116608030929L, 1231219266829437401L, 1489711711346524770L,
+ 1763780090187559275L, 2054864117341782772L, 2364588157623782527L, 2694791916990482441L,
+ 3047567482883491349L, 3425304305830820514L, 3830744187097285423L, 4267048975685836605L,
+ 4737884547990014029L, 5247525842199011422L, 5800989391535342064L, 6404202162993303300L,
+ 7064218894258526746L, 7789505049452354354L, 8590309807749425484L, 7643763810684501605L,
+ 8891950541491453167L, 5457384281016234818L, 9083704440929285914L, 7976211653914461751L,
+ 8178631350487124609L, 2821287825726757492L, 6322989683301736617L, 4309503753387630347L,
+ 4685170734960191673L, 8404845967535252693L, 7330522972447610419L, 1960945799077061994L,
+ 4742910674644933674L, -751799822533438695L, 7023456603742021660L, 3843116882594755262L,
+ 3927231442413889375L, -9223372036854775807L, -9223372036854775807L, -9223372036854775807L };
+
+ static final byte[] exponentialAliasMap = { // 256 entries
+ (byte) 0, (byte) 0, (byte) 1, (byte)235, (byte) 3, (byte) 4, (byte) 5, (byte) 0,
+ (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
+ (byte) 0, (byte) 0, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 2, (byte) 2,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)251, (byte)251, (byte)251, (byte)251, (byte)251, (byte)251, (byte)251,
+ (byte)251, (byte)251, (byte)251, (byte)251, (byte)251, (byte)251, (byte)250, (byte)250,
+ (byte)250, (byte)250, (byte)250, (byte)250, (byte)250, (byte)249, (byte)249, (byte)249,
+ (byte)249, (byte)249, (byte)249, (byte)248, (byte)248, (byte)248, (byte)248, (byte)247,
+ (byte)247, (byte)247, (byte)247, (byte)246, (byte)246, (byte)246, (byte)245, (byte)245,
+ (byte)244, (byte)244, (byte)243, (byte)243, (byte)242, (byte)241, (byte)241, (byte)240,
+ (byte)239, (byte)237, (byte) 3, (byte) 3, (byte) 4, (byte) 4, (byte) 6, (byte) 0,
+ (byte) 0, (byte) 0, (byte) 0, (byte)236, (byte)237, (byte)238, (byte)239, (byte)240,
+ (byte)241, (byte)242, (byte)243, (byte)244, (byte)245, (byte)246, (byte)247, (byte)248,
+ (byte)249, (byte)250, (byte)251, (byte)252, (byte) 2, (byte) 0, (byte) 0, (byte) 0 };
+
+ // Implementation support for modified-ziggurat implementation of nextGaussian()
+
+ // Fraction of the area under the curve that lies outside the layer boxes: 0.0117
+ // Fraction of non-box area that lies in the tail of the distribution: 0.0236
+
+ static final int normalNumberOfLayers = 253;
+ static final int normalLayerMask = 0xff;
+ static final int normalAliasMask = 0xff;
+ static final int normalSignCorrectionMask = 0xff;
+ static final double normalX0 = 3.63600662550094578;
+ static final int normalInflectionIndex = 204;
+ static final long normalConvexMargin = 760463704284035183L; // unscaled convex margin = 0.0824
+ static final long normalConcaveMargin = 2269182951627976012L; // unscaled concave margin = 0.2460
+
+ // normal_X[i] = length of ziggurat layer i for normal distribution, scaled by 2**(-63)
+ static final double[] normalX = { // 254 entries, which is normal_number_of_layers+1
+ 3.9421662825398133e-19, 3.7204945004119012e-19, 3.5827024480628678e-19, 3.4807476236540249e-19,
+ 3.3990177171882136e-19, 3.3303778360340139e-19, 3.2709438817617550e-19, 3.2183577132495100e-19,
+ 3.1710758541840432e-19, 3.1280307407034065e-19, 3.0884520655804019e-19, 3.0517650624107352e-19,
+ 3.0175290292584600e-19, 2.9853983440705320e-19, 2.9550967462801797e-19, 2.9263997988491663e-19,
+ 2.8991225869977476e-19, 2.8731108780226291e-19, 2.8482346327101335e-19, 2.8243831535194389e-19,
+ 2.8014613964727031e-19, 2.7793871261807797e-19, 2.7580886921411212e-19, 2.7375032698308758e-19,
+ 2.7175754543391047e-19, 2.6982561247538484e-19, 2.6795015188771505e-19, 2.6612724730440033e-19,
+ 2.6435337927976633e-19, 2.6262537282028438e-19, 2.6094035335224142e-19, 2.5929570954331002e-19,
+ 2.5768906173214726e-19, 2.5611823497719608e-19, 2.5458123593393361e-19, 2.5307623292372459e-19,
+ 2.5160153867798400e-19, 2.5015559533646191e-19, 2.4873696135403158e-19, 2.4734430003079206e-19,
+ 2.4597636942892726e-19, 2.4463201347912450e-19, 2.4331015411139206e-19, 2.4200978427132955e-19,
+ 2.4072996170445879e-19, 2.3946980340903347e-19, 2.3822848067252674e-19, 2.3700521461931801e-19,
+ 2.3579927220741330e-19, 2.3460996262069972e-19, 2.3343663401054455e-19, 2.3227867054673840e-19,
+ 2.3113548974303765e-19, 2.3000654002704238e-19, 2.2889129852797606e-19, 2.2778926905921897e-19,
+ 2.2669998027527321e-19, 2.2562298398527416e-19, 2.2455785360727260e-19, 2.2350418274933911e-19,
+ 2.2246158390513294e-19, 2.2142968725296249e-19, 2.2040813954857555e-19, 2.1939660310297601e-19,
+ 2.1839475483749618e-19, 2.1740228540916853e-19, 2.1641889840016519e-19, 2.1544430956570613e-19,
+ 2.1447824613540345e-19, 2.1352044616350571e-19, 2.1257065792395107e-19, 2.1162863934653125e-19,
+ 2.1069415749082026e-19, 2.0976698805483467e-19, 2.0884691491567363e-19, 2.0793372969963634e-19,
+ 2.0702723137954107e-19, 2.0612722589717129e-19, 2.0523352580895635e-19, 2.0434594995315797e-19,
+ 2.0346432313698148e-19, 2.0258847584216418e-19, 2.0171824394771313e-19, 2.0085346846857531e-19,
+ 1.9999399530912015e-19, 1.9913967503040585e-19, 1.9829036263028144e-19, 1.9744591733545175e-19,
+ 1.9660620240469857e-19, 1.9577108494251485e-19, 1.9494043572246307e-19, 1.9411412901962161e-19,
+ 1.9329204245152935e-19, 1.9247405682708168e-19, 1.9166005600287074e-19, 1.9084992674649826e-19,
+ 1.9004355860642340e-19, 1.8924084378793725e-19, 1.8844167703488436e-19, 1.8764595551677749e-19,
+ 1.8685357872097450e-19, 1.8606444834960934e-19, 1.8527846822098793e-19, 1.8449554417517928e-19,
+ 1.8371558398354868e-19, 1.8293849726199566e-19, 1.8216419538767393e-19, 1.8139259141898448e-19,
+ 1.8062360001864453e-19, 1.7985713737964743e-19, 1.7909312115393845e-19, 1.7833147038364200e-19,
+ 1.7757210543468428e-19, 1.7681494793266395e-19, 1.7605992070083140e-19, 1.7530694770004409e-19,
+ 1.7455595397057217e-19, 1.7380686557563475e-19, 1.7305960954655264e-19, 1.7231411382940904e-19,
+ 1.7157030723311378e-19, 1.7082811937877138e-19, 1.7008748065025788e-19, 1.6934832214591352e-19,
+ 1.6861057563126349e-19, 1.6787417349268046e-19, 1.6713904869190636e-19, 1.6640513472135291e-19,
+ 1.6567236556010242e-19, 1.6494067563053266e-19, 1.6420999975549115e-19, 1.6348027311594532e-19,
+ 1.6275143120903661e-19, 1.6202340980646725e-19, 1.6129614491314931e-19, 1.6056957272604589e-19,
+ 1.5984362959313479e-19, 1.5911825197242491e-19, 1.5839337639095554e-19, 1.5766893940370800e-19,
+ 1.5694487755235889e-19, 1.5622112732380261e-19, 1.5549762510837070e-19, 1.5477430715767271e-19,
+ 1.5405110954198330e-19, 1.5332796810709688e-19, 1.5260481843056974e-19, 1.5188159577726683e-19,
+ 1.5115823505412761e-19, 1.5043467076406199e-19, 1.4971083695888395e-19, 1.4898666719118714e-19,
+ 1.4826209446506113e-19, 1.4753705118554365e-19, 1.4681146910669830e-19, 1.4608527927820112e-19,
+ 1.4535841199031451e-19, 1.4463079671711862e-19, 1.4390236205786415e-19, 1.4317303567630177e-19,
+ 1.4244274423783481e-19, 1.4171141334433217e-19, 1.4097896746642792e-19, 1.4024532987312287e-19,
+ 1.3951042255849034e-19, 1.3877416616527576e-19, 1.3803647990516385e-19, 1.3729728147547174e-19,
+ 1.3655648697200824e-19, 1.3581401079782068e-19, 1.3506976556752901e-19, 1.3432366200692418e-19,
+ 1.3357560884748263e-19, 1.3282551271542047e-19, 1.3207327801488087e-19, 1.3131880680481524e-19,
+ 1.3056199866908076e-19, 1.2980275057923788e-19, 1.2904095674948608e-19, 1.2827650848312727e-19,
+ 1.2750929400989213e-19, 1.2673919831340482e-19, 1.2596610294799512e-19, 1.2518988584399374e-19,
+ 1.2441042110056523e-19, 1.2362757876504165e-19, 1.2284122459762072e-19, 1.2205121982017852e-19,
+ 1.2125742084782245e-19, 1.2045967900166973e-19, 1.1965784020118020e-19, 1.1885174463419555e-19,
+ 1.1804122640264091e-19, 1.1722611314162064e-19, 1.1640622560939109e-19, 1.1558137724540874e-19,
+ 1.1475137369333185e-19, 1.1391601228549047e-19, 1.1307508148492592e-19, 1.1222836028063025e-19,
+ 1.1137561753107903e-19, 1.1051661125053526e-19, 1.0965108783189755e-19, 1.0877878119905372e-19,
+ 1.0789941188076655e-19, 1.0701268599703640e-19, 1.0611829414763286e-19, 1.0521591019102928e-19,
+ 1.0430518990027552e-19, 1.0338576948035472e-19, 1.0245726392923699e-19, 1.0151926522209310e-19,
+ 1.0057134029488235e-19, 9.9613028799672809e-20, 9.8643840599459914e-20, 9.7663252964755816e-20,
+ 9.6670707427623454e-20, 9.5665606240866670e-20, 9.4647308380433213e-20, 9.3615125017323508e-20,
+ 9.2568314370887282e-20, 9.1506075837638774e-20, 9.0427543267725716e-20, 8.9331777233763680e-20,
+ 8.8217756102327883e-20, 8.7084365674892319e-20, 8.5930387109612162e-20, 8.4754482764244349e-20,
+ 8.3555179508462343e-20, 8.2330848933585364e-20, 8.1079683729129853e-20, 7.9799669284133864e-20,
+ 7.8488549286072745e-20, 7.7143783700934692e-20, 7.5762496979467566e-20, 7.4341413578485329e-20,
+ 7.2876776807378431e-20, 7.1364245443525374e-20, 6.9798760240761066e-20, 6.8174368944799054e-20,
+ 6.6483992986198539e-20, 6.4719110345162767e-20, 6.2869314813103699e-20, 6.0921687548281263e-20,
+ 5.8859873575576818e-20, 5.6662675116090981e-20, 5.4301813630894571e-20, 5.1738171744494220e-20,
+ 4.8915031722398545e-20, 4.5744741890755301e-20, 4.2078802568583416e-20, 3.7625986722404761e-20,
+ 3.1628589805881879e-20, 0.0000000000000000e+00 };
+
+ // normal_Y[i] = value of the normal distribution function at normal_X[i], scaled by 2**(-63)
+ static final double[] normalY = { // 254 entries, which is normal_number_of_layers+1
+ 1.4598410796619063e-22, 3.0066613427942797e-22, 4.6129728815103466e-22, 6.2663350049234362e-22,
+ 7.9594524761881544e-22, 9.6874655021705039e-22, 1.1446877002379439e-21, 1.3235036304379167e-21,
+ 1.5049857692053131e-21, 1.6889653000719298e-21, 1.8753025382711626e-21, 2.0638798423695191e-21,
+ 2.2545966913644708e-21, 2.4473661518801799e-21, 2.6421122727763533e-21, 2.8387681187879908e-21,
+ 3.0372742567457284e-21, 3.2375775699986589e-21, 3.4396303157948780e-21, 3.6433893657997798e-21,
+ 3.8488155868912312e-21, 4.0558733309492775e-21, 4.2645300104283590e-21, 4.4747557422305067e-21,
+ 4.6865230465355582e-21, 4.8998065902775257e-21, 5.1145829672105489e-21, 5.3308305082046173e-21,
+ 5.5485291167031758e-21, 5.7676601252690476e-21, 5.9882061699178461e-21, 6.2101510795442221e-21,
+ 6.4334797782257209e-21, 6.6581781985713897e-21, 6.8842332045893181e-21, 7.1116325227957095e-21,
+ 7.3403646804903092e-21, 7.5704189502886418e-21, 7.8017853001379744e-21, 8.0344543481570017e-21,
+ 8.2684173217333118e-21, 8.5036660203915022e-21, 8.7401927820109521e-21, 8.9779904520281901e-21,
+ 9.2170523553061439e-21, 9.4573722703928820e-21, 9.6989444059269430e-21, 9.9417633789758424e-21,
+ 1.0185824195119818e-20, 1.0431122230114770e-20, 1.0677653212987396e-20, 1.0925413210432004e-20,
+ 1.1174398612392891e-20, 1.1424606118728715e-20, 1.1676032726866302e-20, 1.1928675720361027e-20,
+ 1.2182532658289373e-20, 1.2437601365406785e-20, 1.2693879923010674e-20, 1.2951366660454145e-20,
+ 1.3210060147261461e-20, 1.3469959185800733e-20, 1.3731062804473644e-20, 1.3993370251385596e-20,
+ 1.4256880988463136e-20, 1.4521594685988369e-20, 1.4787511217522902e-20, 1.5054630655196170e-20,
+ 1.5322953265335218e-20, 1.5592479504415048e-20, 1.5863210015310328e-20, 1.6135145623830982e-20,
+ 1.6408287335525592e-20, 1.6682636332737932e-20, 1.6958193971903124e-20, 1.7234961781071113e-20,
+ 1.7512941457646084e-20, 1.7792134866331487e-20, 1.8072544037271070e-20, 1.8354171164377277e-20,
+ 1.8637018603838945e-20, 1.8921088872801004e-20, 1.9206384648209468e-20, 1.9492908765815636e-20,
+ 1.9780664219333857e-20, 2.0069654159747839e-20, 2.0359881894760859e-20, 2.0651350888385696e-20,
+ 2.0944064760670539e-20, 2.1238027287557466e-20, 2.1533242400870487e-20, 2.1829714188430474e-20,
+ 2.2127446894294597e-20, 2.2426444919118270e-20, 2.2726712820637798e-20, 2.3028255314272276e-20,
+ 2.3331077273843558e-20, 2.3635183732413286e-20, 2.3940579883236352e-20, 2.4247271080830277e-20,
+ 2.4555262842160330e-20, 2.4864560847940368e-20, 2.5175170944049622e-20, 2.5487099143065929e-20,
+ 2.5800351625915997e-20, 2.6114934743643687e-20, 2.6430855019297323e-20, 2.6748119149937411e-20,
+ 2.7066734008766247e-20, 2.7386706647381193e-20, 2.7708044298153558e-20, 2.8030754376735269e-20,
+ 2.8354844484695747e-20, 2.8680322412291631e-20, 2.9007196141372126e-20, 2.9335473848423219e-20,
+ 2.9665163907753988e-20, 2.9996274894828624e-20, 3.0328815589748056e-20, 3.0662794980885287e-20,
+ 3.0998222268678760e-20, 3.1335106869588609e-20, 3.1673458420220558e-20, 3.2013286781622988e-20,
+ 3.2354602043762612e-20, 3.2697414530184806e-20, 3.3041734802864950e-20, 3.3387573667257349e-20,
+ 3.3734942177548938e-20, 3.4083851642125208e-20, 3.4434313629256243e-20, 3.4786339973011376e-20,
+ 3.5139942779411164e-20, 3.5495134432826171e-20, 3.5851927602632460e-20, 3.6210335250134172e-20,
+ 3.6570370635764384e-20, 3.6932047326575882e-20, 3.7295379204034252e-20, 3.7660380472126401e-20,
+ 3.8027065665798284e-20, 3.8395449659736649e-20, 3.8765547677510167e-20, 3.9137375301086406e-20,
+ 3.9510948480742172e-20, 3.9886283545385430e-20, 4.0263397213308566e-20, 4.0642306603393541e-20,
+ 4.1023029246790967e-20, 4.1405583099096438e-20, 4.1789986553048817e-20, 4.2176258451776819e-20,
+ 4.2564418102621759e-20, 4.2954485291566197e-20, 4.3346480298300118e-20, 4.3740423911958146e-20,
+ 4.4136337447563716e-20, 4.4534242763218286e-20, 4.4934162278076256e-20, 4.5336118991149025e-20,
+ 4.5740136500984466e-20, 4.6146239026271279e-20, 4.6554451427421133e-20, 4.6964799229185088e-20,
+ 4.7377308644364938e-20, 4.7792006598684169e-20, 4.8208920756888113e-20, 4.8628079550147814e-20,
+ 4.9049512204847653e-20, 4.9473248772842596e-20, 4.9899320163277674e-20, 5.0327758176068971e-20,
+ 5.0758595537153414e-20, 5.1191865935622696e-20, 5.1627604062866059e-20, 5.2065845653856416e-20,
+ 5.2506627530725194e-20, 5.2949987648783448e-20, 5.3395965145159426e-20, 5.3844600390237576e-20,
+ 5.4295935042099358e-20, 5.4750012104183868e-20, 5.5206875986405073e-20, 5.5666572569983821e-20,
+ 5.6129149276275792e-20, 5.6594655139902476e-20, 5.7063140886520563e-20, 5.7534659015596918e-20,
+ 5.8009263888591218e-20, 5.8487011822987583e-20, 5.8967961192659803e-20, 5.9452172535103471e-20,
+ 5.9939708666122605e-20, 6.0430634802618929e-20, 6.0925018694200531e-20, 6.1422930764402860e-20,
+ 6.1924444262401531e-20, 6.2429635426193939e-20, 6.2938583658336214e-20, 6.3451371715447563e-20,
+ 6.3968085912834963e-20, 6.4488816345752736e-20, 6.5013657128995346e-20, 6.5542706656731714e-20,
+ 6.6076067884730717e-20, 6.6613848637404196e-20, 6.7156161942412980e-20, 6.7703126395950580e-20,
+ 6.8254866562246408e-20, 6.8811513411327825e-20, 6.9373204799659681e-20, 6.9940085998959109e-20,
+ 7.0512310279279503e-20, 7.1090039553397167e-20, 7.1673445090644796e-20, 7.2262708309655784e-20,
+ 7.2858021661057338e-20, 7.3459589613035800e-20, 7.4067629754967553e-20, 7.4682374037052817e-20,
+ 7.5304070167226666e-20, 7.5932983190698547e-20, 7.6569397282483754e-20, 7.7213617789487678e-20,
+ 7.7865973566417016e-20, 7.8526819659456755e-20, 7.9196540403850560e-20, 7.9875553017037968e-20,
+ 8.0564311788901630e-20, 8.1263312996426176e-20, 8.1973100703706304e-20, 8.2694273652634034e-20,
+ 8.3427493508836792e-20, 8.4173494807453416e-20, 8.4933097052832066e-20, 8.5707219578230905e-20,
+ 8.6496899985930695e-20, 8.7303317295655327e-20, 8.8127821378859504e-20, 8.8971970928196666e-20,
+ 8.9837583239314064e-20, 9.0726800697869543e-20, 9.1642181484063544e-20, 9.2586826406702765e-20,
+ 9.3564561480278864e-20, 9.4580210012636175e-20, 9.5640015550850358e-20, 9.6752334770503130e-20,
+ 9.7928851697808831e-20, 9.9186905857531331e-20, 1.0055456271343397e-19, 1.0208407377305566e-19,
+ 1.0390360993240711e-19, 1.0842021724855044e-19 };
+
+ // alias_threshold[j] is a threshold for the probability mass function that has been
+ // scaled by (2**64 - 1), translated by -(2**63), and represented as a long value;
+ // in this way it can be directly compared to a randomly chosen long value.
+ static final long[] normalAliasThreshold = { // 256 entries
+ 9223372036854775732L, 1100243796470199922L, 7866600928967318259L, 6788754710669718688L,
+ 9022865200207136940L, 6522434035182564354L, 4723064097388367094L, 3360495653202227820L,
+ 2289663232347306830L, 1423968905585875379L, 708364817795238883L, 106102487338962592L,
+ -408333464668584328L, -853239722790494085L, -1242095211827090004L, -1585059631108655444L,
+ -1889943050267333598L, -2162852901996526266L, -2408637386596951353L, -2631196530256993348L,
+ -2833704942542501760L, -3018774289008775598L, -3188573753501888049L, -3344920681670389334L,
+ -3489349705095933019L, -3623166100045386711L, -3747487436861293578L, -3863276422709141026L,
+ -3971367044055496571L, -4072485557008423504L, -4167267476835653997L, -4256271432259158584L,
+ -4339990541931699221L, -4418861817116128356L, -4493273980399812066L, -4563574004455583972L,
+ -4630072609765608272L, -4693048910437239656L, -4752754358851355990L, -4809416110064308151L,
+ -4863239903553549801L, -4914412541525462120L, -4963104028438393907L, -5009469424783376781L,
+ -5053650458852410933L, -5095776932714599237L, -5135967952538787362L, -5174333008440005397L,
+ -5210972924976812191L, -5245980700089102084L, -5279442247516610920L, -5311437055455710870L,
+ -5342038772315685218L, -5371315728848281940L, -5399331404596850615L, -5426144845492958401L,
+ -5451811038482575296L, -5476381248268660540L, -5499903320574200237L, -5522421955754019296L,
+ -5543978956088644891L, -5564613449670076120L, -5584362093426489951L, -5603259257517942559L,
+ -5621337193067953247L, -5638626184957155131L, -5655154691206501482L, -5670949470299055313L,
+ -5686035697633988263L, -5700437072176015065L, -5714175914241450413L, -5727273255262198220L,
+ -5739748920276454057L, -5751621603817308582L, -5762908939796390234L, -5773627565922293024L,
+ -5783793183134813122L, -5793420610488485693L, -5802523835876777512L, -5811116062947540603L,
+ -5819209754528321254L, -5826816672847738703L, -5833947916812588598L, -5840613956576464230L,
+ -5846824665611918318L, -5852589350480860931L, -5857916778478181241L, -5862815203308620040L,
+ -5867292388942958035L, -5871355631785040459L, -5875011781271709877L, -5878267259014830525L,
+ -5881128076587168793L, -5883599852042383670L, -5885687825255517495L, -5887396872158140520L,
+ -5888731517940791413L, -5889695949285098191L, -5890294025685452079L, -5890529289913339019L,
+ -5890404977673728891L, -5889924026498433105L, -5889089083917111413L, -5887902514943630556L,
+ -5886366408911444323L, -5884482585689698188L, -5882252601307215732L, -5879677753010810505L,
+ -5876759083779777633L, -5873497386319005871L, -5869893206546653493L, -5865946846595933526L,
+ -5861658367342436656L, -5857027590471882377L, -5852054100098427498L, -5846737243942430862L,
+ -5841076134076202917L, -5835069647242632620L, -5828716424752710909L, -5822014871963881822L,
+ -5814963157341321336L, -5807559211102860368L, -5799800723445392235L, -5791685142351319976L,
+ -5783209670970726741L, -5774371264573181466L, -5765166627063894671L, -5755592207054728713L,
+ -5745644193480823967L, -5735318510752045177L, -5724610813425415465L, -5713516480385581414L,
+ -5702030608515423737L, -5690148005840583288L, -5677863184127162093L, -5665170350911168791L,
+ -5652063400935782694L, -5638535906971010691L, -5624581109986711207L, -5610191908648783765L,
+ -5595360848105231304L, -5580080108024969737L, -5564341489852042876L, -5548136403231016978L,
+ -5531455851558564459L, -5514290416611714856L, -5496630242199355791L, -5478465016777918644L,
+ -5459783954970839371L, -5440575777921757436L, -5420828692410297267L, -5400530368650229789L,
+ -5379667916685479525L, -5358227861290596404L, -5336196115276119372L, -5313557951090901350L,
+ -5290297970603367798L, -5266400072934326313L, -5241847420204395031L, -5216622401044877639L,
+ -5190706591710560934L, -5164080714616987256L, -5136724594109421094L, -5108617109256031912L,
+ -5079736143434386281L, -5050058530465123570L, -5019559997019987907L, -4988215101007960589L,
+ -4955997165616088151L, -4922878208649305943L, -4888828866781574127L, -4853818314291958392L,
+ -4817814175818125756L, -4780782432613346925L, -4742687321741700014L, -4703491227589533028L,
+ -4663154565006030194L, -4621635653315226847L, -4578890580363657638L, -4534873055674290590L,
+ -4489534251682380820L, -4442822631912146606L, -4394683764829968681L, -4345060121963632469L,
+ -4293890858720706245L, -4241111576152819891L, -4186654061709945180L, -4130446006793453666L,
+ -4072410698652140640L, -4012466683862855933L, -3950527400292573339L, -3886500774045756804L,
+ -3820288777448438119L, -3751786943603804843L, -3680883832458819395L, -3607460442634330728L,
+ -3531389562479403081L, -3452535052892669800L, -3370751053387208615L, -3285881101636362572L,
+ -3197757155290696249L, -3106198503163967069L, -3011010550898974052L, -2911983463889090176L,
+ -2808890647471134035L, -2701487041141521265L, -2589507199668960785L, -2472663129352313038L,
+ -2350641842148622058L, -2223102583752258356L, -2089673683718520949L, -1949948966041670625L,
+ -1803483646850545328L, -1649789631543398131L, -1488330106106063370L, -1318513295716695859L,
+ -1139685236949889721L, -951121376566993538L, -752016768187462359L, -541474585679321485L,
+ -318492605702529265L, -81947227237782935L, 169425512586600501L, 437052607251310002L,
+ 722551297576808029L, 1027761939321803391L, 1354787941562529921L, 1706044619231670700L,
+ 2084319374410687061L, 2492846399585974279L, 2935400169364870493L, 3416413484632185639L,
+ 3941127949845221101L, 4515787798750242711L, 5147892401460631081L, 5846529325404347588L,
+ 6622819682189677227L, 7490522659877439279L, 8466869998300400224L, 8216968526327386835L,
+ 4550693915429835301L, 7628019504075715697L, 6605080500885794707L, 7121156327618549405L,
+ 2484871780310660533L, 7179104797025802172L, 7066086283790288107L, 1516500120772178463L,
+ 216305945406470492L, 6295963418490399062L, 2889316805640753770L, -2712587580563247199L,
+ 6562498853480442900L, 7975754821117214681L, -9223372036854775807L, -9223372036854775807L };
+
+ static final byte[] normalAliasMap = { // 256 entries
+ (byte) 0, (byte) 0, (byte)239, (byte) 2, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
+ (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 1, (byte) 1, (byte) 1, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253, (byte)253,
+ (byte)253, (byte)253, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252, (byte)252,
+ (byte)252, (byte)252, (byte)252, (byte)252, (byte)251, (byte)251, (byte)251, (byte)251,
+ (byte)251, (byte)251, (byte)251, (byte)250, (byte)250, (byte)250, (byte)250, (byte)250,
+ (byte)249, (byte)249, (byte)249, (byte)248, (byte)248, (byte)248, (byte)247, (byte)247,
+ (byte)247, (byte)246, (byte)246, (byte)245, (byte)244, (byte)244, (byte)243, (byte)242,
+ (byte)240, (byte) 2, (byte) 2, (byte) 3, (byte) 3, (byte) 0, (byte) 0, (byte)240,
+ (byte)241, (byte)242, (byte)243, (byte)244, (byte)245, (byte)246, (byte)247, (byte)248,
+ (byte)249, (byte)250, (byte)251, (byte)252, (byte)253, (byte) 1, (byte) 0, (byte) 0 };
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/JumpableRng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.stream.Stream;
+
+/**
+ * This interface is designed to provide a common protocol for objects
+ * that generate pseudorandom sequences of numbers (or Boolean values)
+ * and furthermore can easily <it>jump</it> forward (by a fixed amount)
+ * to a distant point in the state cycle.
+ *
+ * <p>Ideally, all {@code JumpableRng} objects produced by iterative
+ * jumping from a single original {@code JumpableRng} object are
+ * statistically independent of one another and individually uniform.
+ * In practice, one must settle for some approximation to independence
+ * and uniformity. In particular, a specific implementation may
+ * assume that each generator in a stream produced by the {@code jumps}
+ * method is used to produce a number of values no larger than either
+ * 2<sup>64</sup> or the square root of its period. Implementors are
+ * advised to use algorithms whose period is at least 2<sup>127</sup>.
+ *
+ * <p>Methods are provided to perform a single jump operation and also
+ * to produce a stream of generators produced from the original by
+ * iterative copying and jumping of internal state. A typical
+ * strategy for a multithreaded application is to create a single
+ * {@code JumpableRng} object, calls its {@code jumps} method exactly
+ * once, and then parcel out generators from the resulting stream, one
+ * to each thread. It is generally not a good idea to call {@code jump}
+ * on a generator that was itself produced by the {@code jumps} method,
+ * because the result may be a generator identical to another
+ * generator already produce by that call to the {@code jumps} method.
+ * For this reason, the return type of the {@code jumps} method is
+ * {@code Stream<Rng>} rather than {@code Stream<JumpableRng>}, even
+ * though the actual generator objects in that stream likely do also
+ * implement the {@code JumpableRng} interface.
+ *
+ * <p>An implementation of the {@code JumpableRng} interface must provide
+ * concrete definitions for the methods {@code nextInt()}, {@code nextLong},
+ * {@code period()}, {@code copy()}, {@code jump()}, and {@code defaultJumpDistance()}.
+ * Default implementations are provided for all other methods.
+ *
+ * <p>Objects that implement {@code java.util.JumpableRng} are
+ * typically not cryptographically secure. Consider instead using
+ * {@link java.security.SecureRandom} to get a cryptographically
+ * secure pseudo-random number generator for use by
+ * security-sensitive applications.
+ *
+ * @author Guy Steele
+ * @since 1.9
+ */
+interface JumpableRng extends StreamableRng {
+ /**
+ * Returns a new generator whose internal state is an exact copy
+ * of this generator (therefore their future behavior should be
+ * identical if subjected to the same series of operations).
+ *
+ * @return a new object that is a copy of this generator
+ */
+ JumpableRng copy();
+
+ /**
+ * Alter the state of this pseudorandom number generator so as to
+ * jump forward a large, fixed distance (typically 2<sup>64</sup>
+ * or more) within its state cycle.
+ */
+ void jump();
+
+ /**
+ * Returns the distance by which the {@code jump()} method will jump
+ * forward within the state cycle of this generator object.
+ *
+ * @return the default jump distance (as a {@code double} value)
+ */
+ double defaultJumpDistance();
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code Rng}
+ * interface.
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to {@code jumps(Long.MAX_VALUE)}.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code copy()} and {@code jump()} on this generator,
+ * and the copies become the generators produced by the stream.
+ *
+ * @return a stream of objects that implement the {@code Rng} interface
+ */
+ default Stream<Rng> jumps() {
+ return Stream.generate(this::copyAndJump).sequential();
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code Rng} interface.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code copy()} and {@code jump()} on this generator,
+ * and the copies become the generators produced by the stream.
+ *
+ * @param streamSize the number of generators to generate
+ * @return a stream of objects that implement the {@code Rng} interface
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ default Stream<Rng> jumps(long streamSize) {
+ return jumps().limit(streamSize);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code Rng}
+ * interface. Ideally the generators in the stream will appear
+ * to be statistically independent.
+ *
+ * @implNote The default implementation calls {@code jumps()}.
+ *
+ * @return a stream of objects that implement the {@code Rng} interface
+ */
+ default Stream<Rng> rngs() {
+ return this.jumps();
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code Rng} interface. Ideally the generators in the stream will
+ * appear to be statistically independent.
+ *
+ * @implNote The default implementation calls {@code jumps(streamSize)}.
+ *
+ * @param streamSize the number of generators to generate
+ * @return a stream of objects that implement the {@code Rng} interface
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ default Stream<Rng> rngs(long streamSize) {
+ return this.jumps(streamSize);
+ }
+
+ /**
+ * Copy this generator, jump this generator forward, then return the copy.
+ */
+ default Rng copyAndJump() {
+ Rng result = copy();
+ jump();
+ return result;
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/L128X256MixRandom.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,359 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code L128X256MixRandom} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new split-off {@code L128X256MixRandom} objects,
+ * with similar usages as for class {@link java.util.SplittableRandom}.
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ * (Most recently validated with
+ * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
+ * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
+ * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
+ * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
+ * 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.
+ *
+ * <p>{@code L128X256MixRandom} is a specific member of the LXM family of algorithms
+ * for pseudorandom number generators. Every LXM generator consists of two
+ * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
+ * an Xorshift generator. Each output of an LXM generator is the sum of one
+ * output from each subgenerator, possibly processed by a final mixing function
+ * (and {@code L128X256MixRandom} does use a mixing function).
+ *
+ * <p>The LCG subgenerator for {@code L128X256MixRandom} has an update step of the
+ * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
+ * 128-bit integers; {@code s} is the mutable state, the multiplier {@code m}
+ * is fixed (the same for all instances of {@code L128X256MixRandom}}) and the addend
+ * {@code a} is a parameter (a final field of the instance). The parameter
+ * {@code a} is required to be odd (this allows the LCG to have the maximal
+ * period, namely 2<sup>128</sup>); therefore there are 2<sup>127</sup> distinct choices
+ * of parameter.
+ *
+ * <p>The Xorshift subgenerator for {@code L128X256MixRandom} is the {@code xoshiro256} algorithm,
+ * version 1.0 (parameters 17, 45), without any final scrambler such as "+" or "**".
+ * Its state consists of four {@code long} fields {@code x0}, {@code x1}, {@code x2},
+ * and {@code x3}, which can take on any values provided that they are not all zero.
+ * The period of this subgenerator is 2<sup>256</sup>-1.
+ *
+ * <p> The mixing function for {@code L128X256MixRandom} is the 64-bit MurmurHash3 finalizer.
+ *
+ * <p> Because the periods 2<sup>128</sup> and 2<sup>256</sup>-1 of the two subgenerators
+ * are relatively prime, the <em>period</em> of any single {@code L128X256MixRandom} object
+ * (the length of the series of generated 64-bit values before it repeats) is the product
+ * of the periods of the subgenerators, that is, 2<sup>128</sup>(2<sup>256</sup>-1),
+ * which is just slightly smaller than 2<sup>384</sup>. Moreover, if two distinct
+ * {@code L128X256MixRandom} objects have different {@code a} parameters, then their
+ * cycles of produced values will be different.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
+ * For any specific instance of {@code L128X256MixRandom}, over the course of its cycle each
+ * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>256</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise exactly equidistributed.
+ *
+ * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are exactly
+ * 2-equidistributed. For any specific instance of {@code L128X256MixRandom}, consider
+ * the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by
+ * {@code nextLong()} (assuming no other methods are called that would affect the state).
+ * There are 2<sup>128</sup>(2<sup>256</sup>-1) such subsequences, and each subsequence,
+ * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values, and each
+ * such value occurs 2<sup>256</sup>-1 times. The values produced by the {@code nextInt()},
+ * {@code nextFloat()}, and {@code nextDouble()} methods are likewise exactly 2-equidistributed.
+ *
+ * <p>Moreover, the 64-bit values produced by the {@code nextLong()} method are 4-equidistributed.
+ * To be precise: for any specific instance of {@code L128X256MixRandom}, consider
+ * the (overlapping) length-4 subsequences of the cycle of 64-bit values produced by
+ * {@code nextLong()} (assuming no other methods are called that would affect the state).
+ * There are <sup>128</sup>(2<sup>256</sup>-1) such subsequences, and each subsequence,
+ * which consists of 4 64-bit values, can have one of 2<sup>256</sup> values. Of those
+ * 2<sup>256</sup> subsequence values, nearly all of them (2<sup>256</sup>-2<sup>128</sup>)
+ * occur 2<sup>128</sup> times over the course of the entire cycle, and the other
+ * 2<sup>128</sup> subsequence values occur only 2<sup>128</sup>-1 times. So the ratio
+ * of the probability of getting one of the less common subsequence values and the
+ * probability of getting one of the more common subsequence values is 1-2<sup>-128</sup>.
+ * (Note that the set of 2<sup>128</sup> less-common subsequence values will differ from
+ * one instance of {@code L128X256MixRandom} to another, as a function of the additive
+ * parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()},
+ * and {@code nextDouble()} methods are likewise 4-equidistributed.
+ *
+ * <p>Method {@link #split} constructs and returns a new {@code L128X256MixRandom}
+ * 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 L128X256MixRandom} object.
+ * This is because, with high probability, distinct {@code L128X256MixRandom} objects
+ * have distinct {@code a} parameters and therefore use distinct members of the
+ * algorithmic family; and even if their {@code a} parameters are the same, with
+ * very high probability they will traverse different parts of their common state
+ * cycle.
+ *
+ * <p>As with {@link java.util.SplittableRandom}, instances of
+ * {@code L128X256MixRandom} are <em>not</em> thread-safe.
+ * They are designed to be split, not shared, across threads. For
+ * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
+ * computation using random numbers might include a construction
+ * of the form {@code new Subtask(someL128X256MixRandom.split()).fork()}.
+ *
+ * <p>This class provides additional methods for generating random
+ * streams, that employ the above techniques when used in
+ * {@code stream.parallel()} mode.
+ *
+ * <p>Instances of {@code L128X256MixRandom} 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
+ * @since 1.9
+ */
+public final class L128X256MixRandom extends AbstractSplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * The 128-bit parameter `a` is represented as two long fields `ah` and `al`.
+ * The 128-bit state variable `s` is represented as two long fields `sh` and `sl`.
+ *
+ * The split operation uses the current generator to choose eight
+ * new 64-bit long values that are then used to initialize the
+ * parameters `ah` and `al` and the state variables `sh`, `sl`,
+ * `x0`, `x1`, `x2`, and `x3` for a newly constructed generator.
+ *
+ * With extremely high probability, no two generators so chosen
+ * will have the same `a` parameter, and testing has indicated
+ * that the values generated by two instances of {@code L128X256MixRandom}
+ * will be (approximately) independent if have different values for `a`.
+ *
+ * The default (no-argument) constructor, in essence, uses
+ * "defaultGen" to generate eight new 64-bit values for the same
+ * purpose. Multiple generators created in this way will certainly
+ * differ in their `a` parameters. The defaultGen state must be accessed
+ * in a thread-safe manner, so we use an AtomicLong to represent
+ * this state. 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.
+ *
+ * File organization: First static fields, then instance
+ * fields, then constructors, then instance methods.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is (2**256 - 1) * 2**128.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE).shiftLeft(128);
+
+ /*
+ * The multiplier used in the LCG portion of the algorithm is 2**64 + m;
+ * where m is taken from
+ * Pierre L'Ecuyer, Tables of linear congruential generators of
+ * different sizes and good lattice structure, <em>Mathematics of
+ * Computation</em> 68, 225 (January 1999), pages 249–260,
+ * Table 4 (first multiplier for size 2<sup>64</sup>).
+ *
+ * This is almost certainly not the best possible 128-bit multiplier
+ * for an LCG, but it is sufficient for our purposes here; because
+ * is is larger than 2**64, the 64-bit values produced by nextLong()
+ * are exactly 2-equidistributed, and the fact that it is of the
+ * form (2**64 + m) simplifies the code, given that we have only
+ * 64-bit arithmetic to work with.
+ */
+
+ private static final long m = 2862933555777941757L;
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The parameter that is used as an additive constant for the LCG.
+ * Must be odd.
+ */
+ private final long ah, al;
+
+ /**
+ * The per-instance state: sh and sl for the LCG; x0, x1, x2, and x3 for the xorshift.
+ * At least one of the four fields x0, x1, x2, and x3 must be nonzero.
+ */
+ private long sh, sl, x0, x1, x2, x3;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public L128X256MixRandom(long ah, long al, long sh, long sl, long x0, long x1, long x2, long x3) {
+ // Force a to be odd.
+ this.ah = ah;
+ this.al = al | 1;
+ this.sh = sh;
+ this.sl = sl;
+ this.x0 = x0;
+ this.x1 = x1;
+ this.x2 = x2;
+ this.x3 = x3;
+ // If x0, x1, x2, and x3 are all zero, we must choose nonzero values.
+ if ((x0 | x1 | x2 | x3) == 0) {
+ // At least three of the four values generated here will be nonzero.
+ this.x0 = RngSupport.mixStafford13(sh += RngSupport.GOLDEN_RATIO_64);
+ this.x1 = RngSupport.mixStafford13(sh += RngSupport.GOLDEN_RATIO_64);
+ this.x2 = RngSupport.mixStafford13(sh += RngSupport.GOLDEN_RATIO_64);
+ this.x3 = RngSupport.mixStafford13(sh + RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code L128X256MixRandom} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code L128X256MixRandom} created with the same seed in the same
+ * program generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L128X256MixRandom(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The seed is hashed by mixMurmur64 to produce the `a` parameter.
+ // The seed is hashed by mixStafford13 to produce the initial `x0`,
+ // which will then be used to produce the first generated value.
+ // The other x values are filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixMurmur64(seed ^= RngSupport.SILVER_RATIO_64),
+ RngSupport.mixMurmur64(seed += RngSupport.GOLDEN_RATIO_64),
+ 0,
+ 1,
+ RngSupport.mixStafford13(seed),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L128X256MixRandom} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public L128X256MixRandom() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L128X256MixRandom} using the specified array of
+ * initial seed bytes. Instances of {@code L128X256MixRandom} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L128X256MixRandom(byte[] seed) {
+ // Convert the seed to 6 long values, of which the last 4 are not all zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 6, 4);
+ long ah = data[0], al = data[1], sh = data[2], sl = data[3], x0 = data[4], x1 = data[5], x2 = data[6], x3 = data[7];
+ // Force a to be odd.
+ this.ah = ah;
+ this.al = al | 1;
+ this.sh = sh;
+ this.sl = sl;
+ this.x0 = x0;
+ this.x1 = x1;
+ this.x2 = x2;
+ this.x3 = x3;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Constructs and returns a new instance of {@code L128X256MixRandom}
+ * that shares no mutable state with this instance.
+ * However, with very high probability, the set of values collectively
+ * generated by the two objects has the same statistical properties as if
+ * same the quantity of values were generated by a single thread using
+ * a single {@code L128X256MixRandom} object. Either or both of the two
+ * objects may be further split using the {@code split} method,
+ * and the same expected statistical properties apply to the
+ * entire set of generators constructed by such recursive splitting.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a new instance of {@code L128X256MixRandom}
+ */
+ public L128X256MixRandom split(SplittableRng source) {
+ // Literally pick a new instance "at random".
+ return new L128X256MixRandom(source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong());
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ final long z = sh + x0;
+ // The LCG: in effect, s = ((1LL << 64) + m) * s + a, if only we had 128-bit arithmetic.
+ final long u = m * sl;
+ sh = (m * sh) + Math.multiplyHigh(m, sl) + sl + ah;
+ sl = u + al;
+ if (Long.compareUnsigned(sl, u) < 0) ++sh; // Handle the carry propagation from low half to high half.
+ long q0 = x0, q1 = x1, q2 = x2, q3 = x3;
+ { long t = q1 << 17; q2 ^= q0; q3 ^= q1; q1 ^= q2; q0 ^= q3; q2 ^= t; q3 = Long.rotateLeft(q3, 45); } // xoshiro256 1.0
+ x0 = q0; x1 = q1; x2 = q2; x3 = q3;
+ return RngSupport.mixLea64(z); // mixing function
+ }
+
+ public BigInteger period() { return thePeriod; }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/L32X64MixRandom.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,325 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code L32X64MixRandom} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new split-off {@code L32X64MixRandom} objects,
+ * with similar usages as for class {@link java.util.SplittableRandom}.
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ * (Most recently validated with
+ * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
+ * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
+ * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
+ * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
+ * 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.
+ *
+ * <p>{@code L32X64MixRandom} is a specific member of the LXM family of algorithms
+ * for pseudorandom number generators. Every LXM generator consists of two
+ * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
+ * an Xorshift generator. Each output of an LXM generator is the sum of one
+ * output from each subgenerator, possibly processed by a final mixing function
+ * (and {@code L32X64MixRandom} does use a mixing function).
+ *
+ * <p>The LCG subgenerator for {@code L32X64MixRandom} has an update step of the
+ * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
+ * of type {@code int}; {@code s} is the mutable state, the multiplier {@code m}
+ * is fixed (the same for all instances of {@code L32X64MixRandom}}) and the addend
+ * {@code a} is a parameter (a final field of the instance). The parameter
+ * {@code a} is required to be odd (this allows the LCG to have the maximal
+ * period, namely 2<sup>32</sup>); therefore there are 2<sup>31</sup> distinct choices
+ * of parameter.
+ *
+ * <p>The Xorshift subgenerator for {@code L32X64MixRandom} is the {@code xoroshiro64} algorithm,
+ * version 1.0 (parameters 26, 9, 13), without any final scrambler such as "+" or "**".
+ * Its state consists of two {@code int} fields {@code x0} and {@code x1},
+ * which can take on any values provided that they are not both zero.
+ * The period of this subgenerator is 2<sup>64</sup>-1.
+ *
+ * <p> The mixing function for {@code L32X64MixRandom} is the "starstar" mixing function.
+ *
+ * <p> Because the periods 2<sup>32</sup> and 2<sup>64</sup>-1 of the two subgenerators
+ * are relatively prime, the <em>period</em> of any single {@code L32X64MixRandom} object
+ * (the length of the series of generated 32-bit values before it repeats) is the product
+ * of the periods of the subgenerators, that is, 2<sup>32</sup>(2<sup>64</sup>-1),
+ * which is just slightly smaller than 2<sup>96</sup>. Moreover, if two distinct
+ * {@code L32X64MixRandom} objects have different {@code a} parameters, then their
+ * cycles of produced values will be different.
+ *
+ * <p>The 32-bit values produced by the {@code nextInt()} method are exactly equidistributed.
+ * For any specific instance of {@code L32X64MixRandom}, over the course of its cycle each
+ * of the 2<sup>32</sup> possible {@code int} values will be produced 2<sup>64</sup>-1 times.
+ * The values produced by the {@code nextFloat()} method are likewise exactly equidistributed.
+ *
+ * <p>In fact, the 32-bit values produced by the {@code nextInt()} method are 2-equidistributed.
+ * To be precise: for any specific instance of {@code L32X64MixRandom}, consider
+ * the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by
+ * {@code nextInt()} (assuming no other methods are called that would affect the state).
+ * There are 2<sup>32</sup>(2<sup>64</sup>-1) such subsequences, and each subsequence,
+ * which consists of 2 32-bit values, can have one of 2<sup>64</sup> values. Of those
+ * 2<sup>64</sup> subsequence values, nearly all of them (2<sup>64</sup>-2<sup>32</sup>)
+ * occur 2<sup>32</sup> times over the course of the entire cycle, and the other
+ * 2<sup>32</sup> subsequence values occur only 2<sup>32</sup>-1 times. So the ratio
+ * of the probability of getting one of the less common subsequence values and the
+ * probability of getting one of the more common subsequence values is 1-2<sup>-32</sup>.
+ * (Note that the set of 2<sup>32</sup> less-common subsequence values will differ from
+ * one instance of {@code L32X64MixRandom} to another, as a function of the additive
+ * parameter of the LCG.) As a consequence, the values produced by the {@code nextFloat()}
+ * method are likewise 2-equidistributed, and the values produced by the {@code nextLong()}
+ * and {@code nextDouble()} methods are equidistributed (but not 2-equidistributed).
+ *
+ * <p>Method {@link #split} constructs and returns a new {@code L32X64MixRandom}
+ * 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 L32X64MixRandom} object.
+ * This is because, with high probability, distinct {@code L32X64MixRandom} objects
+ * have distinct {@code a} parameters and therefore use distinct members of the
+ * algorithmic family; and even if their {@code a} parameters are the same, with
+ * very high probability they will traverse different parts of their common state
+ * cycle.
+ *
+ * <p>As with {@link java.util.SplittableRandom}, instances of
+ * {@code L32X64MixRandom} are <em>not</em> thread-safe.
+ * They are designed to be split, not shared, across threads. For
+ * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
+ * computation using random numbers might include a construction
+ * of the form {@code new Subtask(someL32X64MixRandom.split()).fork()}.
+ *
+ * <p>This class provides additional methods for generating random
+ * streams, that employ the above techniques when used in
+ * {@code stream.parallel()} mode.
+ *
+ * <p>Instances of {@code L32X64MixRandom} 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
+ * @since 1.9
+ */
+public final class L32X64MixRandom extends AbstractSplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * The split operation uses the current generator to choose four new 64-bit
+ * int values that are then used to initialize the parameter `a` and the
+ * state variables `s`, `x0`, and `x1` for a newly constructed generator.
+ *
+ * With high probability, no two generators so chosen
+ * will have the same `a` parameter, and testing has indicated
+ * that the values generated by two instances of {@code L32X64MixRandom}
+ * will be (approximately) independent if have different values for `a`.
+ *
+ * The default (no-argument) constructor, in essence, uses
+ * "defaultGen" to generate four new 32-bit values for the same
+ * purpose. Multiple generators created in this way will certainly
+ * differ in their `a` parameters. The defaultGen state must be accessed
+ * in a thread-safe manner, so we use an AtomicLong to represent
+ * this state. 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.
+ *
+ * File organization: First static fields, then instance
+ * fields, then constructors, then instance methods.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is (2**64 - 1) * 2**32.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE).shiftLeft(32);
+
+ /*
+ * Multiplier used in the LCG portion of the algorithm, taken from
+ * Pierre L'Ecuyer, Tables of linear congruential generators of
+ * different sizes and good lattice structure, <em>Mathematics of
+ * Computation</em> 68, 225 (January 1999), pages 249–260,
+ * Table 4 (third multiplier for size 2<sup>32</sup>).
+ */
+
+ private static final int m = 32310901;
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The parameter that is used as an additive constant for the LCG.
+ * Must be odd.
+ */
+ private final int a;
+
+ /**
+ * The per-instance state: s for the LCG; x0 and x1 for the xorshift.
+ * At least one of x0 and x1 must be nonzero.
+ */
+ private int s, x0, x1;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public L32X64MixRandom(int a, int s, int x0, int x1) {
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ // If x0 and x1 are both zero, we must choose nonzero values.
+ if ((x0 | x1) == 0) {
+ // At least one of the two values generated here will be nonzero.
+ this.x0 = RngSupport.mixMurmur32(s += RngSupport.GOLDEN_RATIO_32);
+ this.x1 = RngSupport.mixMurmur32(s + RngSupport.GOLDEN_RATIO_32);
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code L32X64MixRandom} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code L32X64MixRandom} created with the same seed in the same
+ * program generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L32X64MixRandom(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The high half of the seed is hashed by mixMurmur32 to produce the `a` parameter.
+ // The low half of the seed is hashed by mixMurmur32 to produce the initial `x0`,
+ // which will then be used to produce the first generated value.
+ // Then x1 is filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_32 as the gamma value and Murmur32 as the mixer.
+ this(RngSupport.mixMurmur32((int)((seed ^= RngSupport.SILVER_RATIO_64) >>> 32)),
+ 1,
+ RngSupport.mixLea32((int)(seed)),
+ RngSupport.mixLea32((int)(seed) + RngSupport.GOLDEN_RATIO_32));
+ }
+
+ /**
+ * Creates a new instance of {@code L32X64MixRandom} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public L32X64MixRandom() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L32X64MixRandom} using the specified array of
+ * initial seed bytes. Instances of {@code L32X64MixRandom} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L32X64MixRandom(byte[] seed) {
+ // Convert the seed to 4 int values, of which the last 2 are not all zero.
+ int[] data = RngSupport.convertSeedBytesToInts(seed, 4, 2);
+ int a = data[0], s = data[1], x0 = data[2], x1 = data[3];
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x0 = x0;
+ this.x1 = x1;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Constructs and returns a new instance of {@code L32X64MixRandom}
+ * that shares no mutable state with this instance.
+ * However, with very high probability, the set of values collectively
+ * generated by the two objects has the same statistical properties as if
+ * same the quantity of values were generated by a single thread using
+ * a single {@code L32X64MixRandom} object. Either or both of the two
+ * objects may be further split using the {@code split} method,
+ * and the same expected statistical properties apply to the
+ * entire set of generators constructed by such recursive splitting.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a new instance of {@code L32X64MixRandom}
+ */
+ public L32X64MixRandom split(SplittableRng source) {
+ // Literally pick a new instance "at random".
+ return new L32X64MixRandom(source.nextInt(), source.nextInt(),
+ source.nextInt(), source.nextInt());
+ }
+
+ /**
+ * Returns a pseudorandom {@code int} value.
+ *
+ * @return a pseudorandom {@code int} value
+ */
+ public int nextInt() {
+ final int z = s + x0;
+ s = m * s + a; // LCG
+ int q0 = x0, q1 = x1;
+ { q1 ^= q0; q0 = Integer.rotateLeft(q0, 26); q0 = q0 ^ q1 ^ (q1 << 9); q1 = Integer.rotateLeft(q1, 13); } // xoroshiro64
+ x0 = q0; x1 = q1;
+ return Integer.rotateLeft(z * 5, 7) * 9; // "starstar" mixing function
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ return ((long)(nextInt()) << 32) | nextInt();
+ }
+
+ public BigInteger period() { return thePeriod; }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/L64X1024MixRandom.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,378 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code L64X1024MixRandom} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new split-off {@code L64X1024MixRandom} objects,
+ * with similar usages as for class {@link java.util.SplittableRandom}.
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ * (Most recently validated with
+ * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
+ * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
+ * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
+ * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
+ * 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.
+ *
+ * <p>{@code L64X1024MixRandom} is a specific member of the LXM family of algorithms
+ * for pseudorandom number generators. Every LXM generator consists of two
+ * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
+ * an Xorshift generator. Each output of an LXM generator is the sum of one
+ * output from each subgenerator, possibly processed by a final mixing function
+ * (and {@code L64X1024MixRandom} does use a mixing function).
+ *
+ * <p>The LCG subgenerator for {@code L64X1024MixRandom} has an update step of the
+ * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
+ * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m}
+ * is fixed (the same for all instances of {@code L64X1024MixRandom}}) and the addend
+ * {@code a} is a parameter (a final field of the instance). The parameter
+ * {@code a} is required to be odd (this allows the LCG to have the maximal
+ * period, namely 2<sup>64</sup>); therefore there are 2<sup>63</sup> distinct choices
+ * of parameter.
+ *
+ * <p>The Xorshift subgenerator for {@code L64X1024MixRandom} is the {@code xoroshiro1024}
+ * algorithm (parameters 25, 27, and 36), without any final scrambler such as "+" or "**".
+ * Its state consists of an array {@code x} of sixteen {@code long} values,
+ * which can take on any values provided that they are not all zero.
+ * The period of this subgenerator is 2<sup>1024</sup>-1.
+ *
+ * <p> The mixing function for {@code L64X256MixRandom} is the 64-bit MurmurHash3 finalizer.
+ *
+ * <p> Because the periods 2<sup>64</sup> and 2<sup>1024</sup>-1 of the two subgenerators
+ * are relatively prime, the <em>period</em> of any single {@code L64X1024MixRandom} object
+ * (the length of the series of generated 64-bit values before it repeats) is the product
+ * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>1024</sup>-1),
+ * which is just slightly smaller than 2<sup>1088</sup>. Moreover, if two distinct
+ * {@code L64X1024MixRandom} objects have different {@code a} parameters, then their
+ * cycles of produced values will be different.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
+ * For any specific instance of {@code L64X1024MixRandom}, over the course of its cycle each
+ * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>1024</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise exactly equidistributed.
+ *
+ * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 16-equidistributed.
+ * To be precise: for any specific instance of {@code L64X1024MixRandom}, consider
+ * the (overlapping) length-16 subsequences of the cycle of 64-bit values produced by
+ * {@code nextLong()} (assuming no other methods are called that would affect the state).
+ * There are 2<sup>64</sup>(2<sup>1024</sup>-1) such subsequences, and each subsequence,
+ * which consists of 16 64-bit values, can have one of 2<sup>1024</sup> values. Of those
+ * 2<sup>1024</sup> subsequence values, nearly all of them (2<sup>1024</sup>-2<sup>64</sup>)
+ * occur 2<sup>64</sup> times over the course of the entire cycle, and the other
+ * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times. So the ratio
+ * of the probability of getting one of the less common subsequence values and the
+ * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>.
+ * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from
+ * one instance of {@code L64X1024MixRandom} to another, as a function of the additive
+ * parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()},
+ * and {@code nextDouble()} methods are likewise 16-equidistributed.
+ *
+ * <p>Method {@link #split} constructs and returns a new {@code L64X1024MixRandom}
+ * 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 L64X1024MixRandom} object.
+ * This is because, with high probability, distinct {@code L64X1024MixRandom} objects
+ * have distinct {@code a} parameters and therefore use distinct members of the
+ * algorithmic family; and even if their {@code a} parameters are the same, with
+ * very high probability they will traverse different parts of their common state
+ * cycle.
+ *
+ * <p>As with {@link java.util.SplittableRandom}, instances of
+ * {@code L64X1024MixRandom} are <em>not</em> thread-safe.
+ * They are designed to be split, not shared, across threads. For
+ * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
+ * computation using random numbers might include a construction
+ * of the form {@code new Subtask(someL64X1024MixRandom.split()).fork()}.
+ *
+ * <p>This class provides additional methods for generating random
+ * streams, that employ the above techniques when used in
+ * {@code stream.parallel()} mode.
+ *
+ * <p>Instances of {@code L64X1024MixRandom} 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
+ * @since 1.9
+ */
+public final class L64X1024MixRandom extends AbstractSplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * The split() operation uses the current generator to choose 18 new 64-bit
+ * long values that are then used to initialize the parameter `a`, the
+ * state variable `s`, and the array `x` for a newly constructed generator.
+ *
+ * With extremely high probability, no two generators so chosen
+ * will have the same `a` parameter, and testing has indicated
+ * that the values generated by two instances of {@code L64X1024MixRandom}
+ * will be (approximately) independent if have different values for `a`.
+ *
+ * The default (no-argument) constructor, in essence, uses
+ * "defaultGen" to generate 18 new 64-bit values for the same
+ * purpose. Multiple generators created in this way will certainly
+ * differ in their `a` parameters. The defaultGen state must be accessed
+ * in a thread-safe manner, so we use an AtomicLong to represent
+ * this state. 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.
+ *
+ * File organization: First static fields, then instance
+ * fields, then constructors, then instance methods.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /*
+ * The length of the array x.
+ */
+
+ private static final int N = 16;
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is (2**1024 - 1) * 2**64.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(N*64).subtract(BigInteger.ONE).shiftLeft(64);
+
+ /*
+ * Multiplier used in the LCG portion of the algorithm, taken from
+ * Pierre L'Ecuyer, Tables of linear congruential generators of
+ * different sizes and good lattice structure, <em>Mathematics of
+ * Computation</em> 68, 225 (January 1999), pages 249–260,
+ * Table 4 (first multiplier for size 2<sup>64</sup>).
+ */
+
+ private static final long m = 2862933555777941757L;
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The parameter that is used as an additive constant for the LCG.
+ * Must be odd.
+ */
+ private final long a;
+
+ /**
+ * The per-instance state: s for the LCG; the array x for the xorshift;
+ * p is the rotating pointer into the array x.
+ * At least one of the 16 elements of the array x must be nonzero.
+ */
+ private long s;
+ private final long[] x;
+ private int p = N - 1;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public L64X1024MixRandom(long a, long s,
+ long x0, long x1, long x2, long x3,
+ long x4, long x5, long x6, long x7,
+ long x8, long x9, long x10, long x11,
+ long x12, long x13, long x14, long x15) {
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x = new long[N];
+ this.x[0] = x0;
+ this.x[1] = x1;
+ this.x[2] = x2;
+ this.x[3] = x3;
+ this.x[4] = x4;
+ this.x[5] = x5;
+ this.x[6] = x6;
+ this.x[7] = x7;
+ this.x[8] = x8;
+ this.x[9] = x9;
+ this.x[10] = x10;
+ this.x[11] = x11;
+ this.x[12] = x12;
+ this.x[13] = x13;
+ this.x[14] = x14;
+ this.x[15] = x15;
+ // If x0, x1, ..., x15 are all zero (very unlikely), we must choose nonzero values.
+ if ((x0 | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 | x11 | x12 | x13 | x14 | x15) == 0) {
+ // At least fifteen of the sixteen values generated here will be nonzero.
+ for (int j = 0; j < N; j++) {
+ this.x[j] = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code L64X1024MixRandom} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code L64X1024MixRandom} created with the same seed in the same
+ * program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X1024MixRandom(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The seed is hashed by mixMurmur64 to produce the `a` parameter.
+ // The seed is hashed by mixStafford13 to produce the initial `x[0]`,
+ // which will then be used to produce the first generated value.
+ // The other x values are filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixMurmur64(seed ^= RngSupport.SILVER_RATIO_64),
+ 1,
+ RngSupport.mixStafford13(seed),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X1024MixRandom} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public L64X1024MixRandom() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X1024MixRandom} using the specified array of
+ * initial seed bytes. Instances of {@code L64X1024MixRandom} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X1024MixRandom(byte[] seed) {
+ // Convert the seed to 18 long values, of which the last 16 are not all zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 18, 16);
+ long a = data[0], s = data[1];
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x = new long[N];
+ for (int j = 0; j < N; j++) {
+ this.x[j] = data[2+j];
+ }
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Constructs and returns a new instance of {@code L64X1024MixRandom}
+ * that shares no mutable state with this instance.
+ * However, with very high probability, the set of values collectively
+ * generated by the two objects has the same statistical properties as if
+ * same the quantity of values were generated by a single thread using
+ * a single {@code L64X1024MixRandom} object. Either or both of the two
+ * objects may be further split using the {@code split} method,
+ * and the same expected statistical properties apply to the
+ * entire set of generators constructed by such recursive splitting.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a new instance of {@code L64X1024MixRandom}
+ */
+ public L64X1024MixRandom split(SplittableRng source) {
+ // Literally pick a new instance "at random".
+ return new L64X1024MixRandom(source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong());
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ // First part of xoroshiro1024: fetch array data
+ final int q = p;
+ final long s0 = x[p = (p + 1) & (N - 1)];
+ long s15 = x[q];
+
+ final long z = s + s0;
+ s = m * s + a; // LCG
+
+ // Second part of xoroshiro1024: update array data
+ s15 ^= s0;
+ x[q] = Long.rotateLeft(s0, 25) ^ s15 ^ (s15 << 27);
+ x[p] = Long.rotateLeft(s15, 36);
+
+ return RngSupport.mixLea64(z); // mixing function
+ }
+
+ public BigInteger period() { return thePeriod; }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/L64X1024Random.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,375 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code L64X1024Random} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new split-off {@code L64X1024Random} objects,
+ * with similar usages as for class {@link java.util.SplittableRandom}.
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ * (Most recently validated with
+ * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
+ * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
+ * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
+ * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
+ * 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.
+ *
+ * <p>{@code L64X1024Random} is a specific member of the LXM family of algorithms
+ * for pseudorandom number generators. Every LXM generator consists of two
+ * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
+ * an Xorshift generator. Each output of an LXM generator is the sum of one
+ * output from each subgenerator, possibly processed by a final mixing function
+ * (but {@code L64X1024Random} does not use a mixing function).
+ *
+ * <p>The LCG subgenerator for {@code L64X1024Random} has an update step of the
+ * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
+ * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m}
+ * is fixed (the same for all instances of {@code L64X1024Random}}) and the addend
+ * {@code a} is a parameter (a final field of the instance). The parameter
+ * {@code a} is required to be odd (this allows the LCG to have the maximal
+ * period, namely 2<sup>64</sup>); therefore there are 2<sup>63</sup> distinct choices
+ * of parameter.
+ *
+ * <p>The Xorshift subgenerator for {@code L64X1024Random} is the {@code xoroshiro1024}
+ * algorithm (parameters 25, 27, and 36), without any final scrambler such as "+" or "**".
+ * Its state consists of an array {@code x} of sixteen {@code long} values,
+ * which can take on any values provided that they are not all zero.
+ * The period of this subgenerator is 2<sup>1024</sup>-1.
+ *
+ * <p> Because the periods 2<sup>64</sup> and 2<sup>1024</sup>-1 of the two subgenerators
+ * are relatively prime, the <em>period</em> of any single {@code L64X1024Random} object
+ * (the length of the series of generated 64-bit values before it repeats) is the product
+ * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>1024</sup>-1),
+ * which is just slightly smaller than 2<sup>1088</sup>. Moreover, if two distinct
+ * {@code L64X1024Random} objects have different {@code a} parameters, then their
+ * cycles of produced values will be different.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
+ * For any specific instance of {@code L64X1024Random}, over the course of its cycle each
+ * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>1024</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise exactly equidistributed.
+ *
+ * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 16-equidistributed.
+ * To be precise: for any specific instance of {@code L64X1024Random}, consider
+ * the (overlapping) length-16 subsequences of the cycle of 64-bit values produced by
+ * {@code nextLong()} (assuming no other methods are called that would affect the state).
+ * There are 2<sup>64</sup>(2<sup>1024</sup>-1) such subsequences, and each subsequence,
+ * which consists of 16 64-bit values, can have one of 2<sup>1024</sup> values. Of those
+ * 2<sup>1024</sup> subsequence values, nearly all of them (2<sup>1024</sup>-2<sup>64</sup>)
+ * occur 2<sup>64</sup> times over the course of the entire cycle, and the other
+ * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times. So the ratio
+ * of the probability of getting one of the less common subsequence values and the
+ * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>.
+ * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from
+ * one instance of {@code L64X1024Random} to another, as a function of the additive
+ * parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()},
+ * and {@code nextDouble()} methods are likewise 16-equidistributed.
+ *
+ * <p>Method {@link #split} constructs and returns a new {@code L64X1024Random}
+ * 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 L64X1024Random} object.
+ * This is because, with high probability, distinct {@code L64X1024Random} objects
+ * have distinct {@code a} parameters and therefore use distinct members of the
+ * algorithmic family; and even if their {@code a} parameters are the same, with
+ * very high probability they will traverse different parts of their common state
+ * cycle.
+ *
+ * <p>As with {@link java.util.SplittableRandom}, instances of
+ * {@code L64X1024Random} are <em>not</em> thread-safe.
+ * They are designed to be split, not shared, across threads. For
+ * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
+ * computation using random numbers might include a construction
+ * of the form {@code new Subtask(someL64X1024Random.split()).fork()}.
+ *
+ * <p>This class provides additional methods for generating random
+ * streams, that employ the above techniques when used in
+ * {@code stream.parallel()} mode.
+ *
+ * <p>Instances of {@code L64X1024Random} 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
+ * @since 1.9
+ */
+public final class L64X1024Random extends AbstractSplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * The split() operation uses the current generator to choose 18 new 64-bit
+ * long values that are then used to initialize the parameter `a`, the
+ * state variable `s`, and the array `x` for a newly constructed generator.
+ *
+ * With extremely high probability, no two generators so chosen
+ * will have the same `a` parameter, and testing has indicated
+ * that the values generated by two instances of {@code L64X1024Random}
+ * will be (approximately) independent if have different values for `a`.
+ *
+ * The default (no-argument) constructor, in essence, uses
+ * "defaultGen" to generate 18 new 64-bit values for the same
+ * purpose. Multiple generators created in this way will certainly
+ * differ in their `a` parameters. The defaultGen state must be accessed
+ * in a thread-safe manner, so we use an AtomicLong to represent
+ * this state. 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.
+ *
+ * File organization: First static fields, then instance
+ * fields, then constructors, then instance methods.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /*
+ * The length of the array x.
+ */
+
+ private static final int N = 16;
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is (2**1024 - 1) * 2**64.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(N*64).subtract(BigInteger.ONE).shiftLeft(64);
+
+ /*
+ * Multiplier used in the LCG portion of the algorithm, taken from
+ * Pierre L'Ecuyer, Tables of linear congruential generators of
+ * different sizes and good lattice structure, <em>Mathematics of
+ * Computation</em> 68, 225 (January 1999), pages 249–260,
+ * Table 4 (first multiplier for size 2<sup>64</sup>).
+ */
+
+ private static final long m = 2862933555777941757L;
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The parameter that is used as an additive constant for the LCG.
+ * Must be odd.
+ */
+ private final long a;
+
+ /**
+ * The per-instance state: s for the LCG; the array x for the xorshift;
+ * p is the rotating pointer into the array x.
+ * At least one of the 16 elements of the array x must be nonzero.
+ */
+ private long s;
+ private final long[] x;
+ private int p = N - 1;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public L64X1024Random(long a, long s,
+ long x0, long x1, long x2, long x3,
+ long x4, long x5, long x6, long x7,
+ long x8, long x9, long x10, long x11,
+ long x12, long x13, long x14, long x15) {
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x = new long[N];
+ this.x[0] = x0;
+ this.x[1] = x1;
+ this.x[2] = x2;
+ this.x[3] = x3;
+ this.x[4] = x4;
+ this.x[5] = x5;
+ this.x[6] = x6;
+ this.x[7] = x7;
+ this.x[8] = x8;
+ this.x[9] = x9;
+ this.x[10] = x10;
+ this.x[11] = x11;
+ this.x[12] = x12;
+ this.x[13] = x13;
+ this.x[14] = x14;
+ this.x[15] = x15;
+ // If x0, x1, ..., x15 are all zero (very unlikely), we must choose nonzero values.
+ if ((x0 | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 | x11 | x12 | x13 | x14 | x15) == 0) {
+ for (int j = 0; j < N; j++) {
+ this.x[j] = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code L64X1024Random} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code L64X1024Random} created with the same seed in the same
+ * program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X1024Random(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The seed is hashed by mixMurmur64 to produce the `a` parameter.
+ // The seed is hashed by mixStafford13 to produce the initial `x[0]`,
+ // which will then be used to produce the first generated value.
+ // The other x values are filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixMurmur64(seed ^= RngSupport.SILVER_RATIO_64),
+ 1,
+ RngSupport.mixStafford13(seed),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X1024Random} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public L64X1024Random() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X1024Random} using the specified array of
+ * initial seed bytes. Instances of {@code L64X1024Random} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X1024Random(byte[] seed) {
+ // Convert the seed to 18 long values, of which the last 16 are not all zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 18, 16);
+ long a = data[0], s = data[1];
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x = new long[N];
+ for (int j = 0; j < N; j++) {
+ this.x[j] = data[2+j];
+ }
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Constructs and returns a new instance of {@code L64X1024Random}
+ * that shares no mutable state with this instance.
+ * However, with very high probability, the set of values collectively
+ * generated by the two objects has the same statistical properties as if
+ * same the quantity of values were generated by a single thread using
+ * a single {@code L64X1024Random} object. Either or both of the two
+ * objects may be further split using the {@code split} method,
+ * and the same expected statistical properties apply to the
+ * entire set of generators constructed by such recursive splitting.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a new instance of {@code L64X1024Random}
+ */
+ public L64X1024Random split(SplittableRng source) {
+ // Literally pick a new instance "at random".
+ return new L64X1024Random(source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong());
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ // First part of xoroshiro1024: fetch array data
+ final int q = p;
+ final long s0 = x[p = (p + 1) & (N - 1)];
+ long s15 = x[q];
+
+ final long z = s + s0;
+ s = m * s + a; // LCG
+
+ // Second part of xoroshiro1024: update array data
+ s15 ^= s0;
+ x[q] = Long.rotateLeft(s0, 25) ^ s15 ^ (s15 << 27);
+ x[p] = Long.rotateLeft(s15, 36);
+
+ return z;
+ }
+
+ public BigInteger period() { return thePeriod; }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/L64X128MixRandom.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,318 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code L64X128MixRandom} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new split-off {@code L64X128MixRandom} objects,
+ * with similar usages as for class {@link java.util.SplittableRandom}.
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ * (Most recently validated with
+ * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
+ * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
+ * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
+ * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
+ * 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.
+ *
+ * <p>{@code L64X128MixRandom} is a specific member of the LXM family of algorithms
+ * for pseudorandom number generators. Every LXM generator consists of two
+ * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
+ * an Xorshift generator. Each output of an LXM generator is the sum of one
+ * output from each subgenerator, possibly processed by a final mixing function
+ * (and {@code L64X128MixRandom} does use a mixing function).
+ *
+ * <p>The LCG subgenerator for {@code L64X128MixRandom} has an update step of the
+ * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
+ * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m}
+ * is fixed (the same for all instances of {@code L64X128MixRandom}}) and the addend
+ * {@code a} is a parameter (a final field of the instance). The parameter
+ * {@code a} is required to be odd (this allows the LCG to have the maximal
+ * period, namely 2<sup>64</sup>); therefore there are 2<sup>63</sup> distinct choices
+ * of parameter.
+ *
+ * <p>The Xorshift subgenerator for {@code L64X128MixRandom} is the {@code xoroshiro128} algorithm,
+ * version 1.0 (parameters 24, 16, 37), without any final scrambler such as "+" or "**".
+ * Its state consists of two {@code long} fields {@code x0} and {@code x1},
+ * which can take on any values provided that they are not both zero.
+ * The period of this subgenerator is 2<sup>128</sup>-1.
+ *
+ * <p> The mixing function for {@code L64X128MixRandom} is the 64-bit "starstar(5,7,9)" function.
+ *
+ * <p> Because the periods 2<sup>64</sup> and 2<sup>128</sup>-1 of the two subgenerators
+ * are relatively prime, the <em>period</em> of any single {@code L64X128MixRandom} object
+ * (the length of the series of generated 64-bit values before it repeats) is the product
+ * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>128</sup>-1),
+ * which is just slightly smaller than 2<sup>192</sup>. Moreover, if two distinct
+ * {@code L64X128MixRandom} objects have different {@code a} parameters, then their
+ * cycles of produced values will be different.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
+ * For any specific instance of {@code L64X128MixRandom}, over the course of its cycle each
+ * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>128</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise exactly equidistributed.
+ *
+ * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 2-equidistributed.
+ * To be precise: for any specific instance of {@code L64X128MixRandom}, consider
+ * the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by
+ * {@code nextLong()} (assuming no other methods are called that would affect the state).
+ * There are 2<sup>64</sup>(2<sup>128</sup>-1) such subsequences, and each subsequence,
+ * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values. Of those
+ * 2<sup>128</sup> subsequence values, nearly all of them (2<sup>128</sup>-2<sup>64</sup>)
+ * occur 2<sup>64</sup> times over the course of the entire cycle, and the other
+ * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times. So the ratio
+ * of the probability of getting one of the less common subsequence values and the
+ * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>.
+ * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from
+ * one instance of {@code L64X128MixRandom} to another, as a function of the additive
+ * parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()},
+ * and {@code nextDouble()} methods are likewise 2-equidistributed.
+ *
+ * <p>Method {@link #split} constructs and returns a new {@code L64X128MixRandom}
+ * 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 L64X128MixRandom} object.
+ * This is because, with high probability, distinct {@code L64X128MixRandom} objects
+ * have distinct {@code a} parameters and therefore use distinct members of the
+ * algorithmic family; and even if their {@code a} parameters are the same, with
+ * very high probability they will traverse different parts of their common state
+ * cycle.
+ *
+ * <p>As with {@link java.util.SplittableRandom}, instances of
+ * {@code L64X128MixRandom} are <em>not</em> thread-safe.
+ * They are designed to be split, not shared, across threads. For
+ * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
+ * computation using random numbers might include a construction
+ * of the form {@code new Subtask(someL64X128MixRandom.split()).fork()}.
+ *
+ * <p>This class provides additional methods for generating random
+ * streams, that employ the above techniques when used in
+ * {@code stream.parallel()} mode.
+ *
+ * <p>Instances of {@code L64X128MixRandom} 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
+ * @since 1.9
+ */
+public final class L64X128MixRandom extends AbstractSplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * The split operation uses the current generator to choose four new 64-bit
+ * long values that are then used to initialize the parameter `a` and the
+ * state variables `s`, `x0`, and `x1` for a newly constructed generator.
+ *
+ * With extremely high probability, no two generators so chosen
+ * will have the same `a` parameter, and testing has indicated
+ * that the values generated by two instances of {@code L64X128MixRandom}
+ * will be (approximately) independent if have different values for `a`.
+ *
+ * The default (no-argument) constructor, in essence, uses
+ * "defaultGen" to generate four new 64-bit values for the same
+ * purpose. Multiple generators created in this way will certainly
+ * differ in their `a` parameters. The defaultGen state must be accessed
+ * in a thread-safe manner, so we use an AtomicLong to represent
+ * this state. 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.
+ *
+ * File organization: First static fields, then instance
+ * fields, then constructors, then instance methods.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is (2**128 - 1) * 2**64.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).shiftLeft(64);
+
+ /*
+ * Multiplier used in the LCG portion of the algorithm, taken from
+ * Pierre L'Ecuyer, Tables of linear congruential generators of
+ * different sizes and good lattice structure, <em>Mathematics of
+ * Computation</em> 68, 225 (January 1999), pages 249–260,
+ * Table 4 (first multiplier for size 2<sup>64</sup>).
+ */
+
+ private static final long m = 2862933555777941757L;
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The parameter that is used as an additive constant for the LCG.
+ * Must be odd.
+ */
+ private final long a;
+
+ /**
+ * The per-instance state: s for the LCG; x0 and x1 for the xorshift.
+ * At least one of x0 and x1 must be nonzero.
+ */
+ private long s, x0, x1;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public L64X128MixRandom(long a, long s, long x0, long x1) {
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x0 = x0;
+ this.x1 = x1;
+ // If x0 and x1 are both zero, we must choose nonzero values.
+ if ((x0 | x1) == 0) {
+ // At least one of the two values generated here will be nonzero.
+ this.x0 = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ this.x1 = RngSupport.mixStafford13(s + RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code L64X128MixRandom} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code L64X128MixRandom} created with the same seed in the same
+ * program generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X128MixRandom(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The seed is hashed by mixMurmur64 to produce the `a` parameter.
+ // The seed is hashed by mixStafford13 to produce the initial `x0`,
+ // which will then be used to produce the first generated value.
+ // Then x1 is filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixMurmur64(seed ^= RngSupport.SILVER_RATIO_64),
+ 1,
+ RngSupport.mixStafford13(seed),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X128MixRandom} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public L64X128MixRandom() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X128MixRandom} using the specified array of
+ * initial seed bytes. Instances of {@code L64X128MixRandom} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X128MixRandom(byte[] seed) {
+ // Convert the seed to 4 long values, of which the last 2 are not all zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 4, 2);
+ long a = data[0], s = data[1], x0 = data[2], x1 = data[3];
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x0 = x0;
+ this.x1 = x1;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Constructs and returns a new instance of {@code L64X128MixRandom}
+ * that shares no mutable state with this instance.
+ * However, with very high probability, the set of values collectively
+ * generated by the two objects has the same statistical properties as if
+ * same the quantity of values were generated by a single thread using
+ * a single {@code L64X128MixRandom} object. Either or both of the two
+ * objects may be further split using the {@code split} method,
+ * and the same expected statistical properties apply to the
+ * entire set of generators constructed by such recursive splitting.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a new instance of {@code L64X128MixRandom}
+ */
+ public L64X128MixRandom split(SplittableRng source) {
+ // Literally pick a new instance "at random".
+ return new L64X128MixRandom(source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong());
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ final long z = s + x0;
+ s = m * s + a; // LCG
+ long q0 = x0, q1 = x1;
+ { q1 ^= q0; q0 = Long.rotateLeft(q0, 24); q0 = q0 ^ q1 ^ (q1 << 16); q1 = Long.rotateLeft(q1, 37); } // xoroshiro128v1_0
+ x0 = q0; x1 = q1;
+ return Long.rotateLeft(z * 5, 7) * 9; // "starstar" mixing function
+ }
+
+ public BigInteger period() { return thePeriod; }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/L64X128Random.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,314 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code L64X128Random} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new split-off {@code L64X128Random} objects,
+ * with similar usages as for class {@link java.util.SplittableRandom}.
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ * (Most recently validated with
+ * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
+ * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
+ * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
+ * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
+ * 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.
+ *
+ * <p>{@code L64X128Random} is a specific member of the LXM family of algorithms
+ * for pseudorandom number generators. Every LXM generator consists of two
+ * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
+ * an Xorshift generator. Each output of an LXM generator is the sum of one
+ * output from each subgenerator, possibly processed by a final mixing function
+ * (but {@code L64X128Random} does not use a mixing function).
+ *
+ * <p>The LCG subgenerator for {@code L64X128Random} has an update step of the
+ * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
+ * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m}
+ * is fixed (the same for all instances of {@code L64X128Random}}) and the addend
+ * {@code a} is a parameter (a final field of the instance). The parameter
+ * {@code a} is required to be odd (this allows the LCG to have the maximal
+ * period, namely 2<sup>64</sup>); therefore there are 2<sup>63</sup> distinct choices
+ * of parameter.
+ *
+ * <p>The Xorshift subgenerator for {@code L64X128Random} is the {@code xoroshiro128} algorithm,
+ * version 1.0 (parameters 24, 16, 37), without any final scrambler such as "+" or "**".
+ * Its state consists of two {@code long} fields {@code x0} and {@code x1},
+ * which can take on any values provided that they are not both zero.
+ * The period of this subgenerator is 2<sup>128</sup>-1.
+ *
+ * <p> Because the periods 2<sup>64</sup> and 2<sup>128</sup>-1 of the two subgenerators
+ * are relatively prime, the <em>period</em> of any single {@code L64X128Random} object
+ * (the length of the series of generated 64-bit values before it repeats) is the product
+ * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>128</sup>-1),
+ * which is just slightly smaller than 2<sup>192</sup>. Moreover, if two distinct
+ * {@code L64X128Random} objects have different {@code a} parameters, then their
+ * cycles of produced values will be different.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
+ * For any specific instance of {@code L64X128Random}, over the course of its cycle each
+ * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>128</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise exactly equidistributed.
+ *
+ * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 2-equidistributed.
+ * To be precise: for any specific instance of {@code L64X128Random}, consider
+ * the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by
+ * {@code nextLong()} (assuming no other methods are called that would affect the state).
+ * There are 2<sup>64</sup>(2<sup>128</sup>-1) such subsequences, and each subsequence,
+ * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values. Of those
+ * 2<sup>128</sup> subsequence values, nearly all of them (2<sup>128</sup>-2<sup>64</sup>)
+ * occur 2<sup>64</sup> times over the course of the entire cycle, and the other
+ * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times. So the ratio
+ * of the probability of getting one of the less common subsequence values and the
+ * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>.
+ * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from
+ * one instance of {@code L64X128Random} to another, as a function of the additive
+ * parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()},
+ * and {@code nextDouble()} methods are likewise 2-equidistributed.
+ *
+ * <p>Method {@link #split} constructs and returns a new {@code L64X128Random}
+ * 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 L64X128Random} object.
+ * This is because, with high probability, distinct {@code L64X128Random} objects
+ * have distinct {@code a} parameters and therefore use distinct members of the
+ * algorithmic family; and even if their {@code a} parameters are the same, with
+ * very high probability they will traverse different parts of their common state
+ * cycle.
+ *
+ * <p>As with {@link java.util.SplittableRandom}, instances of
+ * {@code L64X128Random} are <em>not</em> thread-safe.
+ * They are designed to be split, not shared, across threads. For
+ * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
+ * computation using random numbers might include a construction
+ * of the form {@code new Subtask(someL64X128Random.split()).fork()}.
+ *
+ * <p>This class provides additional methods for generating random
+ * streams, that employ the above techniques when used in
+ * {@code stream.parallel()} mode.
+ *
+ * <p>Instances of {@code L64X128Random} 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
+ * @since 1.9
+ */
+public final class L64X128Random extends AbstractSplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * The split operation uses the current generator to choose four new 64-bit
+ * long values that are then used to initialize the parameter `a` and the
+ * state variables `s`, `x0`, and `x1` for a newly constructed generator.
+ *
+ * With extremely high probability, no two generators so chosen
+ * will have the same `a` parameter, and testing has indicated
+ * that the values generated by two instances of {@code L64X128Random}
+ * will be (approximately) independent if have different values for `a`.
+ *
+ * The default (no-argument) constructor, in essence, uses
+ * "defaultGen" to generate four new 64-bit values for the same
+ * purpose. Multiple generators created in this way will certainly
+ * differ in their `a` parameters. The defaultGen state must be accessed
+ * in a thread-safe manner, so we use an AtomicLong to represent
+ * this state. 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.
+ *
+ * File organization: First static fields, then instance
+ * fields, then constructors, then instance methods.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is (2**128 - 1) * 2**64.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).shiftLeft(64);
+
+ /*
+ * Multiplier used in the LCG portion of the algorithm, taken from
+ * Pierre L'Ecuyer, Tables of linear congruential generators of
+ * different sizes and good lattice structure, <em>Mathematics of
+ * Computation</em> 68, 225 (January 1999), pages 249–260,
+ * Table 4 (first multiplier for size 2<sup>64</sup>).
+ */
+
+ private static final long m = 2862933555777941757L;
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The parameter that is used as an additive constant for the LCG.
+ * Must be odd.
+ */
+ private final long a;
+
+ /**
+ * The per-instance state: s for the LCG; x0 and x1 for the xorshift.
+ * At least one of x0 and x1 must be nonzero.
+ */
+ private long s, x0, x1;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public L64X128Random(long a, long s, long x0, long x1) {
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ // If x0 and x1 are both zero, we must choose nonzero values.
+ if ((x0 | x1) == 0) {
+ // At least one of the two values generated here will be nonzero.
+ this.x0 = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ this.x1 = RngSupport.mixStafford13(s + RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code L64X128Random} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code L64X128Random} created with the same seed in the same
+ * program generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X128Random(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The seed is hashed by mixMurmur64 to produce the `a` parameter.
+ // The seed is hashed by mixStafford13 to produce the initial `x0`,
+ // which will then be used to produce the first generated value.
+ // Then x1 is filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixMurmur64(seed ^= RngSupport.SILVER_RATIO_64),
+ 1,
+ RngSupport.mixStafford13(seed),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X128Random} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public L64X128Random() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X128MixRandom} using the specified array of
+ * initial seed bytes. Instances of {@code L64X128MixRandom} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X128Random(byte[] seed) {
+ // Convert the seed to 4 long values, of which the last 2 are not all zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 4, 2);
+ long a = data[0], s = data[1], x0 = data[2], x1 = data[3];
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x0 = x0;
+ this.x1 = x1;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Constructs and returns a new instance of {@code L64X128Random}
+ * that shares no mutable state with this instance.
+ * However, with very high probability, the set of values collectively
+ * generated by the two objects has the same statistical properties as if
+ * same the quantity of values were generated by a single thread using
+ * a single {@code L64X128Random} object. Either or both of the two
+ * objects may be further split using the {@code split} method,
+ * and the same expected statistical properties apply to the
+ * entire set of generators constructed by such recursive splitting.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a new instance of {@code L64X128Random}
+ */
+ public L64X128Random split(SplittableRng source) {
+ // Literally pick a new instance "at random".
+ return new L64X128Random(source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong());
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ final long z = s + x0;
+ s = m * s + a; // LCG
+ long q0 = x0, q1 = x1;
+ { q1 ^= q0; q0 = Long.rotateLeft(q0, 24); q0 = q0 ^ q1 ^ (q1 << 16); q1 = Long.rotateLeft(q1, 37); } // xoroshiro128v1_0
+ x0 = q0; x1 = q1;
+ return z;
+ }
+
+ public BigInteger period() { return thePeriod; }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/L64X256MixRandom.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,328 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code L64X256MixRandom} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new split-off {@code L64X256MixRandom} objects,
+ * with similar usages as for class {@link java.util.SplittableRandom}.
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ * (Most recently validated with
+ * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
+ * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
+ * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
+ * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
+ * 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.
+ *
+ * <p>{@code L64X256MixRandom} is a specific member of the LXM family of algorithms
+ * for pseudorandom number generators. Every LXM generator consists of two
+ * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
+ * an Xorshift generator. Each output of an LXM generator is the sum of one
+ * output from each subgenerator, possibly processed by a final mixing function
+ * (and {@code L64X256MixRandom} does use a mixing function).
+ *
+ * <p>The LCG subgenerator for {@code L64X256MixRandom} has an update step of the
+ * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
+ * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m}
+ * is fixed (the same for all instances of {@code L64X256MixRandom}}) and the addend
+ * {@code a} is a parameter (a final field of the instance). The parameter
+ * {@code a} is required to be odd (this allows the LCG to have the maximal
+ * period, namely 2<sup>64</sup>); therefore there are 2<sup>63</sup> distinct choices
+ * of parameter.
+ *
+ * <p>The Xorshift subgenerator for {@code L64X256MixRandom} is the {@code xoshiro256} algorithm,
+ * version 1.0 (parameters 17, 45), without any final scrambler such as "+" or "**".
+ * Its state consists of four {@code long} fields {@code x0}, {@code x1}, {@code x2},
+ * and {@code x3}, which can take on any values provided that they are not all zero.
+ * The period of this subgenerator is 2<sup>256</sup>-1.
+ *
+ * <p> The mixing function for {@code L64X256MixRandom} is the 64-bit MurmurHash3 finalizer.
+ *
+ * <p> Because the periods 2<sup>64</sup> and 2<sup>256</sup>-1 of the two subgenerators
+ * are relatively prime, the <em>period</em> of any single {@code L64X256MixRandom} object
+ * (the length of the series of generated 64-bit values before it repeats) is the product
+ * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>256</sup>-1),
+ * which is just slightly smaller than 2<sup>320</sup>. Moreover, if two distinct
+ * {@code L64X256MixRandom} objects have different {@code a} parameters, then their
+ * cycles of produced values will be different.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
+ * For any specific instance of {@code L64X256MixRandom}, over the course of its cycle each
+ * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>256</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise exactly equidistributed.
+ *
+ * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 4-equidistributed.
+ * To be precise: for any specific instance of {@code L64X256MixRandom}, consider
+ * the (overlapping) length-4 subsequences of the cycle of 64-bit values produced by
+ * {@code nextLong()} (assuming no other methods are called that would affect the state).
+ * There are 2<sup>64</sup>(2<sup>256</sup>-1) such subsequences, and each subsequence,
+ * which consists of 4 64-bit values, can have one of 2<sup>256</sup> values. Of those
+ * 2<sup>256</sup> subsequence values, nearly all of them (2<sup>256</sup>-2<sup>64</sup>)
+ * occur 2<sup>64</sup> times over the course of the entire cycle, and the other
+ * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times. So the ratio
+ * of the probability of getting one of the less common subsequence values and the
+ * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>.
+ * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from
+ * one instance of {@code L64X256MixRandom} to another, as a function of the additive
+ * parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()},
+ * and {@code nextDouble()} methods are likewise 4-equidistributed.
+ *
+ * <p>Method {@link #split} constructs and returns a new {@code L64X256MixRandom}
+ * 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 L64X256MixRandom} object.
+ * This is because, with high probability, distinct {@code L64X256MixRandom} objects
+ * have distinct {@code a} parameters and therefore use distinct members of the
+ * algorithmic family; and even if their {@code a} parameters are the same, with
+ * very high probability they will traverse different parts of their common state
+ * cycle.
+ *
+ * <p>As with {@link java.util.SplittableRandom}, instances of
+ * {@code L64X256MixRandom} are <em>not</em> thread-safe.
+ * They are designed to be split, not shared, across threads. For
+ * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
+ * computation using random numbers might include a construction
+ * of the form {@code new Subtask(someL64X256MixRandom.split()).fork()}.
+ *
+ * <p>This class provides additional methods for generating random
+ * streams, that employ the above techniques when used in
+ * {@code stream.parallel()} mode.
+ *
+ * <p>Instances of {@code L64X256MixRandom} 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
+ * @since 1.9
+ */
+public final class L64X256MixRandom extends AbstractSplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * The split operation uses the current generator to choose six new 64-bit
+ * long values that are then used to initialize the parameter `a` and the
+ * state variables `s`, `x0`, `x1`, `x2`, and `x3` for a newly constructed
+ * generator.
+ *
+ * With extremely high probability, no two generators so chosen
+ * will have the same `a` parameter, and testing has indicated
+ * that the values generated by two instances of {@code L64X256MixRandom}
+ * will be (approximately) independent if have different values for `a`.
+ *
+ * The default (no-argument) constructor, in essence, uses
+ * "defaultGen" to generate six new 64-bit values for the same
+ * purpose. Multiple generators created in this way will certainly
+ * differ in their `a` parameters. The defaultGen state must be accessed
+ * in a thread-safe manner, so we use an AtomicLong to represent
+ * this state. 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.
+ *
+ * File organization: First static fields, then instance
+ * fields, then constructors, then instance methods.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is (2**256 - 1) * 2**64.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE).shiftLeft(64);
+
+ /*
+ * Multiplier used in the LCG portion of the algorithm, taken from
+ * Pierre L'Ecuyer, Tables of linear congruential generators of
+ * different sizes and good lattice structure, <em>Mathematics of
+ * Computation</em> 68, 225 (January 1999), pages 249–260,
+ * Table 4 (first multiplier for size 2<sup>64</sup>).
+ */
+
+ private static final long m = 2862933555777941757L;
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The parameter that is used as an additive constant for the LCG.
+ * Must be odd.
+ */
+ private final long a;
+
+ /**
+ * The per-instance state: s for the LCG; x0, x1, x2, and x3 for the xorshift.
+ * At least one of the four fields x0, x1, x2, and x3 must be nonzero.
+ */
+ private long s, x0, x1, x2, x3;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public L64X256MixRandom(long a, long s, long x0, long x1, long x2, long x3) {
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x0 = x0;
+ this.x1 = x1;
+ this.x2 = x2;
+ this.x3 = x3;
+ // If x0, x1, x2, and x3 are all zero, we must choose nonzero values.
+ if ((x0 | x1 | x2 | x3) == 0) {
+ // At least three of the four values generated here will be nonzero.
+ this.x0 = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ this.x1 = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ this.x2 = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ this.x3 = RngSupport.mixStafford13(s + RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code L64X256MixRandom} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code L64X256MixRandom} created with the same seed in the same
+ * program generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X256MixRandom(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The seed is hashed by mixMurmur64 to produce the `a` parameter.
+ // The seed is hashed by mixStafford13 to produce the initial `x0`,
+ // which will then be used to produce the first generated value.
+ // The other x values are filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixMurmur64(seed ^= RngSupport.SILVER_RATIO_64),
+ 1,
+ RngSupport.mixStafford13(seed),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X256MixRandom} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public L64X256MixRandom() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X256MixRandom} using the specified array of
+ * initial seed bytes. Instances of {@code L64X256MixRandom} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X256MixRandom(byte[] seed) {
+ // Convert the seed to 6 long values, of which the last 4 are not all zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 6, 4);
+ long a = data[0], s = data[1], x0 = data[2], x1 = data[3], x2 = data[4], x3 = data[5];
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x0 = x0;
+ this.x1 = x1;
+ this.x2 = x2;
+ this.x3 = x3;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Constructs and returns a new instance of {@code L64X256MixRandom}
+ * that shares no mutable state with this instance.
+ * However, with very high probability, the set of values collectively
+ * generated by the two objects has the same statistical properties as if
+ * same the quantity of values were generated by a single thread using
+ * a single {@code L64X256MixRandom} object. Either or both of the two
+ * objects may be further split using the {@code split} method,
+ * and the same expected statistical properties apply to the
+ * entire set of generators constructed by such recursive splitting.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a new instance of {@code L64X256MixRandom}
+ */
+ public L64X256MixRandom split(SplittableRng source) {
+ // Literally pick a new instance "at random".
+ return new L64X256MixRandom(source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong());
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ final long z = s + x0;
+ s = m * s + a; // LCG
+ long q0 = x0, q1 = x1, q2 = x2, q3 = x3;
+ { long t = q1 << 17; q2 ^= q0; q3 ^= q1; q1 ^= q2; q0 ^= q3; q2 ^= t; q3 = Long.rotateLeft(q3, 45); } // xoshiro256 1.0
+ x0 = q0; x1 = q1; x2 = q2; x3 = q3;
+ return RngSupport.mixLea64(z); // mixing function
+ }
+
+ public BigInteger period() { return thePeriod; }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/L64X256Random.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,326 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code L64X256Random} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new split-off {@code L64X256Random} objects,
+ * with similar usages as for class {@link java.util.SplittableRandom}.
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ * (Most recently validated with
+ * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
+ * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
+ * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
+ * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
+ * 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.
+ *
+ * <p>{@code L64X256Random} is a specific member of the LXM family of algorithms
+ * for pseudorandom number generators. Every LXM generator consists of two
+ * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
+ * an Xorshift generator. Each output of an LXM generator is the sum of one
+ * output from each subgenerator, possibly processed by a final mixing function
+ * (but {@code L64X256Random} does not use a mixing function).
+ *
+ * <p>The LCG subgenerator for {@code L64X256Random} has an update step of the
+ * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
+ * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m}
+ * is fixed (the same for all instances of {@code L64X256Random}}) and the addend
+ * {@code a} is a parameter (a final field of the instance). The parameter
+ * {@code a} is required to be odd (this allows the LCG to have the maximal
+ * period, namely 2<sup>64</sup>); therefore there are 2<sup>63</sup> distinct choices
+ * of parameter.
+ *
+ * <p>The Xorshift subgenerator for {@code L64X256Random} is the {@code xoshiro256} algorithm,
+ * version 1.0 (parameters 17, 45), without any final scrambler such as "+" or "**".
+ * Its state consists of four {@code long} fields {@code x0}, {@code x1}, {@code x2},
+ * and {@code x3}, which can take on any values provided that they are not all zero.
+ * The period of this subgenerator is 2<sup>256</sup>-1.
+ *
+ * <p> Because the periods 2<sup>64</sup> and 2<sup>256</sup>-1 of the two subgenerators
+ * are relatively prime, the <em>period</em> of any single {@code L64X256Random} object
+ * (the length of the series of generated 64-bit values before it repeats) is the product
+ * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>256</sup>-1),
+ * which is just slightly smaller than 2<sup>320</sup>. Moreover, if two distinct
+ * {@code L64X256Random} objects have different {@code a} parameters, then their
+ * cycles of produced values will be different.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
+ * For any specific instance of {@code L64X256Random}, over the course of its cycle each
+ * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>256</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise exactly equidistributed.
+ *
+ * <p> In fact, the 64-bit values produced by the {@code nextLong()} method are 4-equidistributed.
+ * To be precise: for any specific instance of {@code L64X256Random}, consider
+ * the (overlapping) length-4 subsequences of the cycle of 64-bit values produced by
+ * {@code nextLong()} (assuming no other methods are called that would affect the state).
+ * There are 2<sup>64</sup>(2<sup>256</sup>-1) such subsequences, and each subsequence,
+ * which consists of 4 64-bit values, can have one of 2<sup>256</sup> values. Of those
+ * 2<sup>256</sup> subsequence values, nearly all of them (2<sup>256</sup>-2<sup>64</sup>)
+ * occur 2<sup>64</sup> times over the course of the entire cycle, and the other
+ * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times. So the ratio
+ * of the probability of getting one of the less common subsequence values and the
+ * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>.
+ * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from
+ * one instance of {@code L64X256Random} to another, as a function of the additive
+ * parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()},
+ * and {@code nextDouble()} methods are likewise 4-equidistributed.
+ *
+ * <p>Method {@link #split} constructs and returns a new {@code L64X256Random}
+ * 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 L64X256Random} object.
+ * This is because, with high probability, distinct {@code L64X256Random} objects
+ * have distinct {@code a} parameters and therefore use distinct members of the
+ * algorithmic family; and even if their {@code a} parameters are the same, with
+ * very high probability they will traverse different parts of their common state
+ * cycle.
+ *
+ * <p>As with {@link java.util.SplittableRandom}, instances of
+ * {@code L64X256Random} are <em>not</em> thread-safe.
+ * They are designed to be split, not shared, across threads. For
+ * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
+ * computation using random numbers might include a construction
+ * of the form {@code new Subtask(someL64X256Random.split()).fork()}.
+ *
+ * <p>This class provides additional methods for generating random
+ * streams, that employ the above techniques when used in
+ * {@code stream.parallel()} mode.
+ *
+ * <p>Instances of {@code L64X256Random} 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
+ * @since 1.9
+ */
+public final class L64X256Random extends AbstractSplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * The split() operation uses the current generator to choose six new 64-bit
+ * long values that are then used to initialize the parameter `a` and the
+ * state variables `s`, `x0`, `x1`, `x2`, and `x3` for a newly constructed
+ * generator.
+ *
+ * With extremely high probability, no two generators so chosen
+ * will have the same `a` parameter, and testing has indicated
+ * that the values generated by two instances of {@code L64X256Random}
+ * will be (approximately) independent if have different values for `a`.
+ *
+ * The default (no-argument) constructor, in essence, uses
+ * "defaultGen" to generate six new 64-bit values for the same
+ * purpose. Multiple generators created in this way will certainly
+ * differ in their `a` parameters. The defaultGen state must be accessed
+ * in a thread-safe manner, so we use an AtomicLong to represent
+ * this state. 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.
+ *
+ * File organization: First static fields, then instance
+ * fields, then constructors, then instance methods.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is (2**256 - 1) * 2**64.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE).shiftLeft(64);
+
+ /*
+ * Multiplier used in the LCG portion of the algorithm, taken from
+ * Pierre L'Ecuyer, Tables of linear congruential generators of
+ * different sizes and good lattice structure, <em>Mathematics of
+ * Computation</em> 68, 225 (January 1999), pages 249–260,
+ * Table 4 (first multiplier for size 2<sup>64</sup>).
+ */
+
+ private static final long m = 2862933555777941757L;
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The parameter that is used as an additive constant for the LCG.
+ * Must be odd.
+ */
+ private final long a;
+
+ /**
+ * The per-instance state: s for the LCG; x0, x1, x2, and x3 for the xorshift.
+ * At least one of the four fields x0, x1, x2, and x3 must be nonzero.
+ */
+ private long s, x0, x1, x2, x3;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public L64X256Random(long a, long s, long x0, long x1, long x2, long x3) {
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x0 = x0;
+ this.x1 = x1;
+ this.x2 = x2;
+ this.x3 = x3;
+ // If x0, x1, x2, and x3 are all zero, we must choose nonzero values.
+ if ((x0 | x1 | x2 | x3) == 0) {
+ // At least three of the four values generated here will be nonzero.
+ this.x0 = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ this.x1 = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ this.x2 = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
+ this.x3 = RngSupport.mixStafford13(s + RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code L64X256Random} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code L64X256Random} created with the same seed in the same
+ * program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X256Random(long seed) {
+ // Using a value with irregularly spaced 1-bit to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The seed is hashed by mixMurmur64 to produce the `a` parameter.
+ // The seed is hashed by mixStafford13 to produce the initial `x0`,
+ // which will then be used to produce the first generated value.
+ // The other x values are filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixMurmur64(seed ^= RngSupport.SILVER_RATIO_64),
+ 1,
+ RngSupport.mixStafford13(seed),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X256Random} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public L64X256Random() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code L64X256Random} using the specified array of
+ * initial seed bytes. Instances of {@code L64X256Random} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public L64X256Random(byte[] seed) {
+ // Convert the seed to 6 long values, of which the last 4 are not all zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 6, 4);
+ long a = data[0], s = data[1], x0 = data[2], x1 = data[3], x2 = data[4], x3 = data[5];
+ // Force a to be odd.
+ this.a = a | 1;
+ this.s = s;
+ this.x0 = x0;
+ this.x1 = x1;
+ this.x2 = x2;
+ this.x3 = x3;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Constructs and returns a new instance of {@code L64X256Random}
+ * that shares no mutable state with this instance.
+ * However, with very high probability, the set of values collectively
+ * generated by the two objects has the same statistical properties as if
+ * same the quantity of values were generated by a single thread using
+ * a single {@code L64X256Random} object. Either or both of the two
+ * objects may be further split using the {@code split} method,
+ * and the same expected statistical properties apply to the
+ * entire set of generators constructed by such recursive splitting.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a new instance of {@code L64X256Random}
+ */
+ public L64X256Random split(SplittableRng source) {
+ // Literally pick a new instance "at random".
+ return new L64X256Random(source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong(),
+ source.nextLong(), source.nextLong());
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ final long z = s + x0;
+ s = m * s + a; // LCG
+ long q0 = x0, q1 = x1, q2 = x2, q3 = x3;
+ { long t = q1 << 17; q2 ^= q0; q3 ^= q1; q1 ^= q2; q0 ^= q3; q2 ^= t; q3 = Long.rotateLeft(q3, 45); } // xoshiro256 1.0
+ x0 = q0; x1 = q1; x2 = q2; x3 = q3;
+ return z;
+ }
+
+ public BigInteger period() { return thePeriod; }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/LeapableRng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.stream.Stream;
+
+/**
+ * This interface is designed to provide a common protocol for objects
+ * that generate sequences of pseudorandom numbers (or Boolean values)
+ * and furthermore can easily not only jump but also <it>leap</it> to
+ * a very distant point in the state cycle.
+ *
+ * Typically one will construct a series of {@code LeapableRng} objects
+ * by iterative leaping from a single original {@code LeapableRng}
+ * object, and then for each such object produce a subseries of objects
+ * by iterative jumping. There is little conceptual difference between
+ * leaping and jumping, but typically a leap will be a very long jump
+ * in the state cycle (perhaps distance 2<sup>128</sup> or so).
+ *
+ * <p>Ideally, all {@code LeapableRng} objects produced by iterative
+ * leaping and jumping from a single original {@code LeapableRng} object
+ * are statistically independent of one another and individually uniform.
+ * In practice, one must settle for some approximation to independence
+ * and uniformity. In particular, a specific implementation may
+ * assume that each generator in a stream produced by the {@code leaps}
+ * method is used to produce (by jumping) a number of objects no larger
+ * than 2<sup>64</sup>. Implementors are advised to use algorithms
+ * whose period is at least 2<sup>191</sup>.
+ *
+ * <p>Methods are provided to perform a single leap operation and also
+ * to produce a stream of generators produced from the original by
+ * iterative copying and leaping of internal state. The generators
+ * produced must implement the {@code JumpableRng} interface but need
+ * not also implement the {@code LeapableRng} interface. A typical
+ * strategy for a multithreaded application is to create a single
+ * {@code LeapableRng} object, calls its {@code leaps} method exactly
+ * once, and then parcel out generators from the resulting stream, one
+ * to each thread. Then the {@code jumps} method of each such generator
+ * be called to produce a substream of generator objects.
+ *
+ * <p>An implementation of the {@code LeapableRng} interface must provide
+ * concrete definitions for the methods {@code nextInt()}, {@code nextLong},
+ * {@code period()}, {@code copy()}, {@code jump()}, {@code defaultJumpDistance()},
+ * {@code leap()}, and {@code defaultLeapDistance()}.
+ * Default implementations are provided for all other methods.
+ *
+ * <p>Objects that implement {@code java.util.LeapableRng} are
+ * typically not cryptographically secure. Consider instead using
+ * {@link java.security.SecureRandom} to get a cryptographically
+ * secure pseudo-random number generator for use by
+ * security-sensitive applications.
+ *
+ * @author Guy Steele
+ * @since 1.9
+ */
+interface LeapableRng extends JumpableRng {
+ /**
+ * Returns a new generator whose internal state is an exact copy
+ * of this generator (therefore their future behavior should be
+ * identical if subjected to the same series of operations).
+ *
+ * @return a new object that is a copy of this generator
+ */
+ LeapableRng copy();
+
+ /**
+ * Alter the state of this pseudorandom number generator so as to
+ * leap forward a large, fixed distance (typically 2<sup>96</sup>
+ * or more) within its state cycle.
+ */
+ void leap();
+
+ /**
+ * Returns the distance by which the {@code leap()} method will leap
+ * forward within the state cycle of this generator object.
+ *
+ * @return the default leap distance (as a {@code double} value)
+ */
+ double defaultLeapDistance();
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code JumpableRng}
+ * interface.
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to {@code leaps(Long.MAX_VALUE)}.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code copy()} and {@code leap()} on this generator,
+ * and the copies become the generators produced by the stream.
+ *
+ * @return a stream of objects that implement the {@code JumpableRng} interface
+ */
+ default Stream<JumpableRng> leaps() {
+ return Stream.generate(this::copyAndLeap).sequential();
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code JumpableRng} interface.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code copy()} and {@code leap()} on this generator,
+ * and the copies become the generators produced by the stream.
+ *
+ * @param streamSize the number of generators to generate
+ * @return a stream of objects that implement the {@code JumpableRng} interface
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ default Stream<JumpableRng> leaps(long streamSize) {
+ return leaps().limit(streamSize);
+ }
+
+ /**
+ * Copy this generator, leap this generator forward, then return the copy.
+ */
+ default JumpableRng copyAndLeap() {
+ JumpableRng result = copy();
+ leap();
+ return result;
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/MRG32k3a.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,450 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code MRG32k3a} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.AbstractArbitrarilyJumpableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new {@code Xoroshiro128PlusMRG32k3a} objects
+ * by "jumping" or "leaping".
+ *
+ * <p>Instances {@code Xoroshiro128Plus} are <em>not</em> thread-safe.
+ * They are designed to be used so that each thread as its own instance.
+ * The methods {@link #jump} and {@link #leap} and {@link #jumps} and {@link #leaps}
+ * can be used to construct new instances of {@code Xoroshiro128Plus} that traverse
+ * other parts of the state cycle.
+ *
+ * <p>Instances of {@code MRG32k3a} 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
+ * @since 1.9
+ */
+public final class MRG32k3a extends AbstractArbitrarilyJumpableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * xxxx
+ *
+ * File organization: First the non-public methods that constitute
+ * the main algorithm, then the main public methods, followed by
+ * some custom spliterator classes needed for stream methods.
+ */
+
+ private final static double norm1 = 2.328306549295728e-10;
+ private final static double norm2 = 2.328318824698632e-10;
+ private final static double m1 = 4294967087.0;
+ private final static double m2 = 4294944443.0;
+ private final static double a12 = 1403580.0;
+ private final static double a13n = 810728.0;
+ private final static double a21 = 527612.0;
+ private final static double a23n = 1370589.0;
+ private final static int m1_deficit = 209;
+
+ // IllegalArgumentException messages
+ private static final String BadLogDistance = "logDistance must be non-negative and not greater than 192";
+
+ /**
+ * The per-instance state.
+ The seeds for s10, s11, s12 must be integers in [0, m1 - 1] and not all 0.
+ The seeds for s20, s21, s22 must be integers in [0, m2 - 1] and not all 0.
+ */
+ private double s10, s11, s12,
+ s20, s21, s22;
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ 32-bits Random number generator U(0,1): MRG32k3a
+ Author: Pierre L'Ecuyer,
+ Source: Good Parameter Sets for Combined Multiple Recursive Random
+ Number Generators,
+ Shorter version in Operations Research,
+ 47, 1 (1999), 159--164.
+ ---------------------------------------------------------
+ */
+
+ private void nextState() {
+ /* Component 1 */
+ double p1 = a12 * s11 - a13n * s10;
+ double k1 = p1 / m1; p1 -= k1 * m1; if (p1 < 0.0) p1 += m1;
+ s10 = s11; s11 = s12; s12 = p1;
+ /* Component 2 */
+ double p2 = a21 * s22 - a23n * s20;
+ double k2 = p2 / m2; p2 -= k2 * m2; if (p2 < 0.0) p2 += m2;
+ s20 = s21; s21 = s22; s22 = p2;
+ }
+
+
+ /**
+ * The form of nextInt used by IntStream Spliterators.
+ * Exactly the same as long version, except for types.
+ *
+ * @param origin the least value, unless greater than bound
+ * @param bound the upper bound (exclusive), must not equal origin
+ * @return a pseudorandom value
+ */
+ protected int internalNextInt(int origin, int bound) {
+ if (origin < bound) {
+ final int n = bound - origin;
+ final int m = n - 1;
+ if (n > 0) {
+ int r;
+ for (int u = (int)nextDouble() >>> 1;
+ u + m + ((m1_deficit + 1) >>> 1) - (r = u % n) < 0;
+ u = (int)nextDouble() >>> 1)
+ ;
+ return (r + origin);
+ } else {
+ return RngSupport.boundedNextInt(this, origin, bound);
+ }
+ } else {
+ return nextInt();
+ }
+ }
+
+ protected int internalNextInt(int bound) {
+ // Specialize internalNextInt for origin == 0, bound > 0
+ final int n = bound;
+ final int m = n - 1;
+ int r;
+ for (int u = (int)nextDouble() >>> 1;
+ u + m + ((m1_deficit + 1) >>> 1) - (r = u % n) < 0;
+ u = (int)nextDouble() >>> 1)
+ ;
+ return r;
+ }
+
+ /**
+ * Constructor used by all others except default constructor.
+ * All arguments must be known to be nonnegative integral values.
+ */
+ private MRG32k3a(double s10, double s11, double s12,
+ double s20, double s21, double s22) {
+ this.s10 = s10; this.s11 = s11; this.s12 = s12;
+ this.s20 = s20; this.s21 = s21; this.s22 = s22;
+ if ((s10 == 0.0) && (s11 == 0.0) && (s12 == 0.0)) this.s10 = 12345.0;
+ if ((s20 == 0.0) && (s21 == 0.0) && (s22 == 0.0)) this.s20 = 12345.0;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ public MRG32k3a(int s10, int s11, int s12,
+ int s20, int s21, int s22) {
+ this(((double)(((long)s10) & 0x00000000ffffffffL)) % m1,
+ ((double)(((long)s11) & 0x00000000ffffffffL)) % m1,
+ ((double)(((long)s12) & 0x00000000ffffffffL)) % m1,
+ ((double)(((long)s20) & 0x00000000ffffffffL)) % m2,
+ ((double)(((long)s21) & 0x00000000ffffffffL)) % m2,
+ ((double)(((long)s22) & 0x00000000ffffffffL)) % m2);
+ }
+
+ /**
+ * Creates a new MRG32k3a instance using the specified
+ * initial seed. MRG32k3a instances created with the same
+ * seed in the same program generate identical sequences of values.
+ * An argument of 0 seeds the generator to a widely used initialization
+ * of MRG32k3a: all six state variables are set to 12345.
+ *
+ * @param seed the initial seed
+ */
+ public MRG32k3a(long seed) {
+ this((double)((seed & 0x7FF) + 12345),
+ (double)(((seed >>> 11) & 0x7FF) + 12345),
+ (double)(((seed >>> 22) & 0x7FF) + 12345),
+ (double)(((seed >>> 33) & 0x7FF) + 12345),
+ (double)(((seed >>> 44) & 0x7FF) + 12345),
+ (double)((seed >>> 55) + 12345));
+ }
+
+ /**
+ * Creates a new MRG32k3a instance that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program; and
+ * may, and typically does, vary across program invocations.
+ */
+ public MRG32k3a() {
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code Xoshiro256StarStar} using the specified array of
+ * initial seed bytes. Instances of {@code Xoshiro256StarStar} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public MRG32k3a(byte[] seed) {
+ // Convert the seed to 6 int values.
+ int[] data = RngSupport.convertSeedBytesToInts(seed, 6, 0);
+ int s10 = data[0], s11 = data[1], s12 = data[2];
+ int s20 = data[3], s21 = data[4], s22 = data[5];
+ this.s10 = ((double)(((long)s10) & 0x00000000ffffffffL)) % m1;
+ this.s11 = ((double)(((long)s11) & 0x00000000ffffffffL)) % m1;
+ this.s12 = ((double)(((long)s12) & 0x00000000ffffffffL)) % m1;
+ this.s20 = ((double)(((long)s20) & 0x00000000ffffffffL)) % m2;
+ this.s21 = ((double)(((long)s21) & 0x00000000ffffffffL)) % m2;
+ this.s22 = ((double)(((long)s22) & 0x00000000ffffffffL)) % m2;
+ if ((s10 == 0.0) && (s11 == 0.0) && (s12 == 0.0)) this.s10 = 12345.0;
+ if ((s20 == 0.0) && (s21 == 0.0) && (s22 == 0.0)) this.s20 = 12345.0;
+ }
+
+ public MRG32k3a copy() { return new MRG32k3a(s10, s11, s12, s20, s21, s22); }
+
+ /**
+ * Returns a pseudorandom {@code double} value between zero
+ * (exclusive) and one (exclusive).
+ *
+ * @return a pseudorandom {@code double} value between zero
+ * (exclusive) and one (exclusive)
+ */
+ public double nextOpenDouble() {
+ nextState();
+ double p1 = s12, p2 = s22;
+ if (p1 <= p2)
+ return ((p1 - p2 + m1) * norm1);
+ else
+ return ((p1 - p2) * norm1);
+ }
+
+ /**
+ * Returns a pseudorandom {@code double} value between zero
+ * (inclusive) and one (exclusive).
+ *
+ * @return a pseudorandom {@code double} value between zero
+ * (inclusive) and one (exclusive)
+ */
+ public double nextDouble() {
+ nextState();
+ double p1 = s12, p2 = s22;
+ final double p = p1 * norm1 - p2 * norm2;
+ if (p < 0.0) return (p + 1.0);
+ else return p;
+ }
+
+
+ /**
+ * Returns a pseudorandom {@code float} value between zero
+ * (inclusive) and one (exclusive).
+ *
+ * @return a pseudorandom {@code float} value between zero
+ * (inclusive) and one (exclusive)
+ */
+ public float nextFloat() {
+ return (float)nextDouble();
+ }
+
+ /**
+ * Returns a pseudorandom {@code int} value.
+ *
+ * @return a pseudorandom {@code int} value
+ */
+ public int nextInt() {
+ return (internalNextInt(0x10000) << 16) | internalNextInt(0x10000);
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ return (((long)internalNextInt(0x200000) << 43) |
+ ((long)internalNextInt(0x200000) << 22) |
+ ((long)internalNextInt(0x400000)));
+ }
+
+ // Period is (m1**3 - 1)(m2**3 - 1)/2, or approximately 2**191.
+ static BigInteger calculateThePeriod() {
+ BigInteger bigm1 = BigInteger.valueOf((long)m1);
+ BigInteger bigm2 = BigInteger.valueOf((long)m2);
+ BigInteger t1 = bigm1.multiply(bigm1).multiply(bigm1).subtract(BigInteger.ONE);
+ BigInteger t2 = bigm2.multiply(bigm2).multiply(bigm2).subtract(BigInteger.ONE);
+ return t1.shiftRight(1).multiply(t2);
+ }
+ static final BigInteger thePeriod = calculateThePeriod();
+ public BigInteger period() { return thePeriod; }
+
+ // Jump and leap distances recommended in Section 1.3 of this paper:
+ // Pierre L'Ecuyer, Richard Simard, E. Jack Chen, and W. David Kelton.
+ // An Object-Oriented Random-Number Package with Many Long Streams and Substreams.
+ // Operations Research 50, 6 (Nov--Dec 2002), 1073--1075.
+
+ public double defaultJumpDistance() { return 0x1.0p76; } // 2**76
+ public double defaultLeapDistance() { return 0x1.0p127; } // 2**127
+
+ public void jump(double distance) {
+ if (distance < 0.0 || Double.isInfinite(distance) || distance != Math.floor(distance))
+ throw new IllegalArgumentException("jump distance must be a nonnegative finite integer");
+ // We will compute a jump transformation (s => M s) for each LCG.
+ // We initialize each transformation to the identity transformation.
+ // Each will be turned into the d'th power of the corresponding base transformation.
+ long m1_00 = 1, m1_01 = 0, m1_02 = 0,
+ m1_10 = 0, m1_11 = 1, m1_12 = 0,
+ m1_20 = 0, m1_21 = 0, m1_22 = 1;
+ long m2_00 = 1, m2_01 = 0, m2_02 = 0,
+ m2_10 = 0, m2_11 = 1, m2_12 = 0,
+ m2_20 = 0, m2_21 = 0, m2_22 = 1;
+ // These are the base transformations, which will be repeatedly squared,
+ // and composed with the computed transformations for each 1-bit in distance.
+ long t1_00 = 0, t1_01 = 1, t1_02 = 0,
+ t1_10 = 0, t1_11 = 0, t1_12 = 1,
+ t1_20 = -(long)a13n, t1_21 = (long)a12, t1_22 = 0;
+ long t2_00 = 0, t2_01 = 1, t2_02 = 0,
+ t2_10 = 0, t2_11 = 0, t2_12 = 1,
+ t2_20 = -(long)a23n, t2_21 = (long)a21, t2_22 = 0;
+ while (distance > 0.0) {
+ final double dhalf = 0.5 * distance;
+ if (Math.floor(dhalf) != dhalf) {
+ // distance is odd: accumulate current squaring
+ final long n1_00 = m1_00 * t1_00 + m1_01 * t1_10 + m1_02 * t1_20;
+ final long n1_01 = m1_00 * t1_01 + m1_01 * t1_11 + m1_02 * t1_21;
+ final long n1_02 = m1_00 * t1_02 + m1_01 * t1_12 + m1_02 * t1_22;
+ final long n1_10 = m1_10 * t1_00 + m1_11 * t1_10 + m1_12 * t1_20;
+ final long n1_11 = m1_10 * t1_01 + m1_11 * t1_11 + m1_12 * t1_21;
+ final long n1_12 = m1_10 * t1_02 + m1_11 * t1_12 + m1_12 * t1_22;
+ final long n1_20 = m1_20 * t1_00 + m1_21 * t1_10 + m1_22 * t1_20;
+ final long n1_21 = m1_20 * t1_01 + m1_21 * t1_11 + m1_22 * t1_21;
+ final long n1_22 = m1_20 * t1_02 + m1_21 * t1_12 + m1_22 * t1_22;
+ m1_00 = Math.floorMod(n1_00, (long)m1);
+ m1_01 = Math.floorMod(n1_01, (long)m1);
+ m1_02 = Math.floorMod(n1_02, (long)m1);
+ m1_10 = Math.floorMod(n1_10, (long)m1);
+ m1_11 = Math.floorMod(n1_11, (long)m1);
+ m1_12 = Math.floorMod(n1_12, (long)m1);
+ m1_20 = Math.floorMod(n1_20, (long)m1);
+ m1_21 = Math.floorMod(n1_21, (long)m1);
+ m1_22 = Math.floorMod(n1_22, (long)m1);
+ final long n2_00 = m2_00 * t2_00 + m2_01 * t2_10 + m2_02 * t2_20;
+ final long n2_01 = m2_00 * t2_01 + m2_01 * t2_11 + m2_02 * t2_21;
+ final long n2_02 = m2_00 * t2_02 + m2_01 * t2_12 + m2_02 * t2_22;
+ final long n2_10 = m2_10 * t2_00 + m2_11 * t2_10 + m2_12 * t2_20;
+ final long n2_11 = m2_10 * t2_01 + m2_11 * t2_11 + m2_12 * t2_21;
+ final long n2_12 = m2_10 * t2_02 + m2_11 * t2_12 + m2_12 * t2_22;
+ final long n2_20 = m2_20 * t2_00 + m2_21 * t2_10 + m2_22 * t2_20;
+ final long n2_21 = m2_20 * t2_01 + m2_21 * t2_11 + m2_22 * t2_21;
+ final long n2_22 = m2_20 * t2_02 + m2_21 * t2_12 + m2_22 * t2_22;
+ m2_00 = Math.floorMod(n2_00, (long)m2);
+ m2_01 = Math.floorMod(n2_01, (long)m2);
+ m2_02 = Math.floorMod(n2_02, (long)m2);
+ m2_10 = Math.floorMod(n2_10, (long)m2);
+ m2_11 = Math.floorMod(n2_11, (long)m2);
+ m2_12 = Math.floorMod(n2_12, (long)m2);
+ m2_20 = Math.floorMod(n2_20, (long)m2);
+ m2_21 = Math.floorMod(n2_21, (long)m2);
+ m2_22 = Math.floorMod(n2_22, (long)m2);
+ }
+ // Square the base transformations.
+ {
+ final long z1_00 = m1_00 * m1_00 + m1_01 * m1_10 + m1_02 * m1_20;
+ final long z1_01 = m1_00 * m1_01 + m1_01 * m1_11 + m1_02 * m1_21;
+ final long z1_02 = m1_00 * m1_02 + m1_01 * m1_12 + m1_02 * m1_22;
+ final long z1_10 = m1_10 * m1_00 + m1_11 * m1_10 + m1_12 * m1_20;
+ final long z1_11 = m1_10 * m1_01 + m1_11 * m1_11 + m1_12 * m1_21;
+ final long z1_12 = m1_10 * m1_02 + m1_11 * m1_12 + m1_12 * m1_22;
+ final long z1_20 = m1_20 * m1_00 + m1_21 * m1_10 + m1_22 * m1_20;
+ final long z1_21 = m1_20 * m1_01 + m1_21 * m1_11 + m1_22 * m1_21;
+ final long z1_22 = m1_20 * m1_02 + m1_21 * m1_12 + m1_22 * m1_22;
+ m1_00 = Math.floorMod(z1_00, (long)m1);
+ m1_01 = Math.floorMod(z1_01, (long)m1);
+ m1_02 = Math.floorMod(z1_02, (long)m1);
+ m1_10 = Math.floorMod(z1_10, (long)m1);
+ m1_11 = Math.floorMod(z1_11, (long)m1);
+ m1_12 = Math.floorMod(z1_12, (long)m1);
+ m1_20 = Math.floorMod(z1_20, (long)m1);
+ m1_21 = Math.floorMod(z1_21, (long)m1);
+ m1_22 = Math.floorMod(z1_22, (long)m1);
+ final long z2_00 = m2_00 * m2_00 + m2_01 * m2_10 + m2_02 * m2_20;
+ final long z2_01 = m2_00 * m2_01 + m2_01 * m2_11 + m2_02 * m2_21;
+ final long z2_02 = m2_00 * m2_02 + m2_01 * m2_12 + m2_02 * m2_22;
+ final long z2_10 = m2_10 * m2_00 + m2_11 * m2_10 + m2_12 * m2_20;
+ final long z2_11 = m2_10 * m2_01 + m2_11 * m2_11 + m2_12 * m2_21;
+ final long z2_12 = m2_10 * m2_02 + m2_11 * m2_12 + m2_12 * m2_22;
+ final long z2_20 = m2_20 * m2_00 + m2_21 * m2_10 + m2_22 * m2_20;
+ final long z2_21 = m2_20 * m2_01 + m2_21 * m2_11 + m2_22 * m2_21;
+ final long z2_22 = m2_20 * m2_02 + m2_21 * m2_12 + m2_22 * m2_22;
+ m2_00 = Math.floorMod(z2_00, (long)m2);
+ m2_01 = Math.floorMod(z2_01, (long)m2);
+ m2_02 = Math.floorMod(z2_02, (long)m2);
+ m2_10 = Math.floorMod(z2_10, (long)m2);
+ m2_11 = Math.floorMod(z2_11, (long)m2);
+ m2_12 = Math.floorMod(z2_12, (long)m2);
+ m2_20 = Math.floorMod(z2_20, (long)m2);
+ m2_21 = Math.floorMod(z2_21, (long)m2);
+ m2_22 = Math.floorMod(z2_22, (long)m2);
+ }
+ // Divide distance by 2.
+ distance = dhalf;
+ }
+ final long w10 = m1_00 * (long)s10 + m1_01 * (long)s11 + m1_02 * (long)s12;
+ final long w11 = m1_10 * (long)s10 + m1_11 * (long)s11 + m1_12 * (long)s12;
+ final long w12 = m1_20 * (long)s10 + m1_21 * (long)s11 + m1_22 * (long)s12;
+ s10 = Math.floorMod(w10, (long)m1);
+ s11 = Math.floorMod(w11, (long)m1);
+ s12 = Math.floorMod(w12, (long)m1);
+ final long w20 = m2_00 * (long)s20 + m2_01 * (long)s21 + m2_02 * (long)s22;
+ final long w21 = m2_10 * (long)s20 + m2_11 * (long)s21 + m2_12 * (long)s22;
+ final long w22 = m2_20 * (long)s20 + m2_21 * (long)s21 + m2_22 * (long)s22;
+ s20 = Math.floorMod(w20, (long)m2);
+ s21 = Math.floorMod(w21, (long)m2);
+ s22 = Math.floorMod(w22, (long)m2);
+ }
+
+ /**
+ * Alter the state of this pseudorandom number generator so as to
+ * jump forward a distance equal to 2<sup>{@code logDistance}</sup>
+ * within its state cycle.
+ *
+ * @param logDistance the base-2 logarithm of the distance to jump
+ * forward within the state cycle. Must be non-negative and
+ * not greater than 192.
+ * @throws IllegalArgumentException if {@code logDistance} is
+ * less than zero or 2<sup>{@code logDistance}</sup> is
+ * greater than the period of this generator
+ */
+ public void jumpPowerOfTwo(int logDistance) {
+ if (logDistance < 0 || logDistance > 192)
+ throw new IllegalArgumentException(BadLogDistance);
+ jump(Math.scalb(1.0, logDistance));
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/Random.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,564 @@
+/*
+ * Copyright (c) 1995, 2013, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.io.*;
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.DoubleConsumer;
+import java.util.function.IntConsumer;
+import java.util.function.LongConsumer;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.StreamSupport;
+
+import sun.misc.Unsafe;
+
+/**
+ * An instance of this class is used to generate a stream of
+ * pseudorandom numbers. The class uses a 48-bit seed, which is
+ * modified using a linear congruential formula. (See Donald Knuth,
+ * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.)
+ * <p>
+ * If two instances of {@code Random} are created with the same
+ * seed, and the same sequence of method calls is made for each, they
+ * will generate and return identical sequences of numbers. In order to
+ * guarantee this property, particular algorithms are specified for the
+ * class {@code Random}. Java implementations must use all the algorithms
+ * shown here for the class {@code Random}, for the sake of absolute
+ * portability of Java code. However, subclasses of class {@code Random}
+ * are permitted to use other algorithms, so long as they adhere to the
+ * general contracts for all the methods.
+ * <p>
+ * The algorithms implemented by class {@code Random} use a
+ * {@code protected} utility method that on each invocation can supply
+ * up to 32 pseudorandomly generated bits.
+ * <p>
+ * Many applications will find the method {@link Math#random} simpler to use.
+ *
+ * <p>Instances of {@code java.util.Random} are threadsafe.
+ * However, the concurrent use of the same {@code java.util.Random}
+ * instance across threads may encounter contention and consequent
+ * poor performance. Consider instead using
+ * {@link java.util.concurrent.ThreadLocalRandom} in multithreaded
+ * designs.
+ *
+ * <p>Instances of {@code java.util.Random} are not cryptographically
+ * secure. Consider instead using {@link java.security.SecureRandom} to
+ * get a cryptographically secure pseudo-random number generator for use
+ * by security-sensitive applications.
+ *
+ * @author Frank Yellin
+ * @since 1.0
+ */
+public
+ class Random extends AbstractSharedRng implements java.io.Serializable {
+ /** use serialVersionUID from JDK 1.1 for interoperability */
+ static final long serialVersionUID = 3905348978240129619L;
+
+ /**
+ * The internal state associated with this pseudorandom number generator.
+ * (The specs for the methods in this class describe the ongoing
+ * computation of this value.)
+ */
+ private final AtomicLong seed;
+
+ private static final long multiplier = 0x5DEECE66DL;
+ private static final long addend = 0xBL;
+ private static final long mask = (1L << 48) - 1;
+
+ private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53)
+
+ // 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";
+
+ /**
+ * Creates a new random number generator. This constructor sets
+ * the seed of the random number generator to a value very likely
+ * to be distinct from any other invocation of this constructor.
+ */
+ public Random() {
+ this(seedUniquifier() ^ System.nanoTime());
+ }
+
+ private static long seedUniquifier() {
+ // L'Ecuyer, "Tables of Linear Congruential Generators of
+ // Different Sizes and Good Lattice Structure", 1999
+ for (;;) {
+ long current = seedUniquifier.get();
+ long next = current * 181783497276652981L;
+ if (seedUniquifier.compareAndSet(current, next))
+ return next;
+ }
+ }
+
+ private static final AtomicLong seedUniquifier
+ = new AtomicLong(8682522807148012L);
+
+ /**
+ * Creates a new random number generator using a single {@code long} seed.
+ * The seed is the initial value of the internal state of the pseudorandom
+ * number generator which is maintained by method {@link #next}.
+ *
+ * <p>The invocation {@code new Random(seed)} is equivalent to:
+ * <pre> {@code
+ * Random rnd = new Random();
+ * rnd.setSeed(seed);}</pre>
+ *
+ * @param seed the initial seed
+ * @see #setSeed(long)
+ */
+ public Random(long seed) {
+ if (getClass() == Random.class)
+ this.seed = new AtomicLong(initialScramble(seed));
+ else {
+ // subclass might have overriden setSeed
+ this.seed = new AtomicLong();
+ setSeed(seed);
+ }
+ }
+
+ private static long initialScramble(long seed) {
+ return (seed ^ multiplier) & mask;
+ }
+
+ /**
+ * Sets the seed of this random number generator using a single
+ * {@code long} seed. The general contract of {@code setSeed} is
+ * that it alters the state of this random number generator object
+ * so as to be in exactly the same state as if it had just been
+ * created with the argument {@code seed} as a seed. The method
+ * {@code setSeed} is implemented by class {@code Random} by
+ * atomically updating the seed to
+ * <pre>{@code (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)}</pre>
+ * and clearing the {@code haveNextNextGaussian} flag used by {@link
+ * #nextGaussian}.
+ *
+ * <p>The implementation of {@code setSeed} by class {@code Random}
+ * happens to use only 48 bits of the given seed. In general, however,
+ * an overriding method may use all 64 bits of the {@code long}
+ * argument as a seed value.
+ *
+ * @param seed the initial seed
+ */
+ synchronized public void setSeed(long seed) {
+ this.seed.set(initialScramble(seed));
+ haveNextNextGaussian = false;
+ }
+
+ /**
+ * Generates the next pseudorandom number. Subclasses should
+ * override this, as this is used by all other methods.
+ *
+ * <p>The general contract of {@code next} is that it returns an
+ * {@code int} value and if the argument {@code bits} is between
+ * {@code 1} and {@code 32} (inclusive), then that many low-order
+ * bits of the returned value will be (approximately) independently
+ * chosen bit values, each of which is (approximately) equally
+ * likely to be {@code 0} or {@code 1}. The method {@code next} is
+ * implemented by class {@code Random} by atomically updating the seed to
+ * <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
+ * and returning
+ * <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
+ *
+ * This is a linear congruential pseudorandom number generator, as
+ * defined by D. H. Lehmer and described by Donald E. Knuth in
+ * <i>The Art of Computer Programming,</i> Volume 3:
+ * <i>Seminumerical Algorithms</i>, section 3.2.1.
+ *
+ * @param bits random bits
+ * @return the next pseudorandom value from this random number
+ * generator's sequence
+ * @since 1.1
+ */
+ protected int next(int bits) {
+ long oldseed, nextseed;
+ AtomicLong seed = this.seed;
+ do {
+ oldseed = seed.get();
+ nextseed = (oldseed * multiplier + addend) & mask;
+ } while (!seed.compareAndSet(oldseed, nextseed));
+ return (int)(nextseed >>> (48 - bits));
+ }
+
+ static final BigInteger thePeriod = BigInteger.valueOf(1L<<48); // Period is 2**48
+
+ /**
+ * Returns the period of this random number generator.
+ *
+ * @return the period of this random number generator.
+ */
+ public BigInteger period() {
+ // Here we also take care of checking for instances of class SecureRandom,
+ // just so as not to bother the implementors of that class.
+ // (Any specific instance of SecureRandom can of course override this method.)
+ // The cast to (Object) is of course needed only during development.
+ return ((Object)this instanceof java.security.SecureRandom) ? Rng.HUGE_PERIOD : thePeriod;
+ }
+
+ /**
+ * Generates random bytes and places them into a user-supplied
+ * byte array. The number of random bytes produced is equal to
+ * the length of the byte array.
+ *
+ * <p>The method {@code nextBytes} is implemented by class {@code Random}
+ * as if by:
+ * <pre> {@code
+ * public void nextBytes(byte[] bytes) {
+ * for (int i = 0; i < bytes.length; )
+ * for (int rnd = nextInt(), n = Math.min(bytes.length - i, 4);
+ * n-- > 0; rnd >>= 8)
+ * bytes[i++] = (byte)rnd;
+ * }}</pre>
+ *
+ * @param bytes the byte array to fill with random bytes
+ * @throws NullPointerException if the byte array is null
+ * @since 1.1
+ */
+ public void nextBytes(byte[] bytes) {
+ for (int i = 0, len = bytes.length; i < len; )
+ for (int rnd = nextInt(),
+ n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
+ n-- > 0; rnd >>= Byte.SIZE)
+ bytes[i++] = (byte)rnd;
+ }
+
+ /**
+ * Returns the next pseudorandom, uniformly distributed {@code int}
+ * value from this random number generator's sequence. The general
+ * contract of {@code nextInt} is that one {@code int} value is
+ * pseudorandomly generated and returned. All 2<sup>32</sup> possible
+ * {@code int} values are produced with (approximately) equal probability.
+ *
+ * <p>The method {@code nextInt} is implemented by class {@code Random}
+ * as if by:
+ * <pre> {@code
+ * public int nextInt() {
+ * return next(32);
+ * }}</pre>
+ *
+ * @return the next pseudorandom, uniformly distributed {@code int}
+ * value from this random number generator's sequence
+ */
+ public int nextInt() {
+ return next(32);
+ }
+
+ /**
+ * Returns a pseudorandom {@code int} value between zero (inclusive)
+ * and the specified bound (exclusive).
+ *
+ * @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);
+ // Specialize internalNextInt for origin 0
+ int r = nextInt();
+ int m = bound - 1;
+ if ((bound & m) == 0) // power of two
+ r &= m;
+ else { // reject over-represented candidates
+ for (int u = r >>> 1;
+ u + m - (r = u % bound) < 0;
+ u = nextInt() >>> 1)
+ ;
+ }
+ return r;
+ }
+
+ /**
+ * Returns the next pseudorandom, uniformly distributed {@code long}
+ * value from this random number generator's sequence. The general
+ * contract of {@code nextLong} is that one {@code long} value is
+ * pseudorandomly generated and returned.
+ *
+ * <p>The method {@code nextLong} is implemented by class {@code Random}
+ * as if by:
+ * <pre> {@code
+ * public long nextLong() {
+ * return ((long)next(32) << 32) + next(32);
+ * }}</pre>
+ *
+ * Because class {@code Random} uses a seed with only 48 bits,
+ * this algorithm will not return all possible {@code long} values.
+ *
+ * @return the next pseudorandom, uniformly distributed {@code long}
+ * value from this random number generator's sequence
+ */
+ public long nextLong() {
+ // it's okay that the bottom word remains signed.
+ return ((long)(next(32)) << 32) + next(32);
+ }
+
+ /**
+ * Returns the next pseudorandom, uniformly distributed
+ * {@code boolean} value from this random number generator's
+ * sequence. The general contract of {@code nextBoolean} is that one
+ * {@code boolean} value is pseudorandomly generated and returned. The
+ * values {@code true} and {@code false} are produced with
+ * (approximately) equal probability.
+ *
+ * <p>The method {@code nextBoolean} is implemented by class {@code Random}
+ * as if by:
+ * <pre> {@code
+ * public boolean nextBoolean() {
+ * return next(1) != 0;
+ * }}</pre>
+ *
+ * @return the next pseudorandom, uniformly distributed
+ * {@code boolean} value from this random number generator's
+ * sequence
+ * @since 1.2
+ */
+ public boolean nextBoolean() {
+ return next(1) != 0;
+ }
+
+ /**
+ * Returns the next pseudorandom, uniformly distributed {@code float}
+ * value between {@code 0.0} and {@code 1.0} from this random
+ * number generator's sequence.
+ *
+ * <p>The general contract of {@code nextFloat} is that one
+ * {@code float} value, chosen (approximately) uniformly from the
+ * range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is
+ * pseudorandomly generated and returned. All 2<sup>24</sup> possible
+ * {@code float} values of the form <i>m x </i>2<sup>-24</sup>,
+ * where <i>m</i> is a positive integer less than 2<sup>24</sup>, are
+ * produced with (approximately) equal probability.
+ *
+ * <p>The method {@code nextFloat} is implemented by class {@code Random}
+ * as if by:
+ * <pre> {@code
+ * public float nextFloat() {
+ * return next(24) / ((float)(1 << 24));
+ * }}</pre>
+ *
+ * <p>The hedge "approximately" is used in the foregoing description only
+ * because the next method is only approximately an unbiased source of
+ * independently chosen bits. If it were a perfect source of randomly
+ * chosen bits, then the algorithm shown would choose {@code float}
+ * values from the stated range with perfect uniformity.<p>
+ * [In early versions of Java, the result was incorrectly calculated as:
+ * <pre> {@code
+ * return next(30) / ((float)(1 << 30));}</pre>
+ * This might seem to be equivalent, if not better, but in fact it
+ * introduced a slight nonuniformity because of the bias in the rounding
+ * of floating-point numbers: it was slightly more likely that the
+ * low-order bit of the significand would be 0 than that it would be 1.]
+ *
+ * @return the next pseudorandom, uniformly distributed {@code float}
+ * value between {@code 0.0} and {@code 1.0} from this
+ * random number generator's sequence
+ */
+ public float nextFloat() {
+ return next(24) / ((float)(1 << 24));
+ }
+
+ /**
+ * Returns the next pseudorandom, uniformly distributed
+ * {@code double} value between {@code 0.0} and
+ * {@code 1.0} from this random number generator's sequence.
+ *
+ * <p>The general contract of {@code nextDouble} is that one
+ * {@code double} value, chosen (approximately) uniformly from the
+ * range {@code 0.0d} (inclusive) to {@code 1.0d} (exclusive), is
+ * pseudorandomly generated and returned.
+ *
+ * <p>The method {@code nextDouble} is implemented by class {@code Random}
+ * as if by:
+ * <pre> {@code
+ * public double nextDouble() {
+ * return (((long)next(26) << 27) + next(27))
+ * / (double)(1L << 53);
+ * }}</pre>
+ *
+ * <p>The hedge "approximately" is used in the foregoing description only
+ * because the {@code next} method is only approximately an unbiased
+ * source of independently chosen bits. If it were a perfect source of
+ * randomly chosen bits, then the algorithm shown would choose
+ * {@code double} values from the stated range with perfect uniformity.
+ * <p>[In early versions of Java, the result was incorrectly calculated as:
+ * <pre> {@code
+ * return (((long)next(27) << 27) + next(27))
+ * / (double)(1L << 54);}</pre>
+ * This might seem to be equivalent, if not better, but in fact it
+ * introduced a large nonuniformity because of the bias in the rounding
+ * of floating-point numbers: it was three times as likely that the
+ * low-order bit of the significand would be 0 than that it would be 1!
+ * This nonuniformity probably doesn't matter much in practice, but we
+ * strive for perfection.]
+ *
+ * @return the next pseudorandom, uniformly distributed {@code double}
+ * value between {@code 0.0} and {@code 1.0} from this
+ * random number generator's sequence
+ * @see Math#random
+ */
+ public double nextDouble() {
+ return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
+ }
+
+ private double nextNextGaussian;
+ private boolean haveNextNextGaussian = false;
+
+ /**
+ * Returns the next pseudorandom, Gaussian ("normally") distributed
+ * {@code double} value with mean {@code 0.0} and standard
+ * deviation {@code 1.0} from this random number generator's sequence.
+ * <p>
+ * The general contract of {@code nextGaussian} is that one
+ * {@code double} value, chosen from (approximately) the usual
+ * normal distribution with mean {@code 0.0} and standard deviation
+ * {@code 1.0}, is pseudorandomly generated and returned.
+ *
+ * <p>The method {@code nextGaussian} is implemented by class
+ * {@code Random} as if by a threadsafe version of the following:
+ * <pre> {@code
+ * private double nextNextGaussian;
+ * private boolean haveNextNextGaussian = false;
+ *
+ * public double nextGaussian() {
+ * if (haveNextNextGaussian) {
+ * haveNextNextGaussian = false;
+ * return nextNextGaussian;
+ * } else {
+ * double v1, v2, s;
+ * do {
+ * v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0
+ * v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0
+ * s = v1 * v1 + v2 * v2;
+ * } while (s >= 1 || s == 0);
+ * double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
+ * nextNextGaussian = v2 * multiplier;
+ * haveNextNextGaussian = true;
+ * return v1 * multiplier;
+ * }
+ * }}</pre>
+ * This uses the <i>polar method</i> of G. E. P. Box, M. E. Muller, and
+ * G. Marsaglia, as described by Donald E. Knuth in <i>The Art of
+ * Computer Programming</i>, Volume 3: <i>Seminumerical Algorithms</i>,
+ * section 3.4.1, subsection C, algorithm P. Note that it generates two
+ * independent values at the cost of only one call to {@code StrictMath.log}
+ * and one call to {@code StrictMath.sqrt}.
+ *
+ * @return the next pseudorandom, Gaussian ("normally") distributed
+ * {@code double} value with mean {@code 0.0} and
+ * standard deviation {@code 1.0} from this random number
+ * generator's sequence
+ */
+ synchronized public double nextGaussian() {
+ // See Knuth, ACP, Section 3.4.1 Algorithm C.
+ if (haveNextNextGaussian) {
+ haveNextNextGaussian = false;
+ return nextNextGaussian;
+ } else {
+ double v1, v2, s;
+ do {
+ v1 = 2 * nextDouble() - 1; // between -1 and 1
+ v2 = 2 * nextDouble() - 1; // between -1 and 1
+ s = v1 * v1 + v2 * v2;
+ } while (s >= 1 || s == 0);
+ double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
+ nextNextGaussian = v2 * multiplier;
+ haveNextNextGaussian = true;
+ return v1 * multiplier;
+ }
+ }
+
+ /**
+ * Serializable fields for Random.
+ *
+ * @serialField seed long
+ * seed for random computations
+ * @serialField nextNextGaussian double
+ * next Gaussian to be returned
+ * @serialField haveNextNextGaussian boolean
+ * nextNextGaussian is valid
+ */
+ private static final ObjectStreamField[] serialPersistentFields = {
+ new ObjectStreamField("seed", Long.TYPE),
+ new ObjectStreamField("nextNextGaussian", Double.TYPE),
+ new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
+ };
+
+ /**
+ * Reconstitute the {@code Random} instance from a stream (that is,
+ * deserialize it).
+ */
+ private void readObject(java.io.ObjectInputStream s)
+ throws java.io.IOException, ClassNotFoundException {
+
+ ObjectInputStream.GetField fields = s.readFields();
+
+ // The seed is read in as {@code long} for
+ // historical reasons, but it is converted to an AtomicLong.
+ long seedVal = fields.get("seed", -1L);
+ if (seedVal < 0)
+ throw new java.io.StreamCorruptedException(
+ "Random: invalid seed");
+ resetSeed(seedVal);
+ nextNextGaussian = fields.get("nextNextGaussian", 0.0);
+ haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
+ }
+
+ /**
+ * Save the {@code Random} instance to a stream.
+ */
+ synchronized private void writeObject(ObjectOutputStream s)
+ throws IOException {
+
+ // set the values of the Serializable fields
+ ObjectOutputStream.PutField fields = s.putFields();
+
+ // The seed is serialized as a long for historical reasons.
+ fields.put("seed", seed.get());
+ fields.put("nextNextGaussian", nextNextGaussian);
+ fields.put("haveNextNextGaussian", haveNextNextGaussian);
+
+ // save them
+ s.writeFields();
+ }
+
+ // Support for resetting seed while deserializing
+ private static final Unsafe unsafe = Unsafe.getUnsafe();
+ private static final long seedOffset;
+ static {
+ try {
+ seedOffset = unsafe.objectFieldOffset
+ (Random.class.getDeclaredField("seed"));
+ } catch (Exception ex) { throw new Error(ex); }
+ }
+ private void resetSeed(long seedVal) {
+ unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/Rng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,635 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+
+/**
+ * The {@code Rng} interface is designed to provide a common protocol
+ * for objects that generate random or (more typically) pseudorandom
+ * sequences of numbers (or Boolean values). Such a sequence may be
+ * obtained by either repeatedly invoking a method that returns a
+ * single (pseudo)randomly chosen value, or by invoking a method that
+ * returns a stream of (pseudo)randomly chosen values.
+ *
+ * <p>Ideally, given an implicitly or explicitly specified range of values,
+ * each value would be chosen independently and uniformly from that range.
+ * In practice, one may have to settle for some approximation to independence
+ * and uniformity.
+ *
+ * <p>In the case of {@code int}, {@code long}, and {@code Boolean}
+ * values, if there is no explicit specification of range, then the
+ * range includes all possible values of the type. In the case of
+ * {@code float} and {@code double} values, a value is always chosen
+ * from the set of 2<sup><it>w</it></sup> values between 0.0 (inclusive)
+ * and 1.0 (exclusive), where <it>w</it> is 23 for {@code float}
+ * values and 52 for {@code double} values, such that adjacent values
+ * differ by 2<sup>−<it>w</it></sup>; if an explicit range is
+ * specified, then the chosen number is computationally scaled and
+ * translated so as to appear to have been chosen from that range.
+ *
+ * <p>Each method that returns a stream produces a stream of values each of
+ * which is chosen in the same manner as for a method that
+ * returns a single (pseudo)randomly chosen value. For example, if {@code r}
+ * implements {@code Rng}, then the method call {@code r.ints(100)} returns
+ * a stream of 100 {@code int} values. These are not necessarily the exact
+ * same values that would have been returned if instead {@code r.nextInt()}
+ * had been called 100 times; all that is guaranteed is that each value in
+ * the stream is chosen in a similar (pseudo)random manner from the same range.
+ *
+ * <p>Every object that implements the {@code Rng} interface is assumed
+ * to contain a finite amount of state. Using such an object to
+ * generate a pseudorandomly chosen value alters its state. The
+ * number of distinct possible states of such an object is called its
+ * <it>period</it>. (Some implementations of the {@code Rng} interface
+ * may be truly random rather than pseudorandom, for example relying
+ * on the statistical behavior of a physical object to derive chosen
+ * values. Such implementations do not have a fixed period.)
+ *
+ * <p>As a rule, objects that implement the {@code Rng} interface need not
+ * be thread-safe. It is recommended that multithreaded applications
+ * use either {@code ThreadLocalRandom} or (preferably) pseudorandom
+ * number generators that implement the {@code SplittableRng} or
+ * {@code JumpableRng} interface.
+
+ * To implement this interface, a class only needs to provide concrete
+ * definitions for the methods {@code nextLong()} and {@code period()}.
+ * Default implementations are provided for all other methods
+ * (but it may be desirable to override some of them, especially
+ * {@code nextInt()} if the underlying algorithm is {@code int}-based).
+ * Moerover, it may be preferable instead to implement another interface
+ * such as {@link java.util.JumpableRng} or {@link java.util.LeapableRng},
+ * or to extend an abstract class such as {@link java.util.AbstractSplittableRng}
+ * or {@link java.util.AbstractArbitrarilyJumpableRng}.
+ *
+ * <p>Objects that implement {@code java.util.Rng} are typically
+ * not cryptographically secure. Consider instead using
+ * {@link java.security.SecureRandom} to get a cryptographically
+ * secure pseudorandom number generator for use by
+ * security-sensitive applications. Note, however, that
+ * {@code java.security.SecureRandom} does implement the {@code Rng}
+ * interface, so that instances of {@code java.security.SecureRandom}
+ * may be used interchangeably with other types of pseudorandom
+ * generators in applications that do not require a secure generator.
+ *
+ * @author Guy Steele
+ * @since 1.9
+ */
+
+interface Rng {
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandomly chosen
+ * {@code double} values.
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to {@code doubles(Long.MAX_VALUE)}.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextDouble()}.
+ *
+ * @return a stream of pseudorandomly chosen {@code double} values
+ */
+
+ default DoubleStream doubles() {
+ return DoubleStream.generate(this::nextDouble).sequential();
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandomly chosen
+ * {@code double} values, where each value is between the specified
+ * origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to
+ * {@code doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextDouble(randomNumberOrigin, randomNumberBound)}.
+ *
+ * @param randomNumberOrigin the least value that can be produced
+ * @param randomNumberBound the upper bound (exclusive) for each value produced
+ * @return a stream of pseudorandomly chosen {@code double} values, each between
+ * the specified origin (inclusive) and the specified bound (exclusive)
+ * @throws IllegalArgumentException if {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ default DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return DoubleStream.generate(() -> nextDouble(randomNumberOrigin, randomNumberBound)).sequential();
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * pseudorandomly chosen {@code double} values.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextDouble()}.
+ *
+ * @param streamSize the number of values to generate
+ * @return a stream of pseudorandomly chosen {@code double} values
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ default DoubleStream doubles(long streamSize) {
+ RngSupport.checkStreamSize(streamSize);
+ return doubles().limit(streamSize);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * pseudorandomly chosen {@code double} values, where each value is between
+ * the specified origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextDouble(randomNumberOrigin, randomNumberBound)}.
+ *
+ * @param streamSize the number of values to generate
+ * @param randomNumberOrigin the least value that can be produced
+ * @param randomNumberBound the upper bound (exclusive) for each value produced
+ * @return a stream of pseudorandomly chosen {@code double} values, each between
+ * the specified origin (inclusive) and the specified bound (exclusive)
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero, or {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ default DoubleStream doubles(long streamSize, double randomNumberOrigin,
+ double randomNumberBound) {
+ RngSupport.checkStreamSize(streamSize);
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return doubles(randomNumberOrigin, randomNumberBound).limit(streamSize);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandomly chosen
+ * {@code int} values.
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to {@code ints(Long.MAX_VALUE)}.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextInt()}.
+ *
+ * @return a stream of pseudorandomly chosen {@code int} values
+ */
+
+ default IntStream ints() {
+ return IntStream.generate(this::nextInt).sequential();
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandomly chosen
+ * {@code int} values, where each value is between the specified
+ * origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to
+ * {@code ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextInt(randomNumberOrigin, randomNumberBound)}.
+ *
+ * @param randomNumberOrigin the least value that can be produced
+ * @param randomNumberBound the upper bound (exclusive) for each value produced
+ * @return a stream of pseudorandomly chosen {@code int} values, each between
+ * the specified origin (inclusive) and the specified bound (exclusive)
+ * @throws IllegalArgumentException if {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ default IntStream ints(int randomNumberOrigin, int randomNumberBound) {
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return IntStream.generate(() -> nextInt(randomNumberOrigin, randomNumberBound)).sequential();
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * pseudorandomly chosen {@code int} values.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextInt()}.
+ *
+ * @param streamSize the number of values to generate
+ * @return a stream of pseudorandomly chosen {@code int} values
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ default IntStream ints(long streamSize) {
+ RngSupport.checkStreamSize(streamSize);
+ return ints().limit(streamSize);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * pseudorandomly chosen {@code int} values, where each value is between
+ * the specified origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextInt(randomNumberOrigin, randomNumberBound)}.
+ *
+ * @param streamSize the number of values to generate
+ * @param randomNumberOrigin the least value that can be produced
+ * @param randomNumberBound the upper bound (exclusive) for each value produced
+ * @return a stream of pseudorandomly chosen {@code int} values, each between
+ * the specified origin (inclusive) and the specified bound (exclusive)
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero, or {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ default IntStream ints(long streamSize, int randomNumberOrigin,
+ int randomNumberBound) {
+ RngSupport.checkStreamSize(streamSize);
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return ints(randomNumberOrigin, randomNumberBound).limit(streamSize);
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandomly chosen
+ * {@code long} values.
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to {@code longs(Long.MAX_VALUE)}.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextLong()}.
+ *
+ * @return a stream of pseudorandomly chosen {@code long} values
+ */
+
+ default LongStream longs() {
+ return LongStream.generate(this::nextLong).sequential();
+ }
+
+ /**
+ * Returns an effectively unlimited stream of pseudorandomly chosen
+ * {@code long} values, where each value is between the specified
+ * origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to
+ * {@code longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextLong(randomNumberOrigin, randomNumberBound)}.
+ *
+ * @param randomNumberOrigin the least value that can be produced
+ * @param randomNumberBound the upper bound (exclusive) for each value produced
+ * @return a stream of pseudorandomly chosen {@code long} values, each between
+ * the specified origin (inclusive) and the specified bound (exclusive)
+ * @throws IllegalArgumentException if {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ default LongStream longs(long randomNumberOrigin, long randomNumberBound) {
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return LongStream.generate(() -> nextLong(randomNumberOrigin, randomNumberBound)).sequential();
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * pseudorandomly chosen {@code long} values.
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextLong()}.
+ *
+ * @param streamSize the number of values to generate
+ * @return a stream of pseudorandomly chosen {@code long} values
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ default LongStream longs(long streamSize) {
+ RngSupport.checkStreamSize(streamSize);
+ return longs().limit(streamSize);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * pseudorandomly chosen {@code long} values, where each value is between
+ * the specified origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation produces a sequential stream
+ * that repeatedly calls {@code nextLong(randomNumberOrigin, randomNumberBound)}.
+ *
+ * @param streamSize the number of values to generate
+ * @param randomNumberOrigin the least value that can be produced
+ * @param randomNumberBound the upper bound (exclusive) for each value produced
+ * @return a stream of pseudorandomly chosen {@code long} values, each between
+ * the specified origin (inclusive) and the specified bound (exclusive)
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero, or {@code randomNumberOrigin}
+ * is greater than or equal to {@code randomNumberBound}
+ */
+ default LongStream longs(long streamSize, long randomNumberOrigin,
+ long randomNumberBound) {
+ RngSupport.checkStreamSize(streamSize);
+ RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
+ return longs(randomNumberOrigin, randomNumberBound).limit(streamSize);
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code boolean} value.
+ *
+ * <p>The default implementation tests the high-order bit (sign bit)
+ * of a value produced by {@code nextInt()}, on the grounds
+ * that some algorithms for pseudorandom number generation
+ * produce values whose high-order bits have better
+ * statistical quality than the low-order bits.
+ *
+ * @return a pseudorandomly chosen {@code boolean} value
+ */
+ default boolean nextBoolean() {
+ return nextInt() < 0;
+ }
+
+ /**
+ * Returns a pseudorandom {@code float} value between zero
+ * (inclusive) and one (exclusive).
+ *
+ * The default implementation uses the 24 high-order bits
+ * from a call to {@code nextInt()}.
+ *
+ * @return a pseudorandom {@code float} value between zero
+ * (inclusive) and one (exclusive)
+ */
+ default float nextFloat() {
+ return (nextInt() >>> 8) * 0x1.0p-24f;
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code float} value between zero
+ * (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation simply calls
+ * {@code RngSupport.checkBound(bound)} and then
+ * {@code RngSupport.boundedNextFloat(this, bound)}.
+ *
+ * @param bound the upper bound (exclusive) for the returned value.
+ * Must be positive and finite
+ * @return a pseudorandomly chosen {@code float} value between
+ * zero (inclusive) and the bound (exclusive)
+ * @throws IllegalArgumentException if {@code bound} is not
+ * positive and finite
+ */
+ default float nextFloat(float bound) {
+ RngSupport.checkBound(bound);
+ return RngSupport.boundedNextFloat(this, bound);
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code float} value between the
+ * specified origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation simply calls
+ * {@code RngSupport.checkRange(origin, bound)} and then
+ * {@code RngSupport.boundedNextFloat(this, origin, bound)}.
+ *
+ * @param origin the least value that can be returned
+ * @param bound the upper bound (exclusive)
+ * @return a pseudorandomly chosen {@code float} value between the
+ * origin (inclusive) and the bound (exclusive)
+ * @throws IllegalArgumentException unless {@code origin} is finite,
+ * {@code bound} is finite, and {@code origin} is less than
+ * {@code bound}
+ */
+ default float nextFloat(float origin, float bound) {
+ RngSupport.checkRange(origin, bound);
+ return RngSupport.boundedNextFloat(this, origin, bound);
+ }
+
+ /**
+ * Returns a pseudorandom {@code double} value between zero
+ * (inclusive) and one (exclusive).
+ *
+ * The default implementation uses the 53 high-order bits
+ * from a call to {@code nextLong()}.
+ *
+ * @return a pseudorandom {@code double} value between zero
+ * (inclusive) and one (exclusive)
+ */
+ default double nextDouble() {
+ return (nextLong() >>> 11) * 0x1.0p-53;
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code double} value between zero
+ * (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation simply calls
+ * {@code RngSupport.checkBound(bound)} and then
+ * {@code RngSupport.boundedNextDouble(this, bound)}.
+ *
+ * @param bound the upper bound (exclusive) for the returned value.
+ * Must be positive and finite
+ * @return a pseudorandomly chosen {@code double} value between
+ * zero (inclusive) and the bound (exclusive)
+ * @throws IllegalArgumentException if {@code bound} is not
+ * positive and finite
+ */
+ default double nextDouble(double bound) {
+ RngSupport.checkBound(bound);
+ return RngSupport.boundedNextDouble(this, bound);
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code double} value between the
+ * specified origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation simply calls
+ * {@code RngSupport.checkRange(origin, bound)} and then
+ * {@code RngSupport.boundedNextDouble(this, origin, bound)}.
+ *
+ * @param origin the least value that can be returned
+ * @param bound the upper bound (exclusive) for the returned value
+ * @return a pseudorandomly chosen {@code double} value between the
+ * origin (inclusive) and the bound (exclusive)
+ * @throws IllegalArgumentException unless {@code origin} is finite,
+ * {@code bound} is finite, and {@code origin} is less than
+ * {@code bound}
+ */
+ default double nextDouble(double origin, double bound) {
+ RngSupport.checkRange(origin, bound);
+ return RngSupport.boundedNextDouble(this, origin, bound);
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code int} value.
+ *
+ * The default implementation uses the 32 high-order bits
+ * from a call to {@code nextLong()}.
+ *
+ * @return a pseudorandomly chosen {@code int} value
+ */
+ default public int nextInt() {
+ return (int)(nextLong() >>> 32);
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code int} value between
+ * zero (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation simply calls
+ * {@code RngSupport.checkBound(bound)} and then
+ * {@code RngSupport.boundedNextInt(this, bound)}.
+ *
+ * @param bound the upper bound (exclusive) for the returned value. Must be positive.
+ * @return a pseudorandomly chosen {@code int} value between
+ * zero (inclusive) and the bound (exclusive)
+ * @throws IllegalArgumentException if {@code bound} is not positive
+ */
+ default int nextInt(int bound) {
+ RngSupport.checkBound(bound);
+ return RngSupport.boundedNextInt(this, bound);
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code int} value between the
+ * specified origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation simply calls
+ * {@code RngSupport.checkRange(origin, bound)} and then
+ * {@code RngSupport.boundedNextInt(this, origin, bound)}.
+ *
+ * @param origin the least value that can be returned
+ * @param bound the upper bound (exclusive) for the returned value
+ * @return a pseudorandomly chosen {@code int} value between the
+ * origin (inclusive) and the bound (exclusive)
+ * @throws IllegalArgumentException if {@code origin} is greater than
+ * or equal to {@code bound}
+ */
+ default int nextInt(int origin, int bound) {
+ RngSupport.checkRange(origin, bound);
+ return RngSupport.boundedNextInt(this, origin, bound);
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code long} value.
+ *
+ * @return a pseudorandomly chosen {@code long} value
+ */
+ long nextLong();
+
+ /**
+ * Returns a pseudorandomly chosen {@code long} value between
+ * zero (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation simply calls
+ * {@code RngSupport.checkBound(bound)} and then
+ * {@code RngSupport.boundedNextLong(this, bound)}.
+ *
+ * @param bound the upper bound (exclusive) for the returned value. Must be positive.
+ * @return a pseudorandomly chosen {@code long} value between
+ * zero (inclusive) and the bound (exclusive)
+ * @throws IllegalArgumentException if {@code bound} is not positive
+ */
+ default long nextLong(long bound) {
+ RngSupport.checkBound(bound);
+ return RngSupport.boundedNextLong(this, bound);
+ }
+
+ /**
+ * Returns a pseudorandomly chosen {@code long} value between the
+ * specified origin (inclusive) and the specified bound (exclusive).
+ *
+ * @implNote The default implementation simply calls
+ * {@code RngSupport.checkRange(origin, bound)} and then
+ * {@code RngSupport.boundedNextInt(this, origin, bound)}.
+ *
+ * @param origin the least value that can be returned
+ * @param bound the upper bound (exclusive) for the returned value
+ * @return a pseudorandomly chosen {@code long} value between the
+ * origin (inclusive) and the bound (exclusive)
+ * @throws IllegalArgumentException if {@code origin} is greater than
+ * or equal to {@code bound}
+ */
+ default long nextLong(long origin, long bound) {
+ RngSupport.checkRange(origin, bound);
+ return RngSupport.boundedNextLong(this, origin, bound);
+ }
+
+ /**
+ * Returns a {@code double} value pseudorandomly chosen from
+ * a Gaussian (normal) distribution whose mean is 0 and whose
+ * standard deviation is 1.
+ *
+ * @return a {@code double} value pseudorandomly chosen from a
+ * Gaussian distribution
+ */
+ default double nextGaussian() {
+ return RngSupport.computeNextGaussian(this);
+ }
+
+ /**
+ * Returns a {@code double} value pseudorandomly chosen from
+ * a Gaussian (normal) distribution with a mean and
+ * standard deviation specified by the arguments.
+ *
+ * @param mean the mean of the Gaussian distribution to be drawn from
+ * @param stddev the standard deviation of the Gaussian distribution to be drawn from
+ * @return a {@code double} value pseudorandomly chosen from the
+ * specified Gaussian distribution
+ */
+ default double nextGaussian(double mean, double stddev) {
+ return mean + RngSupport.computeNextGaussian(this) * stddev * stddev;
+ }
+
+ /**
+ * Returns a nonnegative {@code double} value pseudorandomly chosen
+ * from an exponential distribution whose mean is 1.
+ *
+ * @return a nonnegative {@code double} value pseudorandomly chosen from an
+ * exponential distribution
+ */
+ default double nextExponential() {
+ return RngSupport.computeNextExponential(this);
+ }
+
+
+ /**
+ * Returns the period of this {@code Rng} object.
+ *
+ * @return a {@code BigInteger} whose value is the number of
+ * distinct possible states of this {@code Rng} object,
+ * or 0 if unknown, or negative if extremely large.
+ */
+ BigInteger period();
+
+ /**
+ * The value (0) returned by the {@code period()} method if the period is unknown.
+ */
+ static final BigInteger UNKNOWN_PERIOD = BigInteger.ZERO;
+
+ /**
+ * The (negative) value returned by the {@code period()} method if this generator
+ * has no period because it is truly random rather than just pseudorandom.
+ */
+ static final BigInteger TRULY_RANDOM = BigInteger.valueOf(-1);
+
+ /**
+ * The (negative) value that may be returned by the {@code period()} method
+ * if this generator has a huge period (larger than 2**(2**16)).
+ */
+ static final BigInteger HUGE_PERIOD = BigInteger.valueOf(-2);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/RngSupport.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,1018 @@
+/*
+ * Copyright (c) 2013, 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.util.Spliterator;
+import java.util.function.Consumer;
+import java.util.function.IntConsumer;
+import java.util.function.LongConsumer;
+import java.util.function.DoubleConsumer;
+import java.util.stream.StreamSupport;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.DoubleStream;
+// import java.util.DoubleZigguratTables;
+
+/**
+ * Low-level utility methods helpful for implementing pseudorandom number generators.
+ *
+ * This class is mostly for library writers creating specific implementations of the interface {@link java.util.Rng}.
+ *
+ * @author Guy Steele
+ * @author Doug Lea
+ * @since 1.9
+ */
+public class RngSupport {
+
+ /*
+ * Implementation Overview.
+ *
+ * This class provides utility methods and constants frequently
+ * useful in the implentation of pseudorandom number generators
+ * that satisfy the interface {@code java.util.Rng}.
+ *
+ * File organization: First some message strings, then the main
+ * public methods, followed by a non-public base spliterator class.
+ */
+
+ // IllegalArgumentException messages
+ static final String BadSize = "size must be non-negative";
+ static final String BadDistance = "jump distance must be finite, positive, and an exact integer";
+ static final String BadBound = "bound must be positive";
+ static final String BadFloatingBound = "bound must be finite and positive";
+ static final String BadRange = "bound must be greater than origin";
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Check a {@code long} proposed stream size for validity.
+ *
+ * @param streamSize the proposed stream size
+ * @throws IllegalArgumentException if {@code streamSize} is negative
+ */
+ public static void checkStreamSize(long streamSize) {
+ if (streamSize < 0L)
+ throw new IllegalArgumentException(BadSize);
+ }
+
+ /**
+ * Check a {@code double} proposed jump distance for validity.
+ *
+ * @param distance the proposed jump distance
+ * @throws IllegalArgumentException if {@code size} not positive,
+ * finite, and an exact integer
+ */
+ public static void checkJumpDistance(double distance) {
+ if (!(distance > 0.0 && distance < Float.POSITIVE_INFINITY && distance == Math.floor(distance)))
+ throw new IllegalArgumentException(BadDistance);
+ }
+
+ /**
+ * Checks a {@code float} upper bound value for validity.
+ *
+ * @param bound the upper bound (exclusive)
+ * @throws IllegalArgumentException if {@code bound} is not
+ * positive and finite
+ */
+ public static void checkBound(float bound) {
+ if (!(bound > 0.0 && bound < Float.POSITIVE_INFINITY))
+ throw new IllegalArgumentException(BadFloatingBound);
+ }
+
+ /**
+ * Checks a {@code double} upper bound value for validity.
+ *
+ * @param bound the upper bound (exclusive)
+ * @throws IllegalArgumentException if {@code bound} is not
+ * positive and finite
+ */
+ public static void checkBound(double bound) {
+ if (!(bound > 0.0 && bound < Double.POSITIVE_INFINITY))
+ throw new IllegalArgumentException(BadFloatingBound);
+ }
+
+ /**
+ * Checks an {@code int} upper bound value for validity.
+ *
+ * @param bound the upper bound (exclusive)
+ * @throws IllegalArgumentException if {@code bound} is not positive
+ */
+ public static void checkBound(int bound) {
+ if (bound <= 0)
+ throw new IllegalArgumentException(BadBound);
+ }
+
+ /**
+ * Checks a {@code long} upper bound value for validity.
+ *
+ * @param bound the upper bound (exclusive)
+ * @throws IllegalArgumentException if {@code bound} is not positive
+ */
+ public static void checkBound(long bound) {
+ if (bound <= 0)
+ throw new IllegalArgumentException(BadBound);
+ }
+
+ /**
+ * Checks a {@code float} range for validity.
+ *
+ * @param origin the least value (inclusive) in the range
+ * @param bound the upper bound (exclusive) of the range
+ * @throws IllegalArgumentException unless {@code origin} is finite,
+ * {@code bound} is finite, and {@code bound - origin} is finite
+ */
+ public static void checkRange(float origin, float bound) {
+ if (!(origin < bound && (bound - origin) < Float.POSITIVE_INFINITY))
+ throw new IllegalArgumentException(BadRange);
+ }
+
+ /**
+ * Checks a {@code double} range for validity.
+ *
+ * @param origin the least value (inclusive) in the range
+ * @param bound the upper bound (exclusive) of the range
+ * @throws IllegalArgumentException unless {@code origin} is finite,
+ * {@code bound} is finite, and {@code bound - origin} is finite
+ */
+ public static void checkRange(double origin, double bound) {
+ if (!(origin < bound && (bound - origin) < Double.POSITIVE_INFINITY))
+ throw new IllegalArgumentException(BadRange);
+ }
+
+ /**
+ * Checks an {@code int} range for validity.
+ *
+ * @param origin the least value that can be returned
+ * @param bound the upper bound (exclusive) for the returned value
+ * @throws IllegalArgumentException if {@code origin} is greater than
+ * or equal to {@code bound}
+ */
+ public static void checkRange(int origin, int bound) {
+ if (origin >= bound)
+ throw new IllegalArgumentException(BadRange);
+ }
+
+ /**
+ * Checks a {@code long} range for validity.
+ *
+ * @param origin the least value that can be returned
+ * @param bound the upper bound (exclusive) for the returned value
+ * @throws IllegalArgumentException if {@code origin} is greater than
+ * or equal to {@code bound}
+ */
+ public static void checkRange(long origin, long bound) {
+ if (origin >= bound)
+ throw new IllegalArgumentException(BadRange);
+ }
+
+ public static long[] convertSeedBytesToLongs(byte[] seed, int n, int z) {
+ final long[] result = new long[n];
+ final int m = Math.min(seed.length, n << 3);
+ // Distribute seed bytes into the words to be formed.
+ for (int j = 0; j < m; j++) {
+ result[j>>3] = (result[j>>3] << 8) | seed[j];
+ }
+ // If there aren't enough seed bytes for all the words we need,
+ // use a SplitMix-style PRNG to fill in the rest.
+ long v = result[0];
+ for (int j = (m + 7) >> 3; j < n; j++) {
+ result[j] = mixMurmur64(v += SILVER_RATIO_64);
+ }
+ // Finally, we need to make sure the last z words are not all zero.
+ search: {
+ for (int j = n - z; j < n; j++) {
+ if (result[j] != 0) break search;
+ }
+ // If they are, fill in using a SplitMix-style PRNG.
+ // Using "& ~1L" in the next line defends against the case z==1
+ // by guaranteeing that the first generated value will be nonzero.
+ long w = result[0] & ~1L;
+ for (int j = n - z; j < n; j++) {
+ result[j] = mixMurmur64(w += SILVER_RATIO_64);
+ }
+ }
+ return result;
+ }
+
+ public static int[] convertSeedBytesToInts(byte[] seed, int n, int z) {
+ final int[] result = new int[n];
+ final int m = Math.min(seed.length, n << 2);
+ // Distribute seed bytes into the words to be formed.
+ for (int j = 0; j < m; j++) {
+ result[j>>2] = (result[j>>2] << 8) | seed[j];
+ }
+ // If there aren't enough seed bytes for all the words we need,
+ // use a SplitMix-style PRNG to fill in the rest.
+ int v = result[0];
+ for (int j = (m + 3) >> 2; j < n; j++) {
+ result[j] = mixMurmur32(v += SILVER_RATIO_32);
+ }
+ // Finally, we need to make sure the last z words are not all zero.
+ search: {
+ for (int j = n - z; j < n; j++) {
+ if (result[j] != 0) break search;
+ }
+ // If they are, fill in using a SplitMix-style PRNG.
+ // Using "& ~1" in the next line defends against the case z==1
+ // by guaranteeing that the first generated value will be nonzero.
+ int w = result[0] & ~1;
+ for (int j = n - z; j < n; j++) {
+ result[j] = mixMurmur32(w += SILVER_RATIO_32);
+ }
+ }
+ return result;
+ }
+
+ /*
+ * Bounded versions of nextX methods used by streams, as well as
+ * the public nextX(origin, bound) methods. These exist mainly to
+ * avoid the need for multiple versions of stream spliterators
+ * across the different exported forms of streams.
+ */
+
+ /**
+ * This is the form of {@code nextLong} used by a {@code LongStream}
+ * {@code Spliterator} and by the public method
+ * {@code nextLong(origin, bound)}. If {@code origin} is greater
+ * than {@code bound}, then this method simply calls the unbounded
+ * version of {@code nextLong()}, choosing pseudorandomly from
+ * among all 2<sup>64</sup> possible {@code long} values}, and
+ * otherwise uses one or more calls to {@code nextLong()} to
+ * choose a value pseudorandomly from the possible values
+ * between {@code origin} (inclusive) and {@code bound} (exclusive).
+ *
+ * @implNote This method first calls {@code nextLong()} to obtain
+ * a {@code long} value that is assumed to be pseudorandomly
+ * chosen uniformly and independently from the 2<sup>64</sup>
+ * possible {@code long} values (that is, each of the 2<sup>64</sup>
+ * possible long values is equally likely to be chosen).
+ * Under some circumstances (when the specified range is not
+ * a power of 2), {@code nextLong()} may be called additional times
+ * to ensure that that the values in the specified range are
+ * equally likely to be chosen (provided the assumption holds).
+ *
+ * <p> The implementation considers four cases:
+ * <ol>
+ *
+ * <li> If the {@code} bound} is less than or equal to the {@code origin}
+ * (indicated an unbounded form), the 64-bit {@code long} value
+ * obtained from {@code nextLong()} is returned directly.
+ *
+ * <li> Otherwise, if the length <it>n</it> of the specified range is an
+ * exact power of two 2<sup><it>m</it></sup> for some integer
+ * <it>m</it>, then return the sum of {@code origin} and the
+ * <it>m</it> lowest-order bits of the value from {@code nextLong()}.
+ *
+ * <li> Otherwise, if the length <it>n</it> of the specified range
+ * is less than 2<sup>63</sup>, then the basic idea is to use the
+ * remainder modulo <it>n</it> of the value from {@code nextLong()},
+ * but with this approach some values will be over-represented.
+ * Therefore a loop is used to avoid potential bias by rejecting
+ * candidates that are too large. Assuming that the results from
+ * {@code nextLong()} are truly chosen uniformly and independently,
+ * the expected number of iterations will be somewhere between
+ * 1 and 2, depending on the precise value of <it>n</it>.
+ *
+ * <li> Otherwise, the length <it>n</it> of the specified range
+ * cannot be represented as a positive {@code long} value.
+ * A loop repeatedly calls {@code nextlong()} until obtaining
+ * a suitable candidate, Again, the expected number of iterations
+ * is less than 2.
+ *
+ * </ol>
+ *
+ * @param origin the least value that can be produced,
+ * unless greater than or equal to {@code bound}
+ * @param bound the upper bound (exclusive), unless {@code origin}
+ * is greater than or equal to {@code bound}
+ * @return a pseudorandomly chosen {@code long} value,
+ * which will be between {@code origin} (inclusive) and
+ * {@code bound} exclusive unless {@code origin}
+ * is greater than or equal to {@code bound}
+ */
+ public static long boundedNextLong(Rng rng, long origin, long bound) {
+ long r = rng.nextLong();
+ if (origin < bound) {
+ // It's not case (1).
+ final long n = bound - origin;
+ final long m = n - 1;
+ if ((n & m) == 0L) {
+ // It is case (2): length of range is a power of 2.
+ r = (r & m) + origin;
+ } else if (n > 0L) {
+ // It is case (3): need to reject over-represented candidates.
+ /* This loop takes an unlovable form (but it works):
+ because the first candidate is already available,
+ we need a break-in-the-middle construction,
+ which is concisely but cryptically performed
+ within the while-condition of a body-less for loop. */
+ for (long u = r >>> 1; // ensure nonnegative
+ u + m - (r = u % n) < 0L; // rejection check
+ u = rng.nextLong() >>> 1) // retry
+ ;
+ r += origin;
+ }
+ else {
+ // It is case (4): length of range not representable as long.
+ while (r < origin || r >= bound)
+ r = rng.nextLong();
+ }
+ }
+ return r;
+ }
+
+ /**
+ * This is the form of {@code nextLong} used by the public method
+ * {@code nextLong(bound)}. This is essentially a version of
+ * {@code boundedNextLong(origin, bound)} that has been
+ * specialized for the case where the {@code origin} is zero
+ * and the {@code bound} is greater than zero. The value
+ * returned is chosen pseudorandomly from nonnegative integer
+ * values less than {@code bound}.
+ *
+ * @implNote This method first calls {@code nextLong()} to obtain
+ * a {@code long} value that is assumed to be pseudorandomly
+ * chosen uniformly and independently from the 2<sup>64</sup>
+ * possible {@code long} values (that is, each of the 2<sup>64</sup>
+ * possible long values is equally likely to be chosen).
+ * Under some circumstances (when the specified range is not
+ * a power of 2), {@code nextLong()} may be called additional times
+ * to ensure that that the values in the specified range are
+ * equally likely to be chosen (provided the assumption holds).
+ *
+ * <p> The implementation considers two cases:
+ * <ol>
+ *
+ * <li> If {@code bound} is an exact power of two 2<sup><it>m</it></sup>
+ * for some integer <it>m</it>, then return the sum of {@code origin}
+ * and the <it>m</it> lowest-order bits of the value from
+ * {@code nextLong()}.
+ *
+ * <li> Otherwise, the basic idea is to use the remainder modulo
+ * <it>bound</it> of the value from {@code nextLong()},
+ * but with this approach some values will be over-represented.
+ * Therefore a loop is used to avoid potential bias by rejecting
+ * candidates that vare too large. Assuming that the results from
+ * {@code nextLong()} are truly chosen uniformly and independently,
+ * the expected number of iterations will be somewhere between
+ * 1 and 2, depending on the precise value of <it>bound</it>.
+ *
+ * </ol>
+ *
+ * @param bound the upper bound (exclusive); must be greater than zero
+ * @return a pseudorandomly chosen {@code long} value
+ */
+ public static long boundedNextLong(Rng rng, long bound) {
+ // Specialize boundedNextLong for origin == 0, bound > 0
+ final long m = bound - 1;
+ long r = rng.nextLong();
+ if ((bound & m) == 0L) {
+ // The bound is a power of 2.
+ r &= m;
+ } else {
+ // Must reject over-represented candidates
+ /* This loop takes an unlovable form (but it works):
+ because the first candidate is already available,
+ we need a break-in-the-middle construction,
+ which is concisely but cryptically performed
+ within the while-condition of a body-less for loop. */
+ for (long u = r >>> 1;
+ u + m - (r = u % bound) < 0L;
+ u = rng.nextLong() >>> 1)
+ ;
+ }
+ return r;
+ }
+
+ /**
+ * This is the form of {@code nextInt} used by an {@code IntStream}
+ * {@code Spliterator} and by the public method
+ * {@code nextInt(origin, bound)}. If {@code origin} is greater
+ * than {@code bound}, then this method simply calls the unbounded
+ * version of {@code nextInt()}, choosing pseudorandomly from
+ * among all 2<sup>64</sup> possible {@code int} values}, and
+ * otherwise uses one or more calls to {@code nextInt()} to
+ * choose a value pseudorandomly from the possible values
+ * between {@code origin} (inclusive) and {@code bound} (exclusive).
+ *
+ * @implNote The implementation of this method is identical to
+ * the implementation of {@code nextLong(origin, bound)}
+ * except that {@code int} values and the {@code nextInt()}
+ * method are used rather than {@code long} values and the
+ * {@code nextLong()} method.
+ *
+ * @param origin the least value that can be produced,
+ * unless greater than or equal to {@code bound}
+ * @param bound the upper bound (exclusive), unless {@code origin}
+ * is greater than or equal to {@code bound}
+ * @return a pseudorandomly chosen {@code int} value,
+ * which will be between {@code origin} (inclusive) and
+ * {@code bound} exclusive unless {@code origin}
+ * is greater than or equal to {@code bound}
+ */
+ public static int boundedNextInt(Rng rng, int origin, int bound) {
+ int r = rng.nextInt();
+ if (origin < bound) {
+ // It's not case (1).
+ final int n = bound - origin;
+ final int m = n - 1;
+ if ((n & m) == 0) {
+ // It is case (2): length of range is a power of 2.
+ r = (r & m) + origin;
+ } else if (n > 0) {
+ // It is case (3): need to reject over-represented candidates.
+ for (int u = r >>> 1;
+ u + m - (r = u % n) < 0;
+ u = rng.nextInt() >>> 1)
+ ;
+ r += origin;
+ }
+ else {
+ // It is case (4): length of range not representable as long.
+ while (r < origin || r >= bound)
+
+
+ r = rng.nextInt();
+ }
+ }
+ return r;
+ }
+
+ /**
+ * This is the form of {@code nextInt} used by the public method
+ * {@code nextInt(bound)}. This is essentially a version of
+ * {@code boundedNextInt(origin, bound)} that has been
+ * specialized for the case where the {@code origin} is zero
+ * and the {@code bound} is greater than zero. The value
+ * returned is chosen pseudorandomly from nonnegative integer
+ * values less than {@code bound}.
+ *
+ * @implNote The implementation of this method is identical to
+ * the implementation of {@code nextLong(bound)}
+ * except that {@code int} values and the {@code nextInt()}
+ * method are used rather than {@code long} values and the
+ * {@code nextLong()} method.
+ *
+ * @param bound the upper bound (exclusive); must be greater than zero
+ * @return a pseudorandomly chosen {@code long} value
+ */
+ public static int boundedNextInt(Rng rng, int bound) {
+ // Specialize boundedNextInt for origin == 0, bound > 0
+ final int m = bound - 1;
+ int r = rng.nextInt();
+ if ((bound & m) == 0) {
+ // The bound is a power of 2.
+ r &= m;
+ } else {
+ // Must reject over-represented candidates
+ for (int u = r >>> 1;
+ u + m - (r = u % bound) < 0;
+ u = rng.nextInt() >>> 1)
+ ;
+ }
+ return r;
+ }
+
+ /**
+ * This is the form of {@code nextDouble} used by a {@code DoubleStream}
+ * {@code Spliterator} and by the public method
+ * {@code nextDouble(origin, bound)}. If {@code origin} is greater
+ * than {@code bound}, then this method simply calls the unbounded
+ * version of {@code nextDouble()}, and otherwise scales and translates
+ * the result of a call to {@code nextDouble()} so that it lies
+ * between {@code origin} (inclusive) and {@code bound} (exclusive).
+ *
+ * @implNote The implementation considers two cases:
+ * <ol>
+ *
+ * <li> If the {@code bound} is less than or equal to the {@code origin}
+ * (indicated an unbounded form), the 64-bit {@code double} value
+ * obtained from {@code nextDouble()} is returned directly.
+ *
+ * <li> Otherwise, the result of a call to {@code nextDouble} is
+ * multiplied by {@code (bound - origin)}, then {@code origin}
+ * is added, and then if this this result is not less than
+ * {@code bound} (which can sometimes occur because of rounding),
+ * it is replaced with the largest {@code double} value that
+ * is less than {@code bound}.
+ *
+ * </ol>
+ *
+ * @param origin the least value that can be produced,
+ * unless greater than or equal to {@code bound}; must be finite
+ * @param bound the upper bound (exclusive), unless {@code origin}
+ * is greater than or equal to {@code bound}; must be finite
+ * @return a pseudorandomly chosen {@code double} value,
+ * which will be between {@code origin} (inclusive) and
+ * {@code bound} exclusive unless {@code origin}
+ * is greater than or equal to {@code bound},
+ * in which case it will be between 0.0 (inclusive)
+ * and 1.0 (exclusive)
+ */
+ public static double boundedNextDouble(Rng rng, double origin, double bound) {
+ double r = rng.nextDouble();
+ if (origin < bound) {
+ r = r * (bound - origin) + origin;
+ if (r >= bound) // may need to correct a rounding problem
+ r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
+ }
+ return r;
+ }
+
+ /**
+ * This is the form of {@code nextDouble} used by the public method
+ * {@code nextDouble(bound)}. This is essentially a version of
+ * {@code boundedNextDouble(origin, bound)} that has been
+ * specialized for the case where the {@code origin} is zero
+ * and the {@code bound} is greater than zero.
+ *
+ * @implNote The result of a call to {@code nextDouble} is
+ * multiplied by {@code bound}, and then if this result is
+ * not less than {@code bound} (which can sometimes occur
+ * because of rounding), it is replaced with the largest
+ * {@code double} value that is less than {@code bound}.
+ *
+ * @param bound the upper bound (exclusive); must be finite and
+ * greater than zero
+ * @return a pseudorandomly chosen {@code double} value
+ * between zero (inclusive) and {@code bound} (exclusive)
+ */
+ public static double boundedNextDouble(Rng rng, double bound) {
+ // Specialize boundedNextDouble for origin == 0, bound > 0
+ double r = rng.nextDouble();
+ r = r * bound;
+ if (r >= bound) // may need to correct a rounding problem
+ r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
+ return r;
+ }
+
+ /**
+ * This is the form of {@code nextFloat} used by a {@code FloatStream}
+ * {@code Spliterator} (if there were any) and by the public method
+ * {@code nextFloat(origin, bound)}. If {@code origin} is greater
+ * than {@code bound}, then this method simply calls the unbounded
+ * version of {@code nextFloat()}, and otherwise scales and translates
+ * the result of a call to {@code nextFloat()} so that it lies
+ * between {@code origin} (inclusive) and {@code bound} (exclusive).
+ *
+ * @implNote The implementation of this method is identical to
+ * the implementation of {@code nextDouble(origin, bound)}
+ * except that {@code float} values and the {@code nextFloat()}
+ * method are used rather than {@code double} values and the
+ * {@code nextDouble()} method.
+ *
+ * @param origin the least value that can be produced,
+ * unless greater than or equal to {@code bound}; must be finite
+ * @param bound the upper bound (exclusive), unless {@code origin}
+ * is greater than or equal to {@code bound}; must be finite
+ * @return a pseudorandomly chosen {@code float} value,
+ * which will be between {@code origin} (inclusive) and
+ * {@code bound} exclusive unless {@code origin}
+ * is greater than or equal to {@code bound},
+ * in which case it will be between 0.0 (inclusive)
+ * and 1.0 (exclusive)
+ */
+ public static float boundedNextFloat(Rng rng, float origin, float bound) {
+ float r = rng.nextFloat();
+ if (origin < bound) {
+ r = r * (bound - origin) + origin;
+ if (r >= bound) // may need to correct a rounding problem
+ r = Float.intBitsToFloat(Float.floatToIntBits(bound) - 1);
+ }
+ return r;
+ }
+
+ /**
+ * This is the form of {@code nextFloat} used by the public method
+ * {@code nextFloat(bound)}. This is essentially a version of
+ * {@code boundedNextFloat(origin, bound)} that has been
+ * specialized for the case where the {@code origin} is zero
+ * and the {@code bound} is greater than zero.
+ *
+ * @implNote The implementation of this method is identical to
+ * the implementation of {@code nextDouble(bound)}
+ * except that {@code float} values and the {@code nextFloat()}
+ * method are used rather than {@code double} values and the
+ * {@code nextDouble()} method.
+ *
+ * @param bound the upper bound (exclusive); must be finite and
+ * greater than zero
+ * @return a pseudorandomly chosen {@code float} value
+ * between zero (inclusive) and {@code bound} (exclusive)
+ */
+ public static float boundedNextFloat(Rng rng, float bound) {
+ // Specialize boundedNextFloat for origin == 0, bound > 0
+ float r = rng.nextFloat();
+ r = r * bound;
+ if (r >= bound) // may need to correct a rounding problem
+ r = Float.intBitsToFloat(Float.floatToIntBits(bound) - 1);
+ return r;
+ }
+
+ // The following decides which of two strategies initialSeed() will use.
+ private static boolean secureRandomSeedRequested() {
+ String pp = java.security.AccessController.doPrivileged(
+ new sun.security.action.GetPropertyAction(
+ "java.util.secureRandomSeed"));
+ return (pp != null && pp.equalsIgnoreCase("true"));
+ }
+
+ private static final boolean useSecureRandomSeed = secureRandomSeedRequested();
+
+ /**
+ * Returns a {@code long} value (chosen from some
+ * machine-dependent entropy source) that may be useful for
+ * initializing a source of seed values for instances of {@code Rng}
+ * created by zero-argument constructors. (This method should
+ * <it>not</it> be called repeatedly, once per constructed
+ * object; at most it should be called once per class.)
+ *
+ * @return a {@code long} value, randomly chosen using
+ * appropriate environmental entropy
+ */
+ public static long initialSeed() {
+ if (useSecureRandomSeed) {
+ 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);
+ return s;
+ }
+ return (mixStafford13(System.currentTimeMillis()) ^
+ mixStafford13(System.nanoTime()));
+ }
+
+ /**
+ * The fractional part (first 32 or 64 bits, then forced odd) of
+ * the golden ratio (1+sqrt(5))/2 and of the silver ratio 1+sqrt(2).
+ * Useful for producing good Weyl sequences or as arbitrary nonzero values.
+ */
+ public static final int GOLDEN_RATIO_32 = 0x9e3779b9;
+ public static final long GOLDEN_RATIO_64 = 0x9e3779b97f4a7c15L;
+ public static final int SILVER_RATIO_32 = 0x6A09E667;
+ public static final long SILVER_RATIO_64 = 0x6A09E667F3BCC909L;
+
+ /**
+ * Computes the 64-bit mixing function for MurmurHash3.
+ * This is a 64-bit hashing function with excellent avalanche statistics.
+ * https://github.com/aappleby/smhasher/wiki/MurmurHash3
+ *
+ * Note that if the argument {@code z} is 0, the result is 0.
+ *
+ * @param z any long value
+ *
+ * @return the result of hashing z
+ */
+ public static long mixMurmur64(long z) {
+ z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
+ z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
+ return z ^ (z >>> 33);
+ }
+
+ /**
+ * Computes Stafford variant 13 of the 64-bit mixing function for MurmurHash3.
+ * This is a 64-bit hashing function with excellent avalanche statistics.
+ * http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
+ *
+ * Note that if the argument {@code z} is 0, the result is 0.
+ *
+ * @param z any long value
+ *
+ * @return the result of hashing z
+ */
+ public static long mixStafford13(long z) {
+ z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
+ z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
+ return z ^ (z >>> 31);
+ }
+
+ /**
+ * Computes Doug Lea's 64-bit mixing function.
+ * This is a 64-bit hashing function with excellent avalanche statistics.
+ * It has the advantages of using the same multiplicative constant twice
+ * and of using only 32-bit shifts.
+ *
+ * Note that if the argument {@code z} is 0, the result is 0.
+ *
+ * @param z any long value
+ *
+ * @return the result of hashing z
+ */
+ public static long mixLea64(long z) {
+ z = (z ^ (z >>> 32)) * 0xdaba0b6eb09322e3L;
+ z = (z ^ (z >>> 32)) * 0xdaba0b6eb09322e3L;
+ return z ^ (z >>> 32);
+ }
+
+ /**
+ * Computes the 32-bit mixing function for MurmurHash3.
+ * This is a 32-bit hashing function with excellent avalanche statistics.
+ * https://github.com/aappleby/smhasher/wiki/MurmurHash3
+ *
+ * Note that if the argument {@code z} is 0, the result is 0.
+ *
+ * @param z any long value
+ *
+ * @return the result of hashing z
+ */
+ public static int mixMurmur32(int z) {
+ z = (z ^ (z >>> 16)) * 0x85ebca6b;
+ z = (z ^ (z >>> 13)) * 0xc2b2ae35;
+ return z ^ (z >>> 16);
+ }
+
+ /**
+ * Computes Doug Lea's 32-bit mixing function.
+ * This is a 32-bit hashing function with excellent avalanche statistics.
+ * It has the advantages of using the same multiplicative constant twice
+ * and of using only 16-bit shifts.
+ *
+ * Note that if the argument {@code z} is 0, the result is 0.
+ *
+ * @param z any long value
+ *
+ * @return the result of hashing z
+ */
+ public static int mixLea32(int z) {
+ z = (z ^ (z >>> 16)) * 0xd36d884b;
+ z = (z ^ (z >>> 16)) * 0xd36d884b;
+ return z ^ (z >>> 16);
+ }
+
+ // Non-public (package only) support for spliterators needed by AbstractSplittableRng
+ // and AbstractArbitrarilyJumpableRng and AbstractSharedRng
+
+ /**
+ * Base class for making Spliterator classes for streams of randomly chosen values.
+ */
+ static abstract class RandomSpliterator {
+ long index;
+ final long fence;
+
+ RandomSpliterator(long index, long fence) {
+ this.index = index; this.fence = fence;
+ }
+
+ public long estimateSize() {
+ return fence - index;
+ }
+
+ public int characteristics() {
+ return (Spliterator.SIZED | Spliterator.SUBSIZED |
+ Spliterator.NONNULL | Spliterator.IMMUTABLE);
+ }
+ }
+
+
+ /*
+ * Implementation support for nextExponential() and nextGaussian() methods of Rng.
+ *
+ * Each is implemented using McFarland's fast modified ziggurat algorithm (largely
+ * table-driven, with rare cases handled by computation and rejection sampling).
+ * Walker's alias method for sampling a discrete distribution also plays a role.
+ *
+ * The tables themselves, as well as a number of associated parameters, are defined
+ * in class java.util.DoubleZigguratTables, which is automatically generated by the
+ * program create_ziggurat_tables.c (which takes only a few seconds to run).
+ *
+ * For more information about the algorithms, see these articles:
+ *
+ * Christopher D. McFarland. 2016 (published online 24 Jun 2015). A modified ziggurat
+ * algorithm for generating exponentially and normally distributed pseudorandom numbers.
+ * Journal of Statistical Computation and Simulation 86 (7), pages 1281-1294.
+ * https://www.tandfonline.com/doi/abs/10.1080/00949655.2015.1060234
+ * Also at https://arxiv.org/abs/1403.6870 (26 March 2014).
+ *
+ * Alastair J. Walker. 1977. An efficient method for generating discrete random
+ * variables with general distributions. ACM Trans. Math. Software 3, 3
+ * (September 1977), 253-256. DOI: https://doi.org/10.1145/355744.355749
+ *
+ * Certain details of these algorithms depend critically on the quality of the
+ * low-order bits delivered by NextLong(). These algorithms should not be used
+ * with RNG algorithms (such as a simple Linear Congruential Generator) whose
+ * low-order output bits do not have good statistical quality.
+ */
+
+ // Implementation support for nextExponential()
+
+ static double computeNextExponential(Rng rng) {
+ long U1 = rng.nextLong();
+ // Experimentation on a variety of machines indicates that it is overall much faster
+ // to do the following & and < operations on longs rather than first cast U1 to int
+ // (but then we need to cast to int before doing the array indexing operation).
+ long i = U1 & DoubleZigguratTables.exponentialLayerMask;
+ if (i < DoubleZigguratTables.exponentialNumberOfLayers) {
+ // This is the fast path (occurring more than 98% of the time). Make an early exit.
+ return DoubleZigguratTables.exponentialX[(int)i] * (U1 >>> 1);
+ }
+ // We didn't use the upper part of U1 after all. We'll be able to use it later.
+
+ for (double extra = 0.0; ; ) {
+ // Use Walker's alias method to sample an (unsigned) integer j from a discrete
+ // probability distribution that includes the tail and all the ziggurat overhangs;
+ // j will be less than DoubleZigguratTables.exponentialNumberOfLayers + 1.
+ long UA = rng.nextLong();
+ int j = (int)UA & DoubleZigguratTables.exponentialAliasMask;
+ if (UA >= DoubleZigguratTables.exponentialAliasThreshold[j]) {
+ j = DoubleZigguratTables.exponentialAliasMap[j] & DoubleZigguratTables.exponentialSignCorrectionMask;
+ }
+ if (j > 0) { // Sample overhang j
+ // For the exponential distribution, every overhang is convex.
+ final double[] X = DoubleZigguratTables.exponentialX;
+ final double[] Y = DoubleZigguratTables.exponentialY;
+ for (;; U1 = (rng.nextLong() >>> 1)) {
+ long U2 = (rng.nextLong() >>> 1);
+ // Compute the actual x-coordinate of the randomly chosen point.
+ double x = (X[j] * 0x1.0p63) + ((X[j-1] - X[j]) * (double)U1);
+ // Does the point lie below the curve?
+ long Udiff = U2 - U1;
+ if (Udiff < 0) {
+ // We picked a point in the upper-right triangle. None of those can be accepted.
+ // So remap the point into the lower-left triangle and try that.
+ // In effect, we swap U1 and U2, and invert the sign of Udiff.
+ Udiff = -Udiff;
+ U2 = U1;
+ U1 -= Udiff;
+ }
+ if (Udiff >= DoubleZigguratTables.exponentialConvexMargin) {
+ return x + extra; // The chosen point is way below the curve; accept it.
+ }
+ // Compute the actual y-coordinate of the randomly chosen point.
+ double y = (Y[j] * 0x1.0p63) + ((Y[j] - Y[j-1]) * (double)U2);
+ // Now see how that y-coordinate compares to the curve
+ if (y <= Math.exp(-x)) {
+ return x + extra; // The chosen point is below the curve; accept it.
+ }
+ // Otherwise, we reject this sample and have to try again.
+ }
+ }
+ // We are now committed to sampling from the tail. We could do a recursive call
+ // and then add X[0] but we save some time and stack space by using an iterative loop.
+ extra += DoubleZigguratTables.exponentialX0;
+ // This is like the first five lines of this method, but if it returns, it first adds "extra".
+ U1 = rng.nextLong();
+ i = U1 & DoubleZigguratTables.exponentialLayerMask;
+ if (i < DoubleZigguratTables.exponentialNumberOfLayers) {
+ return DoubleZigguratTables.exponentialX[(int)i] * (U1 >>> 1) + extra;
+ }
+ }
+ }
+
+ // Implementation support for nextGaussian()
+
+ static double computeNextGaussian(Rng rng) {
+ long U1 = rng.nextLong();
+ // Experimentation on a variety of machines indicates that it is overall much faster
+ // to do the following & and < operations on longs rather than first cast U1 to int
+ // (but then we need to cast to int before doing the array indexing operation).
+ long i = U1 & DoubleZigguratTables.normalLayerMask;
+
+ if (i < DoubleZigguratTables.normalNumberOfLayers) {
+ // This is the fast path (occurring more than 98% of the time). Make an early exit.
+ return DoubleZigguratTables.normalX[(int)i] * U1; // Note that the sign bit of U1 is used here.
+ }
+ // We didn't use the upper part of U1 after all.
+ // Pull U1 apart into a sign bit and a 63-bit value for later use.
+ double signBit = (U1 >= 0) ? 1.0 : -1.0;
+ U1 = (U1 << 1) >>> 1;
+
+ // Use Walker's alias method to sample an (unsigned) integer j from a discrete
+ // probability distribution that includes the tail and all the ziggurat overhangs;
+ // j will be less than DoubleZigguratTables.normalNumberOfLayers + 1.
+ long UA = rng.nextLong();
+ int j = (int)UA & DoubleZigguratTables.normalAliasMask;
+ if (UA >= DoubleZigguratTables.normalAliasThreshold[j]) {
+ j = DoubleZigguratTables.normalAliasMap[j] & DoubleZigguratTables.normalSignCorrectionMask;
+ }
+
+ double x;
+ // Now the goal is to choose the result, which will be multiplied by signBit just before return.
+
+ // There are four kinds of overhangs:
+ //
+ // j == 0 : Sample from tail
+ // 0 < j < normalInflectionIndex : Overhang is convex; can reject upper-right triangle
+ // j == normalInflectionIndex : Overhang includes the inflection point
+ // j > normalInflectionIndex : Overhang is concave; can accept point in lower-left triangle
+ //
+ // Choose one of four loops to compute x, each specialized for a specific kind of overhang.
+ // Conditional statements are arranged such that the more likely outcomes are first.
+
+ // In the three cases other than the tail case:
+ // U1 represents a fraction (scaled by 2**63) of the width of rectangle measured from the left.
+ // U2 represents a fraction (scaled by 2**63) of the height of rectangle measured from the top.
+ // Together they indicate a randomly chosen point within the rectangle.
+
+ final double[] X = DoubleZigguratTables.normalX;
+ final double[] Y = DoubleZigguratTables.normalY;
+ if (j > DoubleZigguratTables.normalInflectionIndex) { // Concave overhang
+ for (;; U1 = (rng.nextLong() >>> 1)) {
+ long U2 = (rng.nextLong() >>> 1);
+ // Compute the actual x-coordinate of the randomly chosen point.
+ x = (X[j] * 0x1.0p63) + ((X[j-1] - X[j]) * (double)U1);
+ // Does the point lie below the curve?
+ long Udiff = U2 - U1;
+ if (Udiff >= 0) {
+ break; // The chosen point is in the lower-left triangle; accept it.
+ }
+ if (Udiff <= -DoubleZigguratTables.normalConcaveMargin) {
+ continue; // The chosen point is way above the curve; reject it.
+ }
+ // Compute the actual y-coordinate of the randomly chosen point.
+ double y = (Y[j] * 0x1.0p63) + ((Y[j] - Y[j-1]) * (double)U2);
+ // Now see how that y-coordinate compares to the curve
+ if (y <= Math.exp(-0.5*x*x)) {
+ break; // The chosen point is below the curve; accept it.
+ }
+ // Otherwise, we reject this sample and have to try again.
+ }
+ } else if (j == 0) { // Tail
+ // Tail-sampling method of Marsaglia and Tsang. See any one of:
+ // Marsaglia and Tsang. 1984. A fast, easily implemented method for sampling from decreasing
+ // or symmetric unimodal density functions. SIAM J. Sci. Stat. Comput. 5, 349-359.
+ // Marsaglia and Tsang. 1998. The Monty Python method for generating random variables.
+ // ACM Trans. Math. Softw. 24, 3 (September 1998), 341-350. See page 342, step (4).
+ // http://doi.org/10.1145/292395.292453
+ // Thomas, Luk, Leong, and Villasenor. 2007. Gaussian random number generators.
+ // ACM Comput. Surv. 39, 4, Article 11 (November 2007). See Algorithm 16.
+ // http://doi.org/10.1145/1287620.1287622
+ // Compute two separate random exponential samples and then compare them in certain way.
+ do {
+ x = (1.0 / DoubleZigguratTables.normalX0) * computeNextExponential(rng);
+ } while (computeNextExponential(rng) < 0.5*x*x);
+ x += DoubleZigguratTables.normalX0;
+ } else if (j < DoubleZigguratTables.normalInflectionIndex) { // Convex overhang
+ for (;; U1 = (rng.nextLong() >>> 1)) {
+ long U2 = (rng.nextLong() >>> 1);
+ // Compute the actual x-coordinate of the randomly chosen point.
+ x = (X[j] * 0x1.0p63) + ((X[j-1] - X[j]) * (double)U1);
+ // Does the point lie below the curve?
+ long Udiff = U2 - U1;
+ if (Udiff < 0) {
+ // We picked a point in the upper-right triangle. None of those can be accepted.
+ // So remap the point into the lower-left triangle and try that.
+ // In effect, we swap U1 and U2, and invert the sign of Udiff.
+ Udiff = -Udiff;
+ U2 = U1;
+ U1 -= Udiff;
+ }
+ if (Udiff >= DoubleZigguratTables.normalConvexMargin) {
+ break; // The chosen point is way below the curve; accept it.
+ }
+ // Compute the actual y-coordinate of the randomly chosen point.
+ double y = (Y[j] * 0x1.0p63) + ((Y[j] - Y[j-1]) * (double)U2);
+ // Now see how that y-coordinate compares to the curve
+ if (y <= Math.exp(-0.5*x*x)) break; // The chosen point is below the curve; accept it.
+ // Otherwise, we reject this sample and have to try again.
+ }
+ } else {
+ // The overhang includes the inflection point, so the curve is both convex and concave
+ for (;; U1 = (rng.nextLong() >>> 1)) {
+ long U2 = (rng.nextLong() >>> 1);
+ // Compute the actual x-coordinate of the randomly chosen point.
+ x = (X[j] * 0x1.0p63) + ((X[j-1] - X[j]) * (double)U1);
+ // Does the point lie below the curve?
+ long Udiff = U2 - U1;
+ if (Udiff >= DoubleZigguratTables.normalConvexMargin) {
+ break; // The chosen point is way below the curve; accept it.
+ }
+ if (Udiff <= -DoubleZigguratTables.normalConcaveMargin) {
+ continue; // The chosen point is way above the curve; reject it.
+ }
+ // Compute the actual y-coordinate of the randomly chosen point.
+ double y = (Y[j] * 0x1.0p63) + ((Y[j] - Y[j-1]) * (double)U2);
+ // Now see how that y-coordinate compares to the curve
+ if (y <= Math.exp(-0.5*x*x)) {
+ break; // The chosen point is below the curve; accept it.
+ }
+ // Otherwise, we reject this sample and have to try again.
+ }
+ }
+ return signBit*x;
+ }
+
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/SplittableRandom.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,292 @@
+/*
+ * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * 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:
+ *
+ * <ul>
+ *
+ * <li>Series of generated values pass the DieHarder suite testing
+ * independence and uniformity properties of random number generators.
+ * (Most recently validated with <a
+ * href="http://www.phy.duke.edu/~rgb/General/dieharder.php"> version
+ * 3.31.1</a>.) 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. The <em>period</em>
+ * (length of any series of generated values before it repeats) is at
+ * least 2<sup>64</sup>. </li>
+ *
+ * <li> 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. </li>
+ *
+ * <li>Instances of SplittableRandom are <em>not</em> thread-safe.
+ * They are designed to be split, not shared, across threads. For
+ * example, a {@link java.util.concurrent.ForkJoinTask
+ * fork/join-style} computation using random numbers might include a
+ * construction of the form {@code new
+ * Subtask(aSplittableRandom.split()).fork()}.
+ *
+ * <li>This class provides additional methods for generating random
+ * streams, that employ the above techniques when used in {@code
+ * stream.parallel()} mode.</li>
+ *
+ * </ul>
+ *
+ * <p>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 final class SplittableRandom extends AbstractSplittableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * This algorithm was inspired by the "DotMix" algorithm by
+ * Leiserson, Schardl, and Sukha "Deterministic Parallel
+ * Random-Number Generation for Dynamic-Multithreading Platforms",
+ * PPoPP 2012, as well as those in "Parallel random numbers: as
+ * easy as 1, 2, 3" by Salmon, Morae, Dror, and Shaw, SC 2011. It
+ * differs mainly in simplifying and cheapening operations.
+ *
+ * The primary update step (method nextSeed()) is to add a
+ * constant ("gamma") to the current (64 bit) seed, forming a
+ * simple sequence. The seed and the gamma values for any two
+ * SplittableRandom instances are highly likely to be different.
+ *
+ * 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 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 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.
+ *
+ * The resulting generator thus transforms a sequence in which
+ * (typically) many bits change on each step, with an inexpensive
+ * mixer with good (but less than cryptographically secure)
+ * avalanching.
+ *
+ * The default (no-argument) constructor, in essence, invokes
+ * 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
+ * carrying around twice the state add more overhead than appears
+ * warranted for current usages.
+ *
+ * File organization: First the non-public methods that constitute
+ * the main algorithm, then the main public methods, followed by
+ * some custom spliterator classes needed for stream methods.
+ */
+
+ /**
+ * The golden ratio scaled to 64bits, used as the initial gamma
+ * value for (unsplit) SplittableRandoms.
+ */
+ private static final long GOLDEN_GAMMA = 0x9e3779b97f4a7c15L;
+
+ /**
+ * The seed. Updated only via method nextSeed.
+ */
+ private long seed;
+
+ /**
+ * The step value.
+ */
+ private final long gamma;
+
+ /**
+ * Internal constructor used by all others except default constructor.
+ */
+ private SplittableRandom(long seed, long gamma) {
+ this.seed = seed;
+ this.gamma = gamma;
+ }
+
+ /* The implementation of AbstractSplittableRng requires this. */
+ // SplittableRandom getThis() { return this; }
+
+ /**
+ * Computes Stafford variant 13 of 64bit mix function.
+ * http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
+ */
+ private static long mix64(long z) {
+ z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
+ z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
+ return z ^ (z >>> 31);
+ }
+
+ /**
+ * Returns the 32 high bits of Stafford variant 4 mix64 function as int.
+ * http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
+ */
+ private static int mix32(long z) {
+ z = (z ^ (z >>> 33)) * 0x62a9d9ed799705f5L;
+ return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32);
+ }
+
+ /**
+ * Returns the gamma value to use for a new split instance.
+ * Uses the 64bit mix function from MurmurHash3.
+ * https://github.com/aappleby/smhasher/wiki/MurmurHash3
+ */
+ 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;
+ }
+
+ /**
+ * Adds gamma to seed.
+ */
+ private long nextSeed() {
+ return seed += gamma;
+ }
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /* ---------------- public methods ---------------- */
+
+ /**
+ * Creates a new SplittableRandom instance using the specified
+ * initial seed. SplittableRandom instances created with the same
+ * seed in the same program generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public SplittableRandom(long seed) {
+ this(seed, GOLDEN_GAMMA);
+ }
+
+ /**
+ * Creates a new SplittableRandom instance that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program; and
+ * may, and typically does, vary across program invocations.
+ */
+ public SplittableRandom() { // emulate defaultGen.split()
+ long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA);
+ this.seed = mix64(s);
+ this.gamma = mixGamma(s + GOLDEN_GAMMA);
+ }
+
+ // public SplittableRandom copy() { return new SplittableRandom(seed, gamma); }
+
+ /**
+ * Constructs and returns a new SplittableRandom instance that
+ * shares no mutable state with this instance. However, with very
+ * high probability, the set of values collectively generated by
+ * the two objects has the same statistical properties as if the
+ * same quantity of values were generated by a single thread using
+ * a single SplittableRandom object. Either or both of the two
+ * objects may be further split using the {@code split()} method,
+ * and the same expected statistical properties apply to the
+ * entire set of generators constructed by such recursive
+ * splitting.
+ *
+ * @return the new SplittableRandom instance
+ */
+ public SplittableRandom split() {
+ return new SplittableRandom(nextLong(), mixGamma(nextSeed()));
+ }
+
+ public SplittableRandom split(SplittableRng source) {
+ return new SplittableRandom(source.nextLong(), mixGamma(source.nextLong()));
+ }
+
+ /**
+ * Returns a pseudorandom {@code int} value.
+ *
+ * @return a pseudorandom {@code int} value
+ */
+ public int nextInt() {
+ return mix32(nextSeed());
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+ public long nextLong() {
+ return mix64(nextSeed());
+ }
+
+ static final BigInteger thePeriod = BigInteger.ONE.shiftLeft(64); // Period is 2**64
+ public BigInteger period() { return thePeriod; }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/SplittableRng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+// package java.util;
+
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.DoubleStream;
+
+/**
+ * This interface is designed to provide a common protocol for objects
+ * that generate sequences of pseudorandom numbers (or Boolean values)
+ * and furthermore can be <it>split</it> into two objects (the original
+ * one and a new one) each of which obey that same protocol (and therefore
+ * can be recursively split indefinitely).
+ *
+ * <p>Ideally, all {@code SplittableRNG} objects produced by recursive
+ * splitting from a single original {@code SplittableRNG} object are
+ * statistically independent of one another and individually uniform.
+ * Therefore we would expect the set of values collectively generated
+ * by a set of such objects to have the same statistical properties as
+ * if the same quantity of values were generated by a single thread
+ * using a single {@code SplittableRNG} object. In practice, one must
+ * settle for some approximation to independence and uniformity.
+ *
+ * <p>Methods are provided to perform a single splitting operation and
+ * also to produce a stream of generators split off from the original
+ * (by either iterative or recursive splitting, or a combination).
+ *
+ * <p>An implementation of the {@code SplittableRng} interface must provide
+ * concrete definitions for the methods {@code nextInt()}, {@code nextLong},
+ * {@code period()}, {@code split()}, {@code split(SplittableRng)},
+ * {@code splits()}, {@code splits(long)}, {@code splits(SplittableRng)},
+ * and {@code splits(long, SplittableRng)}. Perhaps the most convenient
+ * way to implement this interface is to extend the abstract class
+ * {@link java.util.AbstractSplittableRng}.
+ *
+ * <p>Objects that implement {@code java.util.SplittableRNG} are
+ * typically not cryptographically secure. Consider instead using
+ * {@link java.security.SecureRandom} to get a cryptographically
+ * secure pseudo-random number generator for use by
+ * security-sensitive applications.
+ *
+ * @author Guy Steele
+ * @since 1.9
+ */
+interface SplittableRng extends StreamableRng {
+
+ /**
+ * Returns a new pseudorandom number generator, split off from
+ * this one, that implements the {@code Rng} and {@code SplittableRng}
+ * interfaces.
+ *
+ * This pseudorandom number generator may be used as a source of
+ * pseudorandom bits used to initialize the state the new one.
+ *
+ * @return a new object that implements the {@code Rng} and
+ * {@code SplittableRng} interfaces
+ */
+ SplittableRng split();
+
+ /**
+ * Returns a new pseudorandom number generator, split off from
+ * this one, that implements the {@code Rng} and {@code SplittableRng}
+ * interfaces.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ *
+ * @return an object that implements the {@code Rng} and
+ * {@code SplittableRng} interfaces
+ */
+ SplittableRng split(SplittableRng source);
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code SplittableRng}
+ * interface.
+ *
+ * This pseudorandom number generator may be used as a source of
+ * pseudorandom bits used to initialize the state the new ones.
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to {@code splits(Long.MAX_VALUE)}.
+ *
+ * @return a stream of {@code SplittableRng} objects
+ */
+ default Stream<SplittableRng> splits() {
+ return this.splits(this);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code SplittableRng} interface.
+ *
+ * This pseudorandom number generator may be used as a source of
+ * pseudorandom bits used to initialize the state the new ones.
+ *
+ * @param streamSize the number of values to generate
+ * @return a stream of {@code SplittableRng} objects
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ Stream<SplittableRng> splits(long streamSize);
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code SplittableRng}
+ * interface.
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to {@code splits(Long.MAX_VALUE, source)}.
+ *
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ *
+ * @return a stream of {@code SplittableRng} objects
+ */
+ Stream<SplittableRng> splits(SplittableRng source);
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code SplittableRng} interface.
+ *
+ * @param streamSize the number of values to generate
+ * @param source a {@code SplittableRng} instance to be used instead
+ * of this one as a source of pseudorandom bits used to
+ * initialize the state of the new ones.
+ * @return a stream of {@code SplittableRng} objects
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ Stream<SplittableRng> splits(long streamSize, SplittableRng source);
+
+ /**
+ * Returns an effectively unlimited stream of new pseudorandom
+ * number generators, each of which implements the {@code Rng}
+ * interface. Ideally the generators in the stream will appear
+ * to be statistically independent.
+ *
+ * @implNote The default implementation calls {@code splits()}.
+ *
+ * @return a stream of objects that implement the {@code Rng} interface
+ */
+ default Stream<Rng> rngs() {
+ return this.splits().map(x -> (Rng)x);
+ }
+
+ /**
+ * Returns a stream producing the given {@code streamSize} number of
+ * new pseudorandom number generators, each of which implements the
+ * {@code Rng} interface. Ideally the generators in the stream will
+ * appear to be statistically independent.
+ *
+ * @implNote The default implementation calls {@code splits(streamSize)}.
+ *
+ * @param streamSize the number of generators to generate
+ * @return a stream of objects that implement the {@code Rng} interface
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ default Stream<Rng> rngs(long streamSize) {
+ return this.splits(streamSize).map(x -> (Rng)x);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/StreamableRng.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+
+/**
+ * The {@code StreamableRng} interface augments the {@code Rng} interface
+ * to provide methods that return streams of {@code Rng} objects.
+ * Ideally, such a stream of objects would have the property that the
+ * behavior of each object is statistically independent of all the others.
+ * In practice, one may have to settle for some approximation to this property.
+ *
+ * A generator that implements interface {@link java.util.SplittableRng}
+ * may choose to use its {@code splits} method to implement the {@code rngs}
+ * method required by this interface.
+ *
+ * A generator that implements interface {@link java.util.JumpableRng}
+ * may choose to use its {@code jumps} method to implement the {@code rngs}
+ * method required by this interface.
+ *
+ * A generator that implements interface {@link java.util.LeapableRng}
+ * may choose to use its {@code leaps} method to implement the {@code rngs}
+ * method required by this interface.
+ *
+ * <p>An implementation of the {@code StreamableRng} interface must provide
+ * concrete definitions for the methods {@code nextInt()}, {@code nextLong},
+ * {@code period()}, and {@code rngs()}.
+ * Default implementations are provided for all other methods.
+ *
+ * <p>Objects that implement {@code java.util.StreamableRng} are typically
+ * not cryptographically secure. Consider instead using
+ * {@link java.security.SecureRandom} to get a cryptographically
+ * secure pseudo-random number generator for use by
+ * security-sensitive applications.
+ *
+ * @author Guy Steele
+ * @since 1.9
+ */
+
+import java.util.stream.Stream;
+
+interface StreamableRng extends Rng {
+ /**
+ * Returns an effectively unlimited stream of objects, each of
+ * which implements the {@code Rng} interface. Ideally the
+ * generators in the stream will appear to be statistically
+ * independent. The new generators should be of the same kind
+ * as this generator.
+ *
+ * @implNote It is permitted to implement this method in a manner
+ * equivalent to {@code rngs(Long.MAX_VALUE)}.
+ *
+ * @return a stream of objects that implement the {@code Rng} interface
+ */
+ Stream<Rng> rngs();
+
+ /**
+ * Returns an effectively unlimited stream of objects, each of
+ * which implements the {@code Rng} interface. Ideally the
+ * generators in the stream will appear to be statistically
+ * independent. The new generators should be of the same kind
+ * as this generator.
+ *
+ * @implNote The default implementation calls {@code rngs()} and
+ * then limits its length to {@code streamSize}.
+ *
+ * @param streamSize the number of generators to generate
+ * @return a stream of objects that implement the {@code Rng} interface
+ * @throws IllegalArgumentException if {@code streamSize} is
+ * less than zero
+ */
+ default Stream<Rng> rngs(long streamSize) {
+ RngSupport.checkStreamSize(streamSize);
+ return rngs().limit(streamSize);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/ThreadLocalRandom.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,386 @@
+/*
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+/*
+ *
+ *
+ *
+ *
+ *
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+// package java.util.concurrent;
+
+import java.io.ObjectStreamField;
+import java.math.BigInteger;
+// import java.util.Random;
+import java.util.Spliterator;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A random number generator isolated to the current thread. Like the
+ * global {@link java.util.Random} generator used by the {@link
+ * java.lang.Math} class, a {@code ThreadLocalRandom} is initialized
+ * with an internally generated seed that may not otherwise be
+ * modified. When applicable, use of {@code ThreadLocalRandom} rather
+ * than shared {@code Random} objects in concurrent programs will
+ * typically encounter much less overhead and contention. Use of
+ * {@code ThreadLocalRandom} is particularly appropriate when multiple
+ * tasks (for example, each a {@link ForkJoinTask}) use random numbers
+ * in parallel in thread pools.
+ *
+ * <p>Usages of this class should typically be of the form:
+ * {@code ThreadLocalRandom.current().nextX(...)} (where
+ * {@code X} is {@code Int}, {@code Long}, etc).
+ * When all usages are of this form, it is never possible to
+ * accidently share a {@code ThreadLocalRandom} across multiple threads.
+ *
+ * <p>This class also provides additional commonly used bounded random
+ * generation methods.
+ *
+ * <p>Instances of {@code ThreadLocalRandom} 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}.
+ *
+ * @since 1.7
+ * @author Doug Lea
+ */
+public class ThreadLocalRandom extends Random {
+ /*
+ * This class implements the java.util.Random API (and subclasses
+ * Random) using a single static instance that accesses random
+ * number state held in class Thread (primarily, field
+ * threadLocalRandomSeed). In doing so, it also provides a home
+ * for managing package-private utilities that rely on exactly the
+ * same state as needed to maintain the ThreadLocalRandom
+ * instances. We leverage the need for an initialization flag
+ * field to also use it as a "probe" -- a self-adjusting thread
+ * hash used for contention avoidance, as well as a secondary
+ * simpler (xorShift) random seed that is conservatively used to
+ * avoid otherwise surprising users by hijacking the
+ * ThreadLocalRandom sequence. The dual use is a marriage of
+ * convenience, but is a simple and efficient way of reducing
+ * application-level overhead and footprint of most concurrent
+ * programs.
+ *
+ * Even though this class subclasses java.util.Random, it uses the
+ * same basic algorithm as java.util.SplittableRandom. (See its
+ * internal documentation for explanations, which are not repeated
+ * here.) Because ThreadLocalRandoms are not splittable
+ * though, we use only a single 64bit gamma.
+ *
+ * Because this class is in a different package than class Thread,
+ * field access methods use Unsafe to bypass access control rules.
+ * To conform to the requirements of the Random superclass
+ * constructor, the common static ThreadLocalRandom maintains an
+ * "initialized" field for the sake of rejecting user calls to
+ * setSeed while still allowing a call from constructor. Note
+ * that serialization is completely unnecessary because there is
+ * only a static singleton. But we generate a serial form
+ * containing "rnd" and "initialized" fields to ensure
+ * compatibility across versions.
+ *
+ * Implementations of non-core methods are mostly the same as in
+ * SplittableRandom, that were in part derived from a previous
+ * version of this class.
+ *
+ * The nextLocalGaussian ThreadLocal supports the very rarely used
+ * nextGaussian method by providing a holder for the second of a
+ * pair of them. As is true for the base class version of this
+ * method, this time/space tradeoff is probably never worthwhile,
+ * but we provide identical statistical properties.
+ */
+
+ /** Generates per-thread initialization/probe field */
+ private static final AtomicInteger probeGenerator =
+ new AtomicInteger();
+
+ /**
+ * The next seed for default constructors.
+ */
+ private static final AtomicLong seeder = new AtomicLong(RngSupport.initialSeed());
+
+ /**
+ * The seed increment
+ */
+ private static final long GAMMA = 0x9e3779b97f4a7c15L;
+
+ /**
+ * The increment for generating probe values
+ */
+ private static final int PROBE_INCREMENT = 0x9e3779b9;
+
+ /**
+ * The increment of seeder per new instance
+ */
+ private static final long SEEDER_INCREMENT = 0xbb67ae8584caa73bL;
+
+ // Constants from SplittableRandom
+ private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53)
+ private static final float FLOAT_UNIT = 0x1.0p-24f; // 1.0f / (1 << 24)
+
+ /** Rarely-used holder for the second of a pair of Gaussians */
+ private static final ThreadLocal<Double> nextLocalGaussian =
+ new ThreadLocal<Double>();
+
+ private static long mix64(long z) {
+ z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
+ z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
+ return z ^ (z >>> 33);
+ }
+
+ private static int mix32(long z) {
+ z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
+ return (int)(((z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L) >>> 32);
+ }
+
+ /**
+ * Field used only during singleton initialization.
+ * True when constructor completes.
+ */
+ boolean initialized;
+
+ /** Constructor used only for static singleton */
+ private ThreadLocalRandom() {
+ initialized = true; // false during super() call
+ }
+
+ /** The common ThreadLocalRandom */
+ static final ThreadLocalRandom instance = new ThreadLocalRandom();
+
+ /**
+ * Initialize Thread fields for the current thread. Called only
+ * when Thread.threadLocalRandomProbe is zero, indicating that a
+ * thread local seed value needs to be generated. Note that even
+ * though the initialization is purely thread-local, we need to
+ * rely on (static) atomic generators to initialize the values.
+ */
+ static final void localInit() {
+ int p = probeGenerator.addAndGet(PROBE_INCREMENT);
+ int probe = (p == 0) ? 1 : p; // skip 0
+ long seed = mix64(seeder.getAndAdd(SEEDER_INCREMENT));
+ Thread t = Thread.currentThread();
+ UNSAFE.putLong(t, SEED, seed);
+ UNSAFE.putInt(t, PROBE, probe);
+ }
+
+ /**
+ * Returns the current thread's {@code ThreadLocalRandom}.
+ *
+ * @return the current thread's {@code ThreadLocalRandom}
+ */
+ public static ThreadLocalRandom current() {
+ if (UNSAFE.getInt(Thread.currentThread(), PROBE) == 0)
+ localInit();
+ return instance;
+ }
+
+ /**
+ * Throws {@code UnsupportedOperationException}. Setting seeds in
+ * this generator is not supported.
+ *
+ * @throws UnsupportedOperationException always
+ */
+ public void setSeed(long seed) {
+ // only allow call from super() constructor
+ if (initialized)
+ throw new UnsupportedOperationException();
+ }
+
+ final long nextSeed() {
+ Thread t; long r; // read and update per-thread seed
+ UNSAFE.putLong(t = Thread.currentThread(), SEED,
+ r = UNSAFE.getLong(t, SEED) + GAMMA);
+ return r;
+ }
+
+ // We must define this (to override the definition inherited from
+ // class Random), but we don't use it from within other methods.
+ protected int next(int bits) {
+ return (int)(mix64(nextSeed()) >>> (64 - bits));
+ }
+
+ /**
+ * Returns a pseudorandom {@code int} value.
+ *
+ * @return a pseudorandom {@code int} value
+ */
+ public int nextInt() {
+ return mix32(nextSeed());
+ }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+ public long nextLong() {
+ return mix64(nextSeed());
+ }
+
+ public double nextGaussian() {
+ // Use nextLocalGaussian instead of nextGaussian field
+ Double d = nextLocalGaussian.get();
+ if (d != null) {
+ nextLocalGaussian.set(null);
+ return d.doubleValue();
+ }
+ double v1, v2, s;
+ do {
+ v1 = 2 * nextDouble() - 1; // between -1 and 1
+ v2 = 2 * nextDouble() - 1; // between -1 and 1
+ s = v1 * v1 + v2 * v2;
+ } while (s >= 1 || s == 0);
+ double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
+ nextLocalGaussian.set(new Double(v2 * multiplier));
+ return v1 * multiplier;
+ }
+
+ static final BigInteger thePeriod = BigInteger.valueOf(1).shiftLeft(64); // Period is 2**64
+ public BigInteger period() { return thePeriod; }
+
+ // Within-package utilities
+
+ /*
+ * Descriptions of the usages of the methods below can be found in
+ * the classes that use them. Briefly, a thread's "probe" value is
+ * a non-zero hash code that (probably) does not collide with
+ * other existing threads with respect to any power of two
+ * collision space. When it does collide, it is pseudo-randomly
+ * adjusted (using a Marsaglia XorShift). The nextSecondarySeed
+ * method is used in the same contexts as ThreadLocalRandom, but
+ * only for transient usages such as random adaptive spin/block
+ * sequences for which a cheap Rng suffices and for which it could
+ * in principle disrupt user-visible statistical properties of the
+ * main ThreadLocalRandom if we were to use it.
+ *
+ * Note: Because of package-protection issues, versions of some
+ * these methods also appear in some subpackage classes.
+ */
+
+ /**
+ * Returns the probe value for the current thread without forcing
+ * initialization. Note that invoking ThreadLocalRandom.current()
+ * can be used to force initialization on zero return.
+ */
+ static final int getProbe() {
+ return UNSAFE.getInt(Thread.currentThread(), PROBE);
+ }
+
+ /**
+ * Pseudo-randomly advances and records the given probe value for the
+ * given thread.
+ */
+ static final int advanceProbe(int probe) {
+ probe ^= probe << 13; // xorshift
+ probe ^= probe >>> 17;
+ probe ^= probe << 5;
+ UNSAFE.putInt(Thread.currentThread(), PROBE, probe);
+ return probe;
+ }
+
+ /**
+ * Returns the pseudo-randomly initialized or updated secondary seed.
+ */
+ static final int nextSecondarySeed() {
+ int r;
+ Thread t = Thread.currentThread();
+ if ((r = UNSAFE.getInt(t, SECONDARY)) != 0) {
+ r ^= r << 13; // xorshift
+ r ^= r >>> 17;
+ r ^= r << 5;
+ }
+ else {
+ localInit();
+ if ((r = (int)UNSAFE.getLong(t, SEED)) == 0)
+ r = 1; // avoid zero
+ }
+ UNSAFE.putInt(t, SECONDARY, r);
+ return r;
+ }
+
+ // Serialization support
+
+ private static final long serialVersionUID = -5851777807851030925L;
+
+ /**
+ * @serialField rnd long
+ * seed for random computations
+ * @serialField initialized boolean
+ * always true
+ */
+ private static final ObjectStreamField[] serialPersistentFields = {
+ new ObjectStreamField("rnd", long.class),
+ new ObjectStreamField("initialized", boolean.class),
+ };
+
+ /**
+ * Saves the {@code ThreadLocalRandom} to a stream (that is, serializes it).
+ * @param s the stream
+ * @throws java.io.IOException if an I/O error occurs
+ */
+ private void writeObject(java.io.ObjectOutputStream s)
+ throws java.io.IOException {
+
+ java.io.ObjectOutputStream.PutField fields = s.putFields();
+ fields.put("rnd", UNSAFE.getLong(Thread.currentThread(), SEED));
+ fields.put("initialized", true);
+ s.writeFields();
+ }
+
+ /**
+ * Returns the {@link #current() current} thread's {@code ThreadLocalRandom}.
+ * @return the {@link #current() current} thread's {@code ThreadLocalRandom}
+ */
+ private Object readResolve() {
+ return current();
+ }
+
+ // Unsafe mechanics
+ private static final sun.misc.Unsafe UNSAFE;
+ private static final long SEED;
+ private static final long PROBE;
+ private static final long SECONDARY;
+ static {
+ try {
+ UNSAFE = sun.misc.Unsafe.getUnsafe();
+ Class<?> tk = Thread.class;
+ SEED = UNSAFE.objectFieldOffset
+ (tk.getDeclaredField("threadLocalRandomSeed"));
+ PROBE = UNSAFE.objectFieldOffset
+ (tk.getDeclaredField("threadLocalRandomProbe"));
+ SECONDARY = UNSAFE.objectFieldOffset
+ (tk.getDeclaredField("threadLocalRandomSecondarySeed"));
+ } catch (Exception e) {
+ throw new Error(e);
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/Xoroshiro128Plus.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,287 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code Xoroshiro128Plus} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.LeapableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new {@code Xoroshiro128Plus} objects
+ * by "jumping" or "leaping".
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators,
+ * <em>except</em> that it does not pass the binary rank tests of PractRand,
+ * which fail due to the lowest bit being an LFSR; all other bits pass all tests.
+ * For this reason may be best for some purposes to use this generator to generate
+ * pseudorandom {@code int}, {@code float}, and {@code double} values but not
+ * {@code long} values. For the same reason, it may be best not to use the
+ * method {@code nextGaussian} or {@code nextExponential} with this generator.
+ *
+ * <p>The class {@code Xoroshiro128Plus} uses the {@code xoroshiro128} algorithm,
+ * version 1.0 (parameters 24, 16, 37), with the "+" scrambler
+ * (the returned value is the sum of the two state variables {@code x0} and {@code x1}).
+ * Its state consists of two {@code long} fields {@code x0} and {@code x1},
+ * which can take on any values provided that they are not both zero.
+ * The period of this generator is 2<sup>128</sup>-1.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are equidistributed.
+ * To be precise, over the course of the cycle of length 2<sup>128</sup>-1,
+ * each nonzero {@code long} value is generated 2<sup>64</sup> times,
+ * but the value 0 is generated only 2<sup>64</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise equidistributed.
+ *
+ * <p>Instances {@code Xoroshiro128Plus} are <em>not</em> thread-safe.
+ * They are designed to be used so that each thread as its own instance.
+ * The methods {@link #jump} and {@link #leap} and {@link #jumps} and {@link #leaps}
+ * can be used to construct new instances of {@code Xoroshiro128Plus} that traverse
+ * other parts of the state cycle.
+ *
+ * <p>Instances of {@code Xoroshiro128Plus} 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 final class Xoroshiro128Plus implements LeapableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * This is an implementation of the xoroshiro128+ algorithm written
+ * in 2016 by David Blackman and Sebastiano Vigna (vigna@acm.org),
+ * and updated with improved parameters in 2018.
+ * See http://xoshiro.di.unimi.it and these two papers:
+ *
+ * Sebastiano Vigna. 2016. An Experimental Exploration of Marsaglia's
+ * xorshift Generators, Scrambled. ACM Transactions on Mathematical
+ * Software 42, 4, Article 30 (June 2016), 23 pages.
+ * https://doi.org/10.1145/2845077
+ *
+ * David Blackman and Sebastiano Vigna. 2018. Scrambled Linear
+ * Pseudorandom Number Generators. Computing Research Repository (CoRR).
+ * http://arxiv.org/abs/1805.01407
+ *
+ * The jump operation moves the current generator forward by 2*64
+ * steps; this has the same effect as calling nextLong() 2**64
+ * times, but is much faster. Similarly, the leap operation moves
+ * the current generator forward by 2*96 steps; this has the same
+ * effect as calling nextLong() 2**96 times, but is much faster.
+ * The copy method may be used to make a copy of the current
+ * generator. Thus one may repeatedly and cumulatively copy and
+ * jump to produce a sequence of generators whose states are well
+ * spaced apart along the overall state cycle (indeed, the jumps()
+ * and leaps() methods each produce a stream of such generators).
+ * The generators can then be parceled out to other threads.
+ *
+ * File organization: First the non-public methods that constitute the
+ * main algorithm, then the public methods. Note that many methods are
+ * defined by classes {@code AbstractJumpableRng} and {@code AbstractRng}.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is 2**128 - 1.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE);
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The per-instance state.
+ * At least one of the two fields x0 and x1 must be nonzero.
+ */
+ private long x0, x1;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public Xoroshiro128Plus(long x0, long x1) {
+ this.x0 = x0;
+ this.x1 = x1;
+ // If x0 and x1 are both zero, we must choose nonzero values.
+ if ((x0 | x1) == 0) {
+ // At least one of the two values generated here will be nonzero.
+ this.x0 = RngSupport.mixStafford13(x0 += RngSupport.GOLDEN_RATIO_64);
+ this.x1 = (x0 += RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code Xoroshiro128Plus} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code Xoroshiro128Plus} created with the same seed in the same
+ * program generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public Xoroshiro128Plus(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The x values are then filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixStafford13(seed ^= RngSupport.SILVER_RATIO_64),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code Xoroshiro128Plus} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public Xoroshiro128Plus() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code Xoroshiro128Plus} using the specified array of
+ * initial seed bytes. Instances of {@code Xoroshiro128Plus} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public Xoroshiro128Plus(byte[] seed) {
+ // Convert the seed to 2 long values, which are not both zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 2, 2);
+ long x0 = data[0], x1 = data[1];
+ this.x0 = x0;
+ this.x1 = x1;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ public Xoroshiro128Plus copy() { return new Xoroshiro128Plus(x0, x1); }
+
+/*
+
+To the extent possible under law, the author has dedicated all copyright
+and related and neighboring rights to this software to the public domain
+worldwide. This software is distributed without any warranty.
+
+See <http://creativecommons.org/publicdomain/zero/1.0/>. */
+
+/* This is the successor to xorshift128+. It is the fastest full-period
+ generator passing BigCrush without systematic failures, but due to the
+ relatively short period it is acceptable only for applications with a
+ mild amount of parallelism; otherwise, use a xorshift1024* generator.
+
+ Beside passing BigCrush, this generator passes the PractRand test suite
+ up to (and included) 16TB, with the exception of binary rank tests,
+ which fail due to the lowest bit being an LFSR; all other bits pass all
+ tests. We suggest to use a sign test to extract a random Boolean value.
+
+ Note that the generator uses a simulated rotate operation, which most C
+ compilers will turn into a single instruction. In Java, you can use
+ Long.rotateLeft(). In languages that do not make low-level rotation
+ instructions accessible xorshift128+ could be faster.
+
+ The state must be seeded so that it is not everywhere zero. If you have
+ a 64-bit seed, we suggest to seed a splitmix64 generator and use its
+ output to fill s. */
+
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+ public long nextLong() {
+ final long s0 = x0;
+ long s1 = x1;
+ final long z = s0 + s1;
+
+ s1 ^= s0;
+ x0 = Long.rotateLeft(s0, 24) ^ s1 ^ (s1 << 16); // a, b
+ x1 = Long.rotateLeft(s1, 37); // c
+
+ return z;
+ }
+
+ public BigInteger period() { return thePeriod; }
+
+ public double defaultJumpDistance() { return 0x1.0p64; }
+
+ public double defaultLeapDistance() { return 0x1.0p96; }
+
+ private static final long[] JUMP_TABLE = { 0xdf900294d8f554a5L, 0x170865df4b3201fcL };
+
+ private static final long[] LEAP_TABLE = { 0xd2a98b26625eee7bL, 0xdddf9b1090aa7ac1L };
+
+/* This is the jump function for the generator. It is equivalent
+ to 2**64 calls to nextLong(); it can be used to generate 2**64
+ non-overlapping subsequences for parallel computations. */
+
+ public void jump() { jumpAlgorithm(JUMP_TABLE); }
+
+/* This is the long-jump function for the generator. It is equivalent to
+ 2**96 calls to next(); it can be used to generate 2**32 starting points,
+ from each of which jump() will generate 2**32 non-overlapping
+ subsequences for parallel distributed computations. */
+
+ public void leap() { jumpAlgorithm(LEAP_TABLE); }
+
+ private void jumpAlgorithm(long[] table) {
+ long s0 = 0, s1 = 0;
+ for (int i = 0; i < table.length; i++) {
+ for (int b = 0; b < 64; b++) {
+ if ((table[i] & (1L << b)) != 0) {
+ s0 ^= x0;
+ s1 ^= x1;
+ }
+ nextLong();
+ }
+ x0 = s0;
+ x1 = s1;
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/Xoroshiro128StarStar.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,292 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code Xoroshiro128StarStar} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.LeapableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new {@code Xoroshiro128StarStar} objects
+ * by "jumping" or "leaping".
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ *
+ * <p>The class {@code Xoroshiro128StarStar} uses the {@code xoroshiro128} algorithm,
+ * version 1.0 (parameters 24, 16, 37), with the "**" scrambler (a mixing function).
+ * Its state consists of two {@code long} fields {@code x0} and {@code x1},
+ * which can take on any values provided that they are not both zero.
+ * The period of this generator is 2<sup>128</sup>-1.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are equidistributed.
+ * To be precise, over the course of the cycle of length 2<sup>128</sup>-1,
+ * each nonzero {@code long} value is generated 2<sup>64</sup> times,
+ * but the value 0 is generated only 2<sup>64</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise equidistributed.
+ *
+ * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 2-equidistributed.
+ * To be precise: consider the (overlapping) length-2 subsequences of the cycle of 64-bit
+ * values produced by {@code nextLong()} (assuming no other methods are called that would
+ * affect the state). There are 2<sup>128</sup>-1 such subsequences, and each subsequence,
+ * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values. Of those
+ * 2<sup>128</sup> subsequence values, each one is generated exactly once over the course
+ * of the entire cycle, except that the subsequence (0, 0) never appears.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise 2-equidistributed, but note that that the subsequence (0, 0)
+ * can also appear (but occurring somewhat less frequently than all other subsequences),
+ * because the values produced by those methods have fewer than 64 randomly chosen bits.
+ *
+ * <p>Instances {@code Xoroshiro128StarStar} are <em>not</em> thread-safe.
+ * They are designed to be used so that each thread as its own instance.
+ * The methods {@link #jump} and {@link #leap} and {@link #jumps} and {@link #leaps}
+ * can be used to construct new instances of {@code Xoroshiro128StarStar} that traverse
+ * other parts of the state cycle.
+ *
+ * <p>Instances of {@code Xoroshiro128StarStar} 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 final class Xoroshiro128StarStar implements LeapableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * This is an implementation of the xoroshiro128** algorithm written
+ * in 2016 by David Blackman and Sebastiano Vigna (vigna@acm.org),
+ * and updated with improved parameters in 2018.
+ * See http://xoshiro.di.unimi.it and these two papers:
+ *
+ * Sebastiano Vigna. 2016. An Experimental Exploration of Marsaglia's
+ * xorshift Generators, Scrambled. ACM Transactions on Mathematical
+ * Software 42, 4, Article 30 (June 2016), 23 pages.
+ * https://doi.org/10.1145/2845077
+ *
+ * David Blackman and Sebastiano Vigna. 2018. Scrambled Linear
+ * Pseudorandom Number Generators. Computing Research Repository (CoRR).
+ * http://arxiv.org/abs/1805.01407
+ *
+ * The jump operation moves the current generator forward by 2*64
+ * steps; this has the same effect as calling nextLong() 2**64
+ * times, but is much faster. Similarly, the leap operation moves
+ * the current generator forward by 2*96 steps; this has the same
+ * effect as calling nextLong() 2**96 times, but is much faster.
+ * The copy method may be used to make a copy of the current
+ * generator. Thus one may repeatedly and cumulatively copy and
+ * jump to produce a sequence of generators whose states are well
+ * spaced apart along the overall state cycle (indeed, the jumps()
+ * and leaps() methods each produce a stream of such generators).
+ * The generators can then be parceled out to other threads.
+ *
+ * File organization: First the non-public methods that constitute the
+ * main algorithm, then the public methods. Note that many methods are
+ * defined by classes {@code AbstractJumpableRng} and {@code AbstractRng}.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is 2**128 - 1.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE);
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The per-instance state.
+ * At least one of the two fields x0 and x1 must be nonzero.
+ */
+ private long x0, x1;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public Xoroshiro128StarStar(long x0, long x1) {
+ this.x0 = x0;
+ this.x1 = x1;
+ // If x0 and x1 are both zero, we must choose nonzero values.
+ if ((x0 | x1) == 0) {
+ // At least one of the two values generated here will be nonzero.
+ this.x0 = RngSupport.mixStafford13(x0 += RngSupport.GOLDEN_RATIO_64);
+ this.x1 = (x0 += RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code Xoroshiro128StarStar} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code Xoroshiro128StarStar} created with the same seed in the same
+ * program generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public Xoroshiro128StarStar(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The x values are then filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixStafford13(seed ^= RngSupport.SILVER_RATIO_64),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code Xoroshiro128StarStar} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public Xoroshiro128StarStar() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code Xoroshiro128StarStar} using the specified array of
+ * initial seed bytes. Instances of {@code Xoroshiro128StarStar} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public Xoroshiro128StarStar(byte[] seed) {
+ // Convert the seed to 2 long values, which are not both zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 2, 2);
+ long x0 = data[0], x1 = data[1];
+ this.x0 = x0;
+ this.x1 = x1;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ public Xoroshiro128StarStar copy() { return new Xoroshiro128StarStar(x0, x1); }
+
+/*
+
+To the extent possible under law, the author has dedicated all copyright
+and related and neighboring rights to this software to the public domain
+worldwide. This software is distributed without any warranty.
+
+See <http://creativecommons.org/publicdomain/zero/1.0/>. */
+
+/* This is the successor to xorshift128+. It is the fastest full-period
+ generator passing BigCrush without systematic failures, but due to the
+ relatively short period it is acceptable only for applications with a
+ mild amount of parallelism; otherwise, use a xorshift1024* generator.
+
+ Beside passing BigCrush, this generator passes the PractRand test suite
+ up to (and included) 16TB, with the exception of binary rank tests,
+ which fail due to the lowest bit being an LFSR; all other bits pass all
+ tests. We suggest to use a sign test to extract a random Boolean value.
+
+ Note that the generator uses a simulated rotate operation, which most C
+ compilers will turn into a single instruction. In Java, you can use
+ Long.rotateLeft(). In languages that do not make low-level rotation
+ instructions accessible xorshift128+ could be faster.
+
+ The state must be seeded so that it is not everywhere zero. If you have
+ a 64-bit seed, we suggest to seed a splitmix64 generator and use its
+ output to fill s. */
+
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+ public long nextLong() {
+ final long s0 = x0;
+ long s1 = x1;
+ final long z = s0;
+
+ s1 ^= s0;
+ x0 = Long.rotateLeft(s0, 24) ^ s1 ^ (s1 << 16); // a, b
+ x1 = Long.rotateLeft(s1, 37); // c
+
+ return Long.rotateLeft(z * 5, 7) * 9; // "starstar" mixing function
+ }
+
+ public BigInteger period() { return thePeriod; }
+
+ public double defaultJumpDistance() { return 0x1.0p64; }
+
+ public double defaultLeapDistance() { return 0x1.0p96; }
+
+ private static final long[] JUMP_TABLE = { 0xdf900294d8f554a5L, 0x170865df4b3201fcL };
+
+ private static final long[] LEAP_TABLE = { 0xd2a98b26625eee7bL, 0xdddf9b1090aa7ac1L };
+
+/* This is the jump function for the generator. It is equivalent
+ to 2**64 calls to nextLong(); it can be used to generate 2**64
+ non-overlapping subsequences for parallel computations. */
+
+ public void jump() { jumpAlgorithm(JUMP_TABLE); }
+
+/* This is the long-jump function for the generator. It is equivalent to
+ 2**96 calls to next(); it can be used to generate 2**32 starting points,
+ from each of which jump() will generate 2**32 non-overlapping
+ subsequences for parallel distributed computations. */
+
+ public void leap() { jumpAlgorithm(LEAP_TABLE); }
+
+ private void jumpAlgorithm(long[] table) {
+ long s0 = 0, s1 = 0;
+ for (int i = 0; i < table.length; i++) {
+ for (int b = 0; b < 64; b++) {
+ if ((table[i] & (1L << b)) != 0) {
+ s0 ^= x0;
+ s1 ^= x1;
+ }
+ nextLong();
+ }
+ x0 = s0;
+ x1 = s1;
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/newrandom/Xoshiro256StarStar.java Thu May 23 16:45:56 2019 -0400
@@ -0,0 +1,281 @@
+/*
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+
+// package java.util;
+
+import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * A generator of uniform pseudorandom values applicable for use in
+ * (among other contexts) isolated parallel computations that may
+ * generate subtasks. Class {@code Xoshiro256StarStar} implements
+ * interfaces {@link java.util.Rng} and {@link java.util.LeapableRng},
+ * and therefore supports methods for producing pseudorandomly chosen
+ * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
+ * as well as creating new {@code Xoshiro256StarStar} objects
+ * by "jumping" or "leaping".
+ *
+ * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * that measure independence and uniformity properties of random number generators.
+ * (Most recently validated with
+ * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
+ * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
+ * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
+ * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
+ * 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.
+ *
+ * <p>The class {@code Xoshiro256StarStar} uses the {@code xoshiro256} algorithm,
+ * version 1.0 (parameters 17, 45), with the "**" scrambler (a mixing function).
+ * Its state consists of four {@code long} fields {@code x0}, {@code x1}, {@code x2},
+ * and {@code x3}, which can take on any values provided that they are not all zero.
+ * The period of this generator is 2<sup>256</sup>-1.
+ *
+ * <p>The 64-bit values produced by the {@code nextLong()} method are equidistributed.
+ * To be precise, over the course of the cycle of length 2<sup>256</sup>-1,
+ * each nonzero {@code long} value is generated 2<sup>192</sup> times,
+ * but the value 0 is generated only 2<sup>192</sup>-1 times.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise equidistributed.
+ *
+ * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 4-equidistributed.
+ * To be precise: consider the (overlapping) length-4 subsequences of the cycle of 64-bit
+ * values produced by {@code nextLong()} (assuming no other methods are called that would
+ * affect the state). There are 2<sup>256</sup>-1 such subsequences, and each subsequence,
+ * which consists of 4 64-bit values, can have one of 2<sup>256</sup> values. Of those
+ * 2<sup>256</sup> subsequence values, each one is generated exactly once over the course
+ * of the entire cycle, except that the subsequence (0, 0, 0, 0) never appears.
+ * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
+ * methods are likewise 4-equidistributed, but note that that the subsequence (0, 0, 0, 0)
+ * can also appear (but occurring somewhat less frequently than all other subsequences),
+ * because the values produced by those methods have fewer than 64 randomly chosen bits.
+ *
+ * <p>Instances {@code Xoshiro256StarStar} are <em>not</em> thread-safe.
+ * They are designed to be used so that each thread as its own instance.
+ * The methods {@link #jump} and {@link #leap} and {@link #jumps} and {@link #leaps}
+ * can be used to construct new instances of {@code Xoshiro256StarStar} that traverse
+ * other parts of the state cycle.
+ *
+ * <p>Instances of {@code Xoshiro256StarStar} 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
+ * @since 1.9
+ */
+public final class Xoshiro256StarStar implements LeapableRng {
+
+ /*
+ * Implementation Overview.
+ *
+ * This is an implementation of the xoroshiro128** algorithm written
+ * in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org).
+ * See http://xoshiro.di.unimi.it and these two papers:
+ *
+ * Sebastiano Vigna. 2016. An Experimental Exploration of Marsaglia's
+ * xorshift Generators, Scrambled. ACM Transactions on Mathematical
+ * Software 42, 4, Article 30 (June 2016), 23 pages.
+ * https://doi.org/10.1145/2845077
+ *
+ * David Blackman and Sebastiano Vigna. 2018. Scrambled Linear
+ * Pseudorandom Number Generators. Computing Research Repository (CoRR).
+ * http://arxiv.org/abs/1805.01407
+ *
+ * The jump operation moves the current generator forward by 2*128
+ * steps; this has the same effect as calling nextLong() 2**128
+ * times, but is much faster. Similarly, the leap operation moves
+ * the current generator forward by 2*192 steps; this has the same
+ * effect as calling nextLong() 2**192 times, but is much faster.
+ * The copy method may be used to make a copy of the current
+ * generator. Thus one may repeatedly and cumulatively copy and
+ * jump to produce a sequence of generators whose states are well
+ * spaced apart along the overall state cycle (indeed, the jumps()
+ * and leaps() methods each produce a stream of such generators).
+ * The generators can then be parceled out to other threads.
+ *
+ * File organization: First static fields, then instance
+ * fields, then constructors, then instance methods.
+ */
+
+ /* ---------------- static fields ---------------- */
+
+ /**
+ * The seed generator for default constructors.
+ */
+ private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+
+ /*
+ * The period of this generator, which is 2**256 - 1.
+ */
+ private static final BigInteger thePeriod =
+ BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE);
+
+ /* ---------------- instance fields ---------------- */
+
+ /**
+ * The per-instance state.
+ * At least one of the four fields x0, x1, x2, and x3 must be nonzero.
+ */
+ private long x0, x1, x2, x3;
+
+ /* ---------------- constructors ---------------- */
+
+ /**
+ * Basic constructor that initializes all fields from parameters.
+ * It then adjusts the field values if necessary to ensure that
+ * all constraints on the values of fields are met.
+ */
+ public Xoshiro256StarStar(long x0, long x1, long x2, long x3) {
+ this.x0 = x0;
+ this.x1 = x1;
+ this.x2 = x2;
+ this.x3 = x3;
+ // If x0, x1, x2, and x3 are all zero, we must choose nonzero values.
+ if ((x0 | x1 | x2 | x3) == 0) {
+ // At least three of the four values generated here will be nonzero.
+ this.x0 = RngSupport.mixStafford13(x0 += RngSupport.GOLDEN_RATIO_64);
+ this.x1 = (x0 += RngSupport.GOLDEN_RATIO_64);
+ this.x2 = (x0 += RngSupport.GOLDEN_RATIO_64);
+ this.x3 = (x0 += RngSupport.GOLDEN_RATIO_64);
+ }
+ }
+
+ /**
+ * Creates a new instance of {@code Xoshiro256StarStar} using the
+ * specified {@code long} value as the initial seed. Instances of
+ * {@code Xoshiro256StarStar} created with the same seed in the same
+ * program generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public Xoshiro256StarStar(long seed) {
+ // Using a value with irregularly spaced 1-bits to xor the seed
+ // argument tends to improve "pedestrian" seeds such as 0 or
+ // other small integers. We may as well use SILVER_RATIO_64.
+ //
+ // The x values are then filled in as if by a SplitMix PRNG with
+ // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+ this(RngSupport.mixStafford13(seed ^= RngSupport.SILVER_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
+ RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code Xoshiro256StarStar} that is likely to
+ * generate sequences of values that are statistically independent
+ * of those of any other instances in the current program execution,
+ * but may, and typically does, vary across program invocations.
+ */
+ public Xoshiro256StarStar() {
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
+ }
+
+ /**
+ * Creates a new instance of {@code Xoshiro256StarStar} using the specified array of
+ * initial seed bytes. Instances of {@code Xoshiro256StarStar} created with the same
+ * seed array in the same program execution generate identical sequences of values.
+ *
+ * @param seed the initial seed
+ */
+ public Xoshiro256StarStar(byte[] seed) {
+ // Convert the seed to 4 long values, which are not all zero.
+ long[] data = RngSupport.convertSeedBytesToLongs(seed, 4, 4);
+ long x0 = data[0], x1 = data[1], x2 = data[2], x3 = data[3];
+ this.x0 = x0;
+ this.x1 = x1;
+ this.x2 = x2;
+ this.x3 = x3;
+ }
+
+ /* ---------------- public methods ---------------- */
+
+ public Xoshiro256StarStar copy() { return new Xoshiro256StarStar(x0, x1, x2, x3); }
+
+ /**
+ * Returns a pseudorandom {@code long} value.
+ *
+ * @return a pseudorandom {@code long} value
+ */
+
+ public long nextLong() {
+ final long z = x0;
+ long q0 = x0, q1 = x1, q2 = x2, q3 = x3;
+ { long t = q1 << 17; q2 ^= q0; q3 ^= q1; q1 ^= q2; q0 ^= q3; q2 ^= t; q3 = Long.rotateLeft(q3, 45); } // xoshiro256 1.0
+ x0 = q0; x1 = q1; x2 = q2; x3 = q3;
+ return Long.rotateLeft(z * 5, 7) * 9; // "starstar" mixing function
+ }
+
+ public BigInteger period() { return thePeriod; }
+
+
+ public double defaultJumpDistance() { return 0x1.0p64; }
+ public double defaultLeapDistance() { return 0x1.0p96; }
+
+ private static final long[] JUMP_TABLE = {
+ 0x180ec6d33cfd0abaL, 0xd5a61266f0c9392cL, 0xa9582618e03fc9aaL, 0x39abdc4529b1661cL };
+
+ private static final long[] LEAP_TABLE = {
+ 0x76e15d3efefdcbbfL, 0xc5004e441c522fb3L, 0x77710069854ee241L, 0x39109bb02acbe635L };
+
+/* This is the jump function for the generator. It is equivalent
+ to 2**128 calls to next(); it can be used to generate 2**128
+ non-overlapping subsequences for parallel computations. */
+
+ public void jump() { jumpAlgorithm(JUMP_TABLE); }
+
+/* This is the long-jump function for the generator. It is equivalent to
+ 2**192 calls to next(); it can be used to generate 2**64 starting points,
+ from each of which jump() will generate 2**64 non-overlapping
+ subsequences for parallel distributed computations. */
+
+ public void leap() { jumpAlgorithm(LEAP_TABLE); }
+
+ private void jumpAlgorithm(long[] table) {
+ long s0 = 0, s1 = 0, s2 = 0, s3 = 0;
+ for (int i = 0; i < table.length; i++) {
+ for (int b = 0; b < 64; b++) {
+ if ((table[i] & (1L << b)) != 0) {
+ s0 ^= x0;
+ s1 ^= x1;
+ s2 ^= x2;
+ s3 ^= x3;
+ }
+ nextLong();
+ }
+ x0 = s0;
+ x1 = s1;
+ x2 = s2;
+ x3 = s3;
+ }
+ }
+
+}