src/java.base/share/classes/java/util/random/L64X128MixRandom.java
branchJDK-8193209-branch
changeset 57437 f02ffcb61dce
parent 57436 b0c958c0e6c6
child 57547 56cbdc3ea079
equal deleted inserted replaced
57436:b0c958c0e6c6 57437:f02ffcb61dce
    20  *
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 package java.util;
    25 
       
    26 package java.util.random;
    26 
    27 
    27 import java.math.BigInteger;
    28 import java.math.BigInteger;
    28 import java.util.concurrent.atomic.AtomicLong;
    29 import java.util.concurrent.atomic.AtomicLong;
    29 
    30 
    30 /**
    31 /**
    31  * A generator of uniform pseudorandom values applicable for use in
    32  * A generator of uniform pseudorandom values applicable for use in
    32  * (among other contexts) isolated parallel computations that may
    33  * (among other contexts) isolated parallel computations that may
    33  * generate subtasks.  Class {@code L64X128MixRandom} implements
    34  * generate subtasks.  Class {@link L64X128MixRandom} implements
    34  * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
    35  * interfaces {@link RandomNumberGenerator} and {@link SplittableRNG},
    35  * and therefore supports methods for producing pseudorandomly chosen
    36  * and therefore supports methods for producing pseudorandomly chosen
    36  * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
    37  * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
    37  * as well as creating new split-off {@code L64X128MixRandom} objects,
    38  * as well as creating new split-off {@link L64X128MixRandom} objects,
    38  * with similar usages as for class {@link java.util.SplittableRandom}.
    39  * with similar usages as for class {@link java.util.SplittableRandom}.
    39  *
    40  * <p>
    40  * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
    41  * Series of generated values pass the TestU01 BigCrush and PractRand test suites
    41  * that measure independence and uniformity properties of random number generators.
    42  * that measure independence and uniformity properties of random number generators.
    42  * (Most recently validated with
    43  * (Most recently validated with
    43  * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
    44  * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
    44  * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
    45  * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
    45  * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
    46  * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
    46  * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
    47  * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
    47  * These tests validate only the methods for certain
    48  * These tests validate only the methods for certain
    48  * types and ranges, but similar properties are expected to hold, at
    49  * types and ranges, but similar properties are expected to hold, at
    49  * least approximately, for others as well.
    50  * least approximately, for others as well.
    50  *
    51  * <p>
    51  * <p>{@code L64X128MixRandom} is a specific member of the LXM family of algorithms
    52  * {@link L64X128MixRandom} is a specific member of the LXM family of algorithms
    52  * for pseudorandom number generators.  Every LXM generator consists of two
    53  * for pseudorandom number generators.  Every LXM generator consists of two
    53  * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
    54  * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
    54  * an Xorshift generator.  Each output of an LXM generator is the sum of one
    55  * an Xorshift generator.  Each output of an LXM generator is the sum of one
    55  * output from each subgenerator, possibly processed by a final mixing function
    56  * output from each subgenerator, possibly processed by a final mixing function
    56  * (and {@code L64X128MixRandom} does use a mixing function).
    57  * (and {@link L64X128MixRandom} does use a mixing function).
    57  *
    58  * <p>
    58  * <p>The LCG subgenerator for {@code L64X128MixRandom} has an update step of the
    59  * The LCG subgenerator for {@link L64X128MixRandom} has an update step of the
    59  * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
    60  * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
    60  * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m}
    61  * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m}
    61  * is fixed (the same for all instances of {@code L64X128MixRandom}}) and the addend
    62  * is fixed (the same for all instances of {@link L64X128MixRandom}}) and the addend
    62  * {@code a} is a parameter (a final field of the instance).  The parameter
    63  * {@code a} is a parameter (a final field of the instance).  The parameter
    63  * {@code a} is required to be odd (this allows the LCG to have the maximal
    64  * {@code a} is required to be odd (this allows the LCG to have the maximal
    64  * period, namely 2<sup>64</sup>); therefore there are 2<sup>63</sup> distinct choices
    65  * period, namely 2<sup>64</sup>); therefore there are 2<sup>63</sup> distinct choices
    65  * of parameter.
    66  * of parameter.
    66  *
    67  * <p>
    67  * <p>The Xorshift subgenerator for {@code L64X128MixRandom} is the {@code xoroshiro128} algorithm,
    68  * The Xorshift subgenerator for {@link L64X128MixRandom} is the {@code xoroshiro128} algorithm,
    68  * version 1.0 (parameters 24, 16, 37), without any final scrambler such as "+" or "**".
    69  * version 1.0 (parameters 24, 16, 37), without any final scrambler such as "+" or "**".
    69  * Its state consists of two {@code long} fields {@code x0} and {@code x1},
    70  * Its state consists of two {@code long} fields {@code x0} and {@code x1},
    70  * which can take on any values provided that they are not both zero.
    71  * which can take on any values provided that they are not both zero.
    71  * The period of this subgenerator is 2<sup>128</sup>-1.
    72  * The period of this subgenerator is 2<sup>128</sup>-1.
    72  * 
    73  * <p>
    73  * <p> The mixing function for {@code L64X128MixRandom} is the 64-bit "starstar(5,7,9)" function.
    74  * The mixing function for {@link L64X128MixRandom} is the 64-bit "starstar(5,7,9)" function.
    74  *
    75  * <p>
    75  * <p> Because the periods 2<sup>64</sup> and 2<sup>128</sup>-1 of the two subgenerators
    76  * Because the periods 2<sup>64</sup> and 2<sup>128</sup>-1 of the two subgenerators
    76  * are relatively prime, the <em>period</em> of any single {@code L64X128MixRandom} object 
    77  * are relatively prime, the <em>period</em> of any single {@link L64X128MixRandom} object
    77  * (the length of the series of generated 64-bit values before it repeats) is the product
    78  * (the length of the series of generated 64-bit values before it repeats) is the product
    78  * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>128</sup>-1),
    79  * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>128</sup>-1),
    79  * which is just slightly smaller than 2<sup>192</sup>.  Moreover, if two distinct
    80  * which is just slightly smaller than 2<sup>192</sup>.  Moreover, if two distinct
    80  * {@code L64X128MixRandom} objects have different {@code a} parameters, then their
    81  * {@link L64X128MixRandom} objects have different {@code a} parameters, then their
    81  * cycles of produced values will be different.
    82  * cycles of produced values will be different.
    82  *
    83  * <p>
    83  * <p>The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
    84  * The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
    84  * For any specific instance of {@code L64X128MixRandom}, over the course of its cycle each
    85  * For any specific instance of {@link L64X128MixRandom}, over the course of its cycle each
    85  * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>128</sup>-1 times.
    86  * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>128</sup>-1 times.
    86  * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
    87  * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
    87  * methods are likewise exactly equidistributed.
    88  * methods are likewise exactly equidistributed.
    88  *
    89  * <p>
    89  * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 2-equidistributed.
    90  * In fact, the 64-bit values produced by the {@code nextLong()} method are 2-equidistributed.
    90  * To be precise: for any specific instance of {@code L64X128MixRandom}, consider
    91  * To be precise: for any specific instance of {@link L64X128MixRandom}, consider
    91  * the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by
    92  * the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by
    92  * {@code nextLong()} (assuming no other methods are called that would affect the state).
    93  * {@code nextLong()} (assuming no other methods are called that would affect the state).
    93  * There are 2<sup>64</sup>(2<sup>128</sup>-1) such subsequences, and each subsequence,
    94  * There are 2<sup>64</sup>(2<sup>128</sup>-1) such subsequences, and each subsequence,
    94  * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values. Of those
    95  * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values. Of those
    95  * 2<sup>128</sup> subsequence values, nearly all of them (2<sup>128</sup>-2<sup>64</sup>)
    96  * 2<sup>128</sup> subsequence values, nearly all of them (2<sup>128</sup>-2<sup>64</sup>)
    96  * occur 2<sup>64</sup> times over the course of the entire cycle, and the other
    97  * occur 2<sup>64</sup> times over the course of the entire cycle, and the other
    97  * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times.  So the ratio
    98  * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times.  So the ratio
    98  * of the probability of getting one of the less common subsequence values and the
    99  * of the probability of getting one of the less common subsequence values and the
    99  * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>.
   100  * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>.
   100  * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from
   101  * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from
   101  * one instance of {@code L64X128MixRandom} to another, as a function of the additive
   102  * one instance of {@link L64X128MixRandom} to another, as a function of the additive
   102  * parameter of the LCG.)  The values produced by the {@code nextInt()}, {@code nextFloat()},
   103  * parameter of the LCG.)  The values produced by the {@code nextInt()}, {@code nextFloat()},
   103  * and {@code nextDouble()} methods are likewise 2-equidistributed.
   104  * and {@code nextDouble()} methods are likewise 2-equidistributed.
   104  *
   105  * <p>
   105  * <p>Method {@link #split} constructs and returns a new {@code L64X128MixRandom}
   106  * Method {@link #split} constructs and returns a new {@link L64X128MixRandom}
   106  * instance that shares no mutable state with the current instance. However, with
   107  * instance that shares no mutable state with the current instance. However, with
   107  * very high probability, the values collectively generated by the two objects
   108  * very high probability, the values collectively generated by the two objects
   108  * have the same statistical properties as if the same quantity of values were
   109  * have the same statistical properties as if the same quantity of values were
   109  * generated by a single thread using a single {@code L64X128MixRandom} object.
   110  * generated by a single thread using a single {@link L64X128MixRandom} object.
   110  * This is because, with high probability, distinct {@code L64X128MixRandom} objects
   111  * This is because, with high probability, distinct {@link L64X128MixRandom} objects
   111  * have distinct {@code a} parameters and therefore use distinct members of the
   112  * have distinct {@code a} parameters and therefore use distinct members of the
   112  * algorithmic family; and even if their {@code a} parameters are the same, with
   113  * algorithmic family; and even if their {@code a} parameters are the same, with
   113  * very high probability they will traverse different parts of their common state
   114  * very high probability they will traverse different parts of their common state
   114  * cycle.
   115  * cycle.
   115  *
   116  * <p>
   116  * <p>As with {@link java.util.SplittableRandom}, instances of
   117  * As with {@link java.util.SplittableRandom}, instances of
   117  * {@code L64X128MixRandom} are <em>not</em> thread-safe.
   118  * {@link L64X128MixRandom} are <em>not</em> thread-safe.
   118  * They are designed to be split, not shared, across threads. For
   119  * They are designed to be split, not shared, across threads. For
   119  * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
   120  * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
   120  * computation using random numbers might include a construction
   121  * computation using random numbers might include a construction
   121  * of the form {@code new Subtask(someL64X128MixRandom.split()).fork()}.
   122  * of the form {@code new Subtask(someL64X128MixRandom.split()).fork()}.
   122  *
   123  * <p>
   123  * <p>This class provides additional methods for generating random
   124  * This class provides additional methods for generating random
   124  * streams, that employ the above techniques when used in
   125  * streams, that employ the above techniques when used in
   125  * {@code stream.parallel()} mode.
   126  * {@code stream.parallel()} mode.
   126  *
   127  * <p>
   127  * <p>Instances of {@code L64X128MixRandom} are not cryptographically
   128  * Instances of {@link L64X128MixRandom} are not cryptographically
   128  * secure.  Consider instead using {@link java.security.SecureRandom}
   129  * secure.  Consider instead using {@link java.security.SecureRandom}
   129  * in security-sensitive applications. Additionally,
   130  * in security-sensitive applications. Additionally,
   130  * default-constructed instances do not use a cryptographically random
   131  * default-constructed instances do not use a cryptographically random
   131  * seed unless the {@linkplain System#getProperty system property}
   132  * seed unless the {@linkplain System#getProperty system property}
   132  * {@code java.util.secureRandomSeed} is set to {@code true}.
   133  * {@code java.util.secureRandomSeed} is set to {@code true}.
   133  *
   134  *
   134  * @author  Guy Steele
   135  * @since 14
   135  * @since   1.9
       
   136  */
   136  */
   137 public final class L64X128MixRandom extends AbstractSplittableRng {
   137 public final class L64X128MixRandom extends AbstractSplittableRNG {
   138 
   138 
   139     /*
   139     /*
   140      * Implementation Overview.
   140      * Implementation Overview.
   141      *
   141      *
   142      * The split operation uses the current generator to choose four new 64-bit
   142      * The split operation uses the current generator to choose four new 64-bit
   143      * long values that are then used to initialize the parameter `a` and the
   143      * long values that are then used to initialize the parameter `a` and the
   144      * state variables `s`, `x0`, and `x1` for a newly constructed generator.
   144      * state variables `s`, `x0`, and `x1` for a newly constructed generator.
   145      *
   145      *
   146      * With extremely high probability, no two generators so chosen
   146      * With extremely high probability, no two generators so chosen
   147      * will have the same `a` parameter, and testing has indicated
   147      * will have the same `a` parameter, and testing has indicated
   148      * that the values generated by two instances of {@code L64X128MixRandom}
   148      * that the values generated by two instances of {@link L64X128MixRandom}
   149      * will be (approximately) independent if have different values for `a`.
   149      * will be (approximately) independent if have different values for `a`.
   150      *
   150      *
   151      * The default (no-argument) constructor, in essence, uses
   151      * The default (no-argument) constructor, in essence, uses
   152      * "defaultGen" to generate four new 64-bit values for the same
   152      * "defaultGen" to generate four new 64-bit values for the same
   153      * purpose.  Multiple generators created in this way will certainly
   153      * purpose.  Multiple generators created in this way will certainly
   160      * avoids stalls that may occur when using /dev/random.
   160      * avoids stalls that may occur when using /dev/random.
   161      *
   161      *
   162      * File organization: First static fields, then instance
   162      * File organization: First static fields, then instance
   163      * fields, then constructors, then instance methods.
   163      * fields, then constructors, then instance methods.
   164      */
   164      */
   165     
   165 
   166     /* ---------------- static fields ---------------- */
   166     /* ---------------- static fields ---------------- */
   167 
   167 
   168     /**
   168     /**
   169      * The seed generator for default constructors.
   169      * The seed generator for default constructors.
   170      */
   170      */
   171     private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
   171     private static final AtomicLong defaultGen = new AtomicLong(RNGSupport.initialSeed());
   172 
   172 
   173     /*
   173     /*
   174      * The period of this generator, which is (2**128 - 1) * 2**64.
   174      * The period of this generator, which is (2**128 - 1) * 2**64.
   175      */
   175      */
   176     private static final BigInteger thePeriod =
   176     private static final BigInteger PERIOD =
   177 	BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).shiftLeft(64);
   177         BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).shiftLeft(64);
   178 
   178 
   179     /*
   179     /*
   180      * Multiplier used in the LCG portion of the algorithm, taken from
   180      * Multiplier used in the LCG portion of the algorithm, taken from
   181      * Pierre L'Ecuyer, Tables of linear congruential generators of
   181      * Pierre L'Ecuyer, Tables of linear congruential generators of
   182      * different sizes and good lattice structure, <em>Mathematics of
   182      * different sizes and good lattice structure, <em>Mathematics of
   183      * Computation</em> 68, 225 (January 1999), pages 249-260,
   183      * Computation</em> 68, 225 (January 1999), pages 249-260,
   184      * Table 4 (first multiplier for size 2<sup>64</sup>).
   184      * Table 4 (first multiplier for size 2<sup>64</sup>).
   185      */
   185      */
   186 
   186 
   187     private static final long m = 2862933555777941757L;
   187     private static final long M = 2862933555777941757L;
   188 
   188 
   189     /* ---------------- instance fields ---------------- */
   189     /* ---------------- instance fields ---------------- */
   190 
   190 
   191     /**
   191     /**
   192      * The parameter that is used as an additive constant for the LCG.
   192      * The parameter that is used as an additive constant for the LCG.
   211      * @param s initial state for the LCG
   211      * @param s initial state for the LCG
   212      * @param x0 first word of the initial state for the xorshift generator
   212      * @param x0 first word of the initial state for the xorshift generator
   213      * @param x1 second word of the initial state for the xorshift generator
   213      * @param x1 second word of the initial state for the xorshift generator
   214      */
   214      */
   215     public L64X128MixRandom(long a, long s, long x0, long x1) {
   215     public L64X128MixRandom(long a, long s, long x0, long x1) {
   216 	// Force a to be odd.
   216         // Force a to be odd.
   217         this.a = a | 1;
       
   218         this.s = s;
       
   219 	this.x0 = x0;
       
   220         this.x1 = x1;
       
   221 	// If x0 and x1 are both zero, we must choose nonzero values.
       
   222         if ((x0 | x1) == 0) {
       
   223 	    // At least one of the two values generated here will be nonzero.
       
   224 	    this.x0 = RngSupport.mixStafford13(s += RngSupport.GOLDEN_RATIO_64);
       
   225 	    this.x1 = RngSupport.mixStafford13(s + RngSupport.GOLDEN_RATIO_64);
       
   226 	}
       
   227     }
       
   228 
       
   229     /**
       
   230      * Creates a new instance of {@code L64X128MixRandom} using the
       
   231      * specified {@code long} value as the initial seed. Instances of
       
   232      * {@code L64X128MixRandom} created with the same seed in the same
       
   233      * program generate identical sequences of values.
       
   234      *
       
   235      * @param seed the initial seed
       
   236      */
       
   237     public L64X128MixRandom(long seed) {
       
   238 	// Using a value with irregularly spaced 1-bits to xor the seed
       
   239 	// argument tends to improve "pedestrian" seeds such as 0 or
       
   240 	// other small integers.  We may as well use SILVER_RATIO_64.
       
   241 	//
       
   242 	// The seed is hashed by mixMurmur64 to produce the `a` parameter.
       
   243 	// The seed is hashed by mixStafford13 to produce the initial `x0`,
       
   244 	// which will then be used to produce the first generated value.
       
   245 	// Then x1 is filled in as if by a SplitMix PRNG with
       
   246 	// GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
       
   247         this(RngSupport.mixMurmur64(seed ^= RngSupport.SILVER_RATIO_64),
       
   248 	     1,
       
   249 	     RngSupport.mixStafford13(seed),
       
   250 	     RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
       
   251     }
       
   252 
       
   253     /**
       
   254      * Creates a new instance of {@code L64X128MixRandom} that is likely to
       
   255      * generate sequences of values that are statistically independent
       
   256      * of those of any other instances in the current program execution,
       
   257      * but may, and typically does, vary across program invocations.
       
   258      */
       
   259     public L64X128MixRandom() {
       
   260 	// Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
       
   261 	this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
       
   262     }
       
   263 
       
   264     /**
       
   265      * Creates a new instance of {@code L64X128MixRandom} using the specified array of
       
   266      * initial seed bytes. Instances of {@code L64X128MixRandom} created with the same
       
   267      * seed array in the same program execution generate identical sequences of values.
       
   268      *
       
   269      * @param seed the initial seed
       
   270      */
       
   271     public L64X128MixRandom(byte[] seed) {
       
   272 	// Convert the seed to 4 long values, of which the last 2 are not all zero.
       
   273 	long[] data = RngSupport.convertSeedBytesToLongs(seed, 4, 2);
       
   274 	long a = data[0], s = data[1], x0 = data[2], x1 = data[3];
       
   275 	// Force a to be odd.
       
   276         this.a = a | 1;
   217         this.a = a | 1;
   277         this.s = s;
   218         this.s = s;
   278         this.x0 = x0;
   219         this.x0 = x0;
   279         this.x1 = x1;
   220         this.x1 = x1;
       
   221         // If x0 and x1 are both zero, we must choose nonzero values.
       
   222         if ((x0 | x1) == 0) {
       
   223             // At least one of the two values generated here will be nonzero.
       
   224             this.x0 = RNGSupport.mixStafford13(s += RNGSupport.GOLDEN_RATIO_64);
       
   225             this.x1 = RNGSupport.mixStafford13(s + RNGSupport.GOLDEN_RATIO_64);
       
   226         }
       
   227     }
       
   228 
       
   229     /**
       
   230      * Creates a new instance of {@link L64X128MixRandom} using the
       
   231      * specified {@code long} value as the initial seed. Instances of
       
   232      * {@link L64X128MixRandom} created with the same seed in the same
       
   233      * program generate identical sequences of values.
       
   234      *
       
   235      * @param seed the initial seed
       
   236      */
       
   237     public L64X128MixRandom(long seed) {
       
   238         // Using a value with irregularly spaced 1-bits to xor the seed
       
   239         // argument tends to improve "pedestrian" seeds such as 0 or
       
   240         // other small integers.  We may as well use SILVER_RATIO_64.
       
   241         //
       
   242         // The seed is hashed by mixMurmur64 to produce the `a` parameter.
       
   243         // The seed is hashed by mixStafford13 to produce the initial `x0`,
       
   244         // which will then be used to produce the first generated value.
       
   245         // Then x1 is filled in as if by a SplitMix PRNG with
       
   246         // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
       
   247         this(RNGSupport.mixMurmur64(seed ^= RNGSupport.SILVER_RATIO_64),
       
   248              1,
       
   249              RNGSupport.mixStafford13(seed),
       
   250              RNGSupport.mixStafford13(seed + RNGSupport.GOLDEN_RATIO_64));
       
   251     }
       
   252 
       
   253     /**
       
   254      * Creates a new instance of {@link L64X128MixRandom} that is likely to
       
   255      * generate sequences of values that are statistically independent
       
   256      * of those of any other instances in the current program execution,
       
   257      * but may, and typically does, vary across program invocations.
       
   258      */
       
   259     public L64X128MixRandom() {
       
   260         // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
       
   261         this(defaultGen.getAndAdd(RNGSupport.GOLDEN_RATIO_64));
       
   262     }
       
   263 
       
   264     /**
       
   265      * Creates a new instance of {@link L64X128MixRandom} using the specified array of
       
   266      * initial seed bytes. Instances of {@link L64X128MixRandom} created with the same
       
   267      * seed array in the same program execution generate identical sequences of values.
       
   268      *
       
   269      * @param seed the initial seed
       
   270      */
       
   271     public L64X128MixRandom(byte[] seed) {
       
   272         // Convert the seed to 4 long values, of which the last 2 are not all zero.
       
   273         long[] data = RNGSupport.convertSeedBytesToLongs(seed, 4, 2);
       
   274         long a = data[0], s = data[1], x0 = data[2], x1 = data[3];
       
   275         // Force a to be odd.
       
   276         this.a = a | 1;
       
   277         this.s = s;
       
   278         this.x0 = x0;
       
   279         this.x1 = x1;
   280     }
   280     }
   281 
   281 
   282     /* ---------------- public methods ---------------- */
   282     /* ---------------- public methods ---------------- */
   283     
   283 
   284     /**
   284     /**
   285      * Constructs and returns a new instance of {@code L64X128MixRandom}
   285      * Constructs and returns a new instance of {@link L64X128MixRandom}
   286      * that shares no mutable state with this instance.
   286      * that shares no mutable state with this instance.
   287      * However, with very high probability, the set of values collectively
   287      * However, with very high probability, the set of values collectively
   288      * generated by the two objects has the same statistical properties as if
   288      * generated by the two objects has the same statistical properties as if
   289      * same the quantity of values were generated by a single thread using
   289      * same the quantity of values were generated by a single thread using
   290      * a single {@code L64X128MixRandom} object.  Either or both of the two
   290      * a single {@link L64X128MixRandom} object.  Either or both of the two
   291      * objects may be further split using the {@code split} method,
   291      * objects may be further split using the {@code split} method,
   292      * and the same expected statistical properties apply to the
   292      * and the same expected statistical properties apply to the
   293      * entire set of generators constructed by such recursive splitting.
   293      * entire set of generators constructed by such recursive splitting.
   294      *
   294      *
   295      * @param source a {@code SplittableRng} instance to be used instead
   295      * @param source a {@link SplittableRNG} instance to be used instead
   296      *               of this one as a source of pseudorandom bits used to
   296      *               of this one as a source of pseudorandom bits used to
   297      *               initialize the state of the new ones.
   297      *               initialize the state of the new ones.
   298      * @return a new instance of {@code L64X128MixRandom}
   298      *
   299      */
   299      * @return a new instance of {@link L64X128MixRandom}
   300     public L64X128MixRandom split(SplittableRng source) {
   300      */
   301 	// Literally pick a new instance "at random".
   301     public L64X128MixRandom split(SplittableRNG source) {
       
   302         // Literally pick a new instance "at random".
   302         return new L64X128MixRandom(source.nextLong(), source.nextLong(),
   303         return new L64X128MixRandom(source.nextLong(), source.nextLong(),
   303 				    source.nextLong(), source.nextLong());
   304                                     source.nextLong(), source.nextLong());
   304     }
   305     }
   305 
   306 
   306     /**
   307     /**
   307      * Returns a pseudorandom {@code long} value.
   308      * Returns a pseudorandom {@code long} value.
   308      *
   309      *
   309      * @return a pseudorandom {@code long} value
   310      * @return a pseudorandom {@code long} value
   310      */
   311      */
   311 
       
   312     public long nextLong() {
   312     public long nextLong() {
   313 	final long z = s + x0;
   313         final long z = s + x0;
   314 	s = m * s + a;  // LCG
   314         s = M * s + a;  // LCG
   315 	long q0 = x0, q1 = x1;
   315         long q0 = x0, q1 = x1;
   316 	{ q1 ^= q0; q0 = Long.rotateLeft(q0, 24); q0 = q0 ^ q1 ^ (q1 << 16); q1 = Long.rotateLeft(q1, 37); }  // xoroshiro128v1_0
   316         { q1 ^= q0; q0 = Long.rotateLeft(q0, 24); q0 = q0 ^ q1 ^ (q1 << 16); q1 = Long.rotateLeft(q1, 37); }  // xoroshiro128v1_0
   317 	x0 = q0; x1 = q1;
   317         x0 = q0; x1 = q1;
   318 	return Long.rotateLeft(z * 5, 7) * 9;  // "starstar" mixing function
   318         return Long.rotateLeft(z * 5, 7) * 9;  // "starstar" mixing function
   319     }
   319     }
   320 
   320 
   321     public BigInteger period() { return thePeriod; }
   321     public BigInteger period() {
       
   322         return PERIOD;
       
   323     }
   322 }
   324 }