src/java.base/share/classes/java/util/random/L128X256MixRandom.java
author jlaskey
Thu, 27 Jun 2019 18:02:51 -0300
branchJDK-8193209-branch
changeset 57436 b0c958c0e6c6
parent 57388 src/java.base/share/classes/java/util/L128X256MixRandom.java@b1e6bc96af3d
child 57437 f02ffcb61dce
permissions -rw-r--r--
Move random number generators to new folder.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     1
/*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     4
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    10
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    15
 * accompanied this code).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    16
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    20
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    23
 * questions.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    24
 */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    25
package java.util;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    26
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    27
import java.math.BigInteger;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    28
import java.util.concurrent.atomic.AtomicLong;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    29
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    30
/**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    31
 * A generator of uniform pseudorandom values applicable for use in
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    32
 * (among other contexts) isolated parallel computations that may
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    33
 * generate subtasks.  Class {@code L128X256MixRandom} implements
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    34
 * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    35
 * and therefore supports methods for producing pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    36
 * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    37
 * as well as creating new split-off {@code L128X256MixRandom} objects,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    38
 * with similar usages as for class {@link java.util.SplittableRandom}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    39
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    40
 * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    41
 * that measure independence and uniformity properties of random number generators.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    42
 * (Most recently validated with
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    43
 * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    44
 * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    45
 * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    46
 * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    47
 * These tests validate only the methods for certain
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    48
 * types and ranges, but similar properties are expected to hold, at
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    49
 * least approximately, for others as well.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    50
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    51
 * <p>{@code L128X256MixRandom} is a specific member of the LXM family of algorithms
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    52
 * for pseudorandom number generators.  Every LXM generator consists of two
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    53
 * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    54
 * an Xorshift generator.  Each output of an LXM generator is the sum of one
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    55
 * output from each subgenerator, possibly processed by a final mixing function
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    56
 * (and {@code L128X256MixRandom} does use a mixing function).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    57
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    58
 * <p>The LCG subgenerator for {@code L128X256MixRandom} has an update step of the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    59
 * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    60
 * 128-bit integers; {@code s} is the mutable state, the multiplier {@code m}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    61
 * is fixed (the same for all instances of {@code L128X256MixRandom}}) and the addend
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    62
 * {@code a} is a parameter (a final field of the instance).  The parameter
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    63
 * {@code a} is required to be odd (this allows the LCG to have the maximal
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    64
 * period, namely 2<sup>128</sup>); therefore there are 2<sup>127</sup> distinct choices
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    65
 * of parameter.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    66
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    67
 * <p>The Xorshift subgenerator for {@code L128X256MixRandom} is the {@code xoshiro256} algorithm,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    68
 * version 1.0 (parameters 17, 45), without any final scrambler such as "+" or "**".
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    69
 * Its state consists of four {@code long} fields {@code x0}, {@code x1}, {@code x2},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    70
 * and {@code x3}, which can take on any values provided that they are not all zero.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    71
 * The period of this subgenerator is 2<sup>256</sup>-1.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    72
 * 
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    73
 * <p> The mixing function for {@code L128X256MixRandom} is the 64-bit MurmurHash3 finalizer.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    74
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    75
 * <p> Because the periods 2<sup>128</sup> and 2<sup>256</sup>-1 of the two subgenerators
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    76
 * are relatively prime, the <em>period</em> of any single {@code L128X256MixRandom} object 
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    77
 * (the length of the series of generated 64-bit values before it repeats) is the product
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    78
 * of the periods of the subgenerators, that is, 2<sup>128</sup>(2<sup>256</sup>-1),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    79
 * which is just slightly smaller than 2<sup>384</sup>.  Moreover, if two distinct
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    80
 * {@code L128X256MixRandom} objects have different {@code a} parameters, then their
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    81
 * cycles of produced values will be different.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    82
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    83
 * <p>The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    84
 * For any specific instance of {@code L128X256MixRandom}, over the course of its cycle each
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    85
 * of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>256</sup>-1 times.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    86
 * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    87
 * methods are likewise exactly equidistributed.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    88
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    89
 * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are exactly
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    90
 * 2-equidistributed.  For any specific instance of {@code L128X256MixRandom}, consider
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    91
 * the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    92
 * {@code nextLong()} (assuming no other methods are called that would affect the state).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    93
 * There are 2<sup>128</sup>(2<sup>256</sup>-1) such subsequences, and each subsequence,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    94
 * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values, and each
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    95
 * such value occurs  2<sup>256</sup>-1 times.  The values produced by the {@code nextInt()},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    96
 * {@code nextFloat()}, and {@code nextDouble()} methods are likewise exactly 2-equidistributed.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    97
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    98
 * <p>Moreover, the 64-bit values produced by the {@code nextLong()} method are 4-equidistributed.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    99
 * To be precise: for any specific instance of {@code L128X256MixRandom}, consider
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   100
 * the (overlapping) length-4 subsequences of the cycle of 64-bit values produced by
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   101
 * {@code nextLong()} (assuming no other methods are called that would affect the state).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   102
 * There are <sup>128</sup>(2<sup>256</sup>-1) such subsequences, and each subsequence,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   103
 * which consists of 4 64-bit values, can have one of 2<sup>256</sup> values. Of those
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   104
 * 2<sup>256</sup> subsequence values, nearly all of them (2<sup>256</sup>-2<sup>128</sup>)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   105
 * occur 2<sup>128</sup> times over the course of the entire cycle, and the other
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   106
 * 2<sup>128</sup> subsequence values occur only 2<sup>128</sup>-1 times.  So the ratio
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   107
 * of the probability of getting one of the less common subsequence values and the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   108
 * probability of getting one of the more common subsequence values is 1-2<sup>-128</sup>.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   109
 * (Note that the set of 2<sup>128</sup> less-common subsequence values will differ from
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   110
 * one instance of {@code L128X256MixRandom} to another, as a function of the additive
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   111
 * parameter of the LCG.)  The values produced by the {@code nextInt()}, {@code nextFloat()},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   112
 * and {@code nextDouble()} methods are likewise 4-equidistributed.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   113
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   114
 * <p>Method {@link #split} constructs and returns a new {@code L128X256MixRandom}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   115
 * instance that shares no mutable state with the current instance. However, with
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   116
 * very high probability, the values collectively generated by the two objects
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   117
 * have the same statistical properties as if the same quantity of values were
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   118
 * generated by a single thread using a single {@code L128X256MixRandom} object.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   119
 * This is because, with high probability, distinct {@code L128X256MixRandom} objects
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   120
 * have distinct {@code a} parameters and therefore use distinct members of the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   121
 * algorithmic family; and even if their {@code a} parameters are the same, with
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   122
 * very high probability they will traverse different parts of their common state
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   123
 * cycle.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   124
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   125
 * <p>As with {@link java.util.SplittableRandom}, instances of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   126
 * {@code L128X256MixRandom} are <em>not</em> thread-safe.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   127
 * They are designed to be split, not shared, across threads. For
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   128
 * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   129
 * computation using random numbers might include a construction
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   130
 * of the form {@code new Subtask(someL128X256MixRandom.split()).fork()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   131
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   132
 * <p>This class provides additional methods for generating random
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   133
 * streams, that employ the above techniques when used in
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   134
 * {@code stream.parallel()} mode.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   135
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   136
 * <p>Instances of {@code L128X256MixRandom} are not cryptographically
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   137
 * secure.  Consider instead using {@link java.security.SecureRandom}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   138
 * in security-sensitive applications. Additionally,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   139
 * default-constructed instances do not use a cryptographically random
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   140
 * seed unless the {@linkplain System#getProperty system property}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   141
 * {@code java.util.secureRandomSeed} is set to {@code true}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   142
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   143
 * @author  Guy Steele
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   144
 * @since   1.9
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   145
 */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   146
public final class L128X256MixRandom extends AbstractSplittableRng {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   147
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   148
    /*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   149
     * Implementation Overview.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   150
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   151
     * The 128-bit parameter `a` is represented as two long fields `ah` and `al`.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   152
     * The 128-bit state variable `s` is represented as two long fields `sh` and `sl`.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   153
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   154
     * The split operation uses the current generator to choose eight
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   155
     * new 64-bit long values that are then used to initialize the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   156
     * parameters `ah` and `al` and the state variables `sh`, `sl`,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   157
     * `x0`, `x1`, `x2`, and `x3` for a newly constructed generator.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   158
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   159
     * With extremely high probability, no two generators so chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   160
     * will have the same `a` parameter, and testing has indicated
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   161
     * that the values generated by two instances of {@code L128X256MixRandom}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   162
     * will be (approximately) independent if have different values for `a`.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   163
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   164
     * The default (no-argument) constructor, in essence, uses
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   165
     * "defaultGen" to generate eight new 64-bit values for the same
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   166
     * purpose.  Multiple generators created in this way will certainly
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   167
     * differ in their `a` parameters.  The defaultGen state must be accessed
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   168
     * in a thread-safe manner, so we use an AtomicLong to represent
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   169
     * this state.  To bootstrap the defaultGen, we start off using a
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   170
     * seed based on current time unless the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   171
     * java.util.secureRandomSeed property is set. This serves as a
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   172
     * slimmed-down (and insecure) variant of SecureRandom that also
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   173
     * avoids stalls that may occur when using /dev/random.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   174
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   175
     * File organization: First static fields, then instance
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   176
     * fields, then constructors, then instance methods.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   177
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   178
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   179
    /* ---------------- static fields ---------------- */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   180
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   181
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   182
     * The seed generator for default constructors.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   183
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   184
    private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   185
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   186
    /*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   187
     * The period of this generator, which is (2**256 - 1) * 2**128.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   188
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   189
    private static final BigInteger thePeriod =
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   190
	BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE).shiftLeft(128);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   191
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   192
    /*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   193
     * The multiplier used in the LCG portion of the algorithm is 2**64 + m;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   194
     * where m is taken from
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   195
     * Pierre L'Ecuyer, Tables of linear congruential generators of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   196
     * different sizes and good lattice structure, <em>Mathematics of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   197
     * Computation</em> 68, 225 (January 1999), pages 249-260,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   198
     * Table 4 (first multiplier for size 2<sup>64</sup>).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   199
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   200
     * This is almost certainly not the best possible 128-bit multiplier
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   201
     * for an LCG, but it is sufficient for our purposes here; because
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   202
     * is is larger than 2**64, the 64-bit values produced by nextLong()
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   203
     * are exactly 2-equidistributed, and the fact that it is of the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   204
     * form (2**64 + m) simplifies the code, given that we have only
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   205
     * 64-bit arithmetic to work with.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   206
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   207
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   208
    private static final long m = 2862933555777941757L;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   209
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   210
    /* ---------------- instance fields ---------------- */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   211
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   212
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   213
     * The parameter that is used as an additive constant for the LCG.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   214
     * Must be odd.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   215
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   216
    private final long ah, al;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   217
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   218
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   219
     * The per-instance state: sh and sl for the LCG; x0, x1, x2, and x3 for the xorshift.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   220
     * At least one of the four fields x0, x1, x2, and x3 must be nonzero.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   221
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   222
    private long sh, sl, x0, x1, x2, x3;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   223
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   224
    /* ---------------- constructors ---------------- */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   225
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   226
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   227
     * Basic constructor that initializes all fields from parameters.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   228
     * It then adjusts the field values if necessary to ensure that
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   229
     * all constraints on the values of fields are met.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   230
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   231
     * @param ah high half of the additive parameter for the LCG
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   232
     * @param al low half of the additive parameter for the LCG
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   233
     * @param sh high half of the initial state for the LCG
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   234
     * @param sl low half of the initial state for the LCG
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   235
     * @param x0 first word of the initial state for the xorshift generator
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   236
     * @param x1 second word of the initial state for the xorshift generator
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   237
     * @param x2 third word of the initial state for the xorshift generator
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   238
     * @param x3 fourth word of the initial state for the xorshift generator
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   239
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   240
    public L128X256MixRandom(long ah, long al, long sh, long sl, long x0, long x1, long x2, long x3) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   241
	// Force a to be odd.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   242
        this.ah = ah;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   243
        this.al = al | 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   244
        this.sh = sh;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   245
        this.sl = sl;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   246
        this.x0 = x0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   247
        this.x1 = x1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   248
        this.x2 = x2;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   249
        this.x3 = x3;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   250
	// If x0, x1, x2, and x3 are all zero, we must choose nonzero values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   251
        if ((x0 | x1 | x2 | x3) == 0) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   252
	    // At least three of the four values generated here will be nonzero.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   253
	    this.x0 = RngSupport.mixStafford13(sh += RngSupport.GOLDEN_RATIO_64);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   254
	    this.x1 = RngSupport.mixStafford13(sh += RngSupport.GOLDEN_RATIO_64);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   255
	    this.x2 = RngSupport.mixStafford13(sh += RngSupport.GOLDEN_RATIO_64);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   256
	    this.x3 = RngSupport.mixStafford13(sh + RngSupport.GOLDEN_RATIO_64);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   257
	}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   258
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   259
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   260
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   261
     * Creates a new instance of {@code L128X256MixRandom} using the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   262
     * specified {@code long} value as the initial seed. Instances of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   263
     * {@code L128X256MixRandom} created with the same seed in the same
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   264
     * program generate identical sequences of values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   265
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   266
     * @param seed the initial seed
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   267
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   268
    public L128X256MixRandom(long seed) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   269
	// Using a value with irregularly spaced 1-bits to xor the seed
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   270
	// argument tends to improve "pedestrian" seeds such as 0 or
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   271
	// other small integers.  We may as well use SILVER_RATIO_64.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   272
	//
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   273
	// The seed is hashed by mixMurmur64 to produce the `a` parameter.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   274
	// The seed is hashed by mixStafford13 to produce the initial `x0`,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   275
	// which will then be used to produce the first generated value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   276
	// The other x values are filled in as if by a SplitMix PRNG with
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   277
	// GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   278
        this(RngSupport.mixMurmur64(seed ^= RngSupport.SILVER_RATIO_64),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   279
	     RngSupport.mixMurmur64(seed += RngSupport.GOLDEN_RATIO_64),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   280
	     0,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   281
	     1,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   282
	     RngSupport.mixStafford13(seed),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   283
	     RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   284
	     RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   285
	     RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   286
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   287
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   288
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   289
     * Creates a new instance of {@code L128X256MixRandom} that is likely to
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   290
     * generate sequences of values that are statistically independent
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   291
     * of those of any other instances in the current program execution,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   292
     * but may, and typically does, vary across program invocations.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   293
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   294
    public L128X256MixRandom() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   295
	// Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   296
        this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   297
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   298
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   299
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   300
     * Creates a new instance of {@code L128X256MixRandom} using the specified array of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   301
     * initial seed bytes. Instances of {@code L128X256MixRandom} created with the same
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   302
     * seed array in the same program execution generate identical sequences of values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   303
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   304
     * @param seed the initial seed
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   305
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   306
    public L128X256MixRandom(byte[] seed) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   307
	// Convert the seed to 6 long values, of which the last 4 are not all zero.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   308
	long[] data = RngSupport.convertSeedBytesToLongs(seed, 6, 4);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   309
	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];
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   310
	// Force a to be odd.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   311
        this.ah = ah;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   312
        this.al = al | 1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   313
        this.sh = sh;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   314
        this.sl = sl;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   315
        this.x0 = x0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   316
        this.x1 = x1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   317
        this.x2 = x2;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   318
        this.x3 = x3;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   319
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   320
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   321
    /* ---------------- public methods ---------------- */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   322
    
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   323
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   324
     * Constructs and returns a new instance of {@code L128X256MixRandom}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   325
     * that shares no mutable state with this instance.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   326
     * However, with very high probability, the set of values collectively
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   327
     * generated by the two objects has the same statistical properties as if
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   328
     * same the quantity of values were generated by a single thread using
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   329
     * a single {@code L128X256MixRandom} object.  Either or both of the two
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   330
     * objects may be further split using the {@code split} method,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   331
     * and the same expected statistical properties apply to the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   332
     * entire set of generators constructed by such recursive splitting.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   333
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   334
     * @param source a {@code SplittableRng} instance to be used instead
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   335
     *               of this one as a source of pseudorandom bits used to
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   336
     *               initialize the state of the new ones.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   337
     * @return a new instance of {@code L128X256MixRandom}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   338
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   339
    public L128X256MixRandom split(SplittableRng source) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   340
	// Literally pick a new instance "at random".
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   341
        return new L128X256MixRandom(source.nextLong(), source.nextLong(), 
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   342
				     source.nextLong(), source.nextLong(),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   343
				     source.nextLong(), source.nextLong(),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   344
				     source.nextLong(), source.nextLong());
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   345
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   346
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   347
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   348
     * Returns a pseudorandom {@code long} value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   349
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   350
     * @return a pseudorandom {@code long} value
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   351
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   352
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   353
    public long nextLong() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   354
	final long z = sh + x0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   355
	// The LCG: in effect, s = ((1LL << 64) + m) * s + a, if only we had 128-bit arithmetic.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   356
	final long u = m * sl;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   357
	sh = (m * sh) + Math.multiplyHigh(m, sl) + sl + ah;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   358
	sl = u + al;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   359
	if (Long.compareUnsigned(sl, u) < 0) ++sh;  // Handle the carry propagation from low half to high half.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   360
	long q0 = x0, q1 = x1, q2 = x2, q3 = x3;	
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   361
	{ long t = q1 << 17; q2 ^= q0; q3 ^= q1; q1 ^= q2; q0 ^= q3; q2 ^= t; q3 = Long.rotateLeft(q3, 45); }  // xoshiro256 1.0
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   362
	x0 = q0; x1 = q1; x2 = q2; x3 = q3;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   363
	return RngSupport.mixLea64(z);  // mixing function
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   364
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   365
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   366
    public BigInteger period() { return thePeriod; }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   367
}