--- a/src/java.base/share/classes/java/util/random/Xoroshiro128StarStar.java Thu Jun 27 18:02:51 2019 -0300
+++ b/src/java.base/share/classes/java/util/random/Xoroshiro128StarStar.java Thu Jun 27 18:30:27 2019 -0300
@@ -22,7 +22,8 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
-package java.util;
+
+package java.util.random;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicLong;
@@ -30,30 +31,30 @@
/**
* 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},
+ * generate subtasks. Class {@link Xoroshiro128StarStar} implements
+ * interfaces {@link RandomNumberGenerator} and {@link 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
+ * as well as creating new {@link Xoroshiro128StarStar} objects
* by "jumping" or "leaping".
- *
- * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
+ * <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,
+ * <p>
+ * The class {@link 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.
+ * <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.
+ * <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,
@@ -64,25 +65,23 @@
* 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.
+ * <p>
+ * Instances {@link 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
+ * can be used to construct new instances of {@link Xoroshiro128StarStar} that traverse
* other parts of the state cycle.
- *
- * <p>Instances of {@code Xoroshiro128StarStar} are not cryptographically
+ * <p>
+ * Instances of {@link 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
+ * @since 14
*/
-public final class Xoroshiro128StarStar implements LeapableRng {
+public final class Xoroshiro128StarStar implements LeapableRNG {
/*
* Implementation Overview.
@@ -115,7 +114,7 @@
*
* 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}.
+ * defined by classes {@link AbstractJumpableRNG} and {@link AbstractRNG}.
*/
/* ---------------- static fields ---------------- */
@@ -123,13 +122,13 @@
/**
* The seed generator for default constructors.
*/
- private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
+ private static final AtomicLong DEFAULT_GEN = 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);
+ private static final BigInteger PERIOD =
+ BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE);
/* ---------------- instance fields ---------------- */
@@ -150,92 +149,91 @@
* @param x1 second word of the initial state
*/
public Xoroshiro128StarStar(long x0, long x1) {
- this.x0 = x0;
+ this.x0 = x0;
this.x1 = x1;
- // If x0 and x1 are both zero, we must choose nonzero values.
+ // 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);
- }
+ // 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
+ * Creates a new instance of {@link Xoroshiro128StarStar} using the
* specified {@code long} value as the initial seed. Instances of
- * {@code Xoroshiro128StarStar} created with the same seed in the same
+ * {@link 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));
+ // 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
+ * Creates a new instance of {@link 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));
+ // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
+ this(DEFAULT_GEN.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
+ * Creates a new instance of {@link Xoroshiro128StarStar} using the specified array of
+ * initial seed bytes. Instances of {@link 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];
+ // 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.
+ /*
+ * 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.
+ * <p>
+ * See <http://creativecommons.org/publicdomain/zero/1.0/>.
+ */
- 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. */
-
+ /*
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * 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.
@@ -243,52 +241,62 @@
* @return a pseudorandom {@code long} value
*/
public long nextLong() {
- final long s0 = x0;
- long s1 = x1;
- final long z = s0;
+ 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
+ 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 BigInteger period() {
+ return PERIOD;
+ }
- public double defaultJumpDistance() { return 0x1.0p64; }
+ public double defaultJumpDistance() {
+ return 0x1.0p64;
+ }
- public double defaultLeapDistance() { return 0x1.0p96; }
+ 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. */
+ /**
+ * 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);
+ }
- public void leap() { jumpAlgorithm(LEAP_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;
- }
+ 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;
+ }
}
}