src/java.base/share/classes/java/util/random/Xoroshiro128StarStar.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/Xoroshiro128StarStar.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 Xoroshiro128StarStar} implements
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    34
 * interfaces {@link java.util.Rng} and {@link java.util.LeapableRng},
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 {@code Xoroshiro128StarStar} objects
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    38
 * by "jumping" or "leaping".
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
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    43
 * <p>The class {@code Xoroshiro128StarStar} uses the {@code xoroshiro128} algorithm,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    44
 * version 1.0 (parameters 24, 16, 37), with the "**" scrambler (a mixing function).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    45
 * Its state consists of two {@code long} fields {@code x0} and {@code x1},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    46
 * which can take on any values provided that they are not both zero.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    47
 * The period of this generator is 2<sup>128</sup>-1.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    48
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    49
 * <p>The 64-bit values produced by the {@code nextLong()} method are equidistributed.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    50
 * To be precise, over the course of the cycle of length 2<sup>128</sup>-1,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    51
 * each nonzero {@code long} value is generated 2<sup>64</sup> times,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    52
 * but the value 0 is generated only 2<sup>64</sup>-1 times.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    53
 * 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
    54
 * methods are likewise equidistributed.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    55
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    56
 * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 2-equidistributed.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    57
 * To be precise: consider the (overlapping) length-2 subsequences of the cycle of 64-bit
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    58
 * values produced by {@code nextLong()} (assuming no other methods are called that would
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    59
 * affect the state).  There are 2<sup>128</sup>-1 such subsequences, and each subsequence,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    60
 * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values.  Of those
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    61
 * 2<sup>128</sup> subsequence values, each one is generated exactly once over the course
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    62
 * of the entire cycle, except that the subsequence (0, 0) never appears.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    63
 * 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
    64
 * methods are likewise 2-equidistributed, but note that that the subsequence (0, 0)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    65
 * can also appear (but occurring somewhat less frequently than all other subsequences),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    66
 * because the values produced by those methods have fewer than 64 randomly chosen bits.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    67
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    68
 * <p>Instances {@code Xoroshiro128StarStar} are <em>not</em> thread-safe.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    69
 * They are designed to be used so that each thread as its own instance.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    70
 * The methods {@link #jump} and {@link #leap} and {@link #jumps} and {@link #leaps}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    71
 * can be used to construct new instances of {@code Xoroshiro128StarStar} that traverse
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    72
 * other parts of the state cycle.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    73
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    74
 * <p>Instances of {@code Xoroshiro128StarStar} are not cryptographically
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    75
 * secure.  Consider instead using {@link java.security.SecureRandom}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    76
 * in security-sensitive applications. Additionally,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    77
 * default-constructed instances do not use a cryptographically random
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    78
 * seed unless the {@linkplain System#getProperty system property}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    79
 * {@code java.util.secureRandomSeed} is set to {@code true}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    80
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    81
 * @author  Guy Steele
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    82
 * @author  Doug Lea
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    83
 * @since   1.8
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    84
 */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    85
public final class Xoroshiro128StarStar implements LeapableRng {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    86
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    87
    /*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    88
     * Implementation Overview.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    89
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    90
     * This is an implementation of the xoroshiro128** algorithm written
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    91
     * in 2016 by David Blackman and Sebastiano Vigna (vigna@acm.org),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    92
     * and updated with improved parameters in 2018.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    93
     * See http://xoshiro.di.unimi.it and these two papers:
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    94
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    95
     *    Sebastiano Vigna. 2016. An Experimental Exploration of Marsaglia's
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    96
     *    xorshift Generators, Scrambled. ACM Transactions on Mathematical
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    97
     *    Software 42, 4, Article 30 (June 2016), 23 pages.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    98
     *    https://doi.org/10.1145/2845077
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    99
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   100
     *    David Blackman and Sebastiano Vigna.  2018.  Scrambled Linear
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   101
     *    Pseudorandom Number Generators.  Computing Research Repository (CoRR).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   102
     *    http://arxiv.org/abs/1805.01407
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   103
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   104
     * The jump operation moves the current generator forward by 2*64
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   105
     * steps; this has the same effect as calling nextLong() 2**64
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   106
     * times, but is much faster.  Similarly, the leap operation moves
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   107
     * the current generator forward by 2*96 steps; this has the same
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   108
     * effect as calling nextLong() 2**96 times, but is much faster.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   109
     * The copy method may be used to make a copy of the current
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   110
     * generator.  Thus one may repeatedly and cumulatively copy and
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   111
     * jump to produce a sequence of generators whose states are well
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   112
     * spaced apart along the overall state cycle (indeed, the jumps()
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   113
     * and leaps() methods each produce a stream of such generators).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   114
     * The generators can then be parceled out to other threads.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   115
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   116
     * File organization: First the non-public methods that constitute the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   117
     * main algorithm, then the public methods.  Note that many methods are
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   118
     * defined by classes {@code AbstractJumpableRng} and {@code AbstractRng}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   119
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   120
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   121
    /* ---------------- static fields ---------------- */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   122
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   123
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   124
     * The seed generator for default constructors.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   125
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   126
    private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   127
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   128
    /*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   129
     * The period of this generator, which is 2**128 - 1.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   130
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   131
    private static final BigInteger thePeriod =
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   132
	BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   133
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   134
    /* ---------------- instance fields ---------------- */
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   137
     * The per-instance state.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   138
     * At least one of the two fields x0 and x1 must be nonzero.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   139
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   140
    private long x0, x1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   141
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   142
    /* ---------------- constructors ---------------- */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   143
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   144
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   145
     * Basic constructor that initializes all fields from parameters.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   146
     * It then adjusts the field values if necessary to ensure that
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   147
     * all constraints on the values of fields are met.
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
     * @param x0 first word of the initial state
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   150
     * @param x1 second word of the initial state
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   151
    */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   152
    public Xoroshiro128StarStar(long x0, long x1) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   153
	this.x0 = x0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   154
        this.x1 = x1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   155
	// If x0 and x1 are both zero, we must choose nonzero values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   156
        if ((x0 | x1) == 0) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   157
	    // At least one of the two values generated here will be nonzero.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   158
	    this.x0 = RngSupport.mixStafford13(x0 += RngSupport.GOLDEN_RATIO_64);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   159
	    this.x1 = (x0 += RngSupport.GOLDEN_RATIO_64);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   160
	}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   161
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   162
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
     * Creates a new instance of {@code Xoroshiro128StarStar} using the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   165
     * specified {@code long} value as the initial seed. Instances of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   166
     * {@code Xoroshiro128StarStar} created with the same seed in the same
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   167
     * program generate identical sequences of values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   168
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   169
     * @param seed the initial seed
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   170
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   171
    public Xoroshiro128StarStar(long seed) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   172
	// 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
   173
	// argument tends to improve "pedestrian" seeds such as 0 or
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   174
	// other small integers.  We may as well use SILVER_RATIO_64.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   175
	//
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   176
	// The x values are then filled in as if by a SplitMix PRNG with
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   177
	// 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
   178
        this(RngSupport.mixStafford13(seed ^= RngSupport.SILVER_RATIO_64),
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   179
	     RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   183
     * Creates a new instance of {@code Xoroshiro128StarStar} that is likely to
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   184
     * generate sequences of values that are statistically independent
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   185
     * of those of any other instances in the current program execution,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   186
     * but may, and typically does, vary across program invocations.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   187
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   188
    public Xoroshiro128StarStar() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   189
	// 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
   190
        this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   194
     * Creates a new instance of {@code Xoroshiro128StarStar} using the specified array of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   195
     * initial seed bytes. Instances of {@code Xoroshiro128StarStar} created with the same
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   196
     * 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
   197
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   198
     * @param seed the initial seed
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
    public Xoroshiro128StarStar(byte[] seed) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   201
	// Convert the seed to 2 long values, which are not both zero.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   202
	long[] data = RngSupport.convertSeedBytesToLongs(seed, 2, 2);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   203
	long x0 = data[0], x1 = data[1];
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   204
        this.x0 = x0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   205
        this.x1 = x1;
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
    /* ---------------- public methods ---------------- */
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
    public Xoroshiro128StarStar copy() { return new Xoroshiro128StarStar(x0, x1); }
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
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   214
To the extent possible under law, the author has dedicated all copyright
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   215
and related and neighboring rights to this software to the public domain
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   216
worldwide. This software is distributed without any warranty.
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
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   219
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   220
/* This is the successor to xorshift128+. It is the fastest full-period
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   221
   generator passing BigCrush without systematic failures, but due to the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   222
   relatively short period it is acceptable only for applications with a
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   223
   mild amount of parallelism; otherwise, use a xorshift1024* generator.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   224
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   225
   Beside passing BigCrush, this generator passes the PractRand test suite
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   226
   up to (and included) 16TB, with the exception of binary rank tests,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   227
   which fail due to the lowest bit being an LFSR; all other bits pass all
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   228
   tests. We suggest to use a sign test to extract a random Boolean value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   229
   
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   230
   Note that the generator uses a simulated rotate operation, which most C
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   231
   compilers will turn into a single instruction. In Java, you can use
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   232
   Long.rotateLeft(). In languages that do not make low-level rotation
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   233
   instructions accessible xorshift128+ could be faster.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   234
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   235
   The state must be seeded so that it is not everywhere zero. If you have
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   236
   a 64-bit seed, we suggest to seed a splitmix64 generator and use its
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   237
   output to fill s. */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   238
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   241
     * Returns a pseudorandom {@code long} value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   242
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   243
     * @return a pseudorandom {@code long} value
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   244
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   245
    public long nextLong() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   246
	final long s0 = x0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   247
	long s1 = x1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   248
	final long z = s0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   249
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   250
	s1 ^= s0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   251
	x0 = Long.rotateLeft(s0, 24) ^ s1 ^ (s1 << 16); // a, b
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   252
	x1 = Long.rotateLeft(s1, 37); // c
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   253
	
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   254
	return Long.rotateLeft(z * 5, 7) * 9;  // "starstar" mixing function
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   255
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   256
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   257
    public BigInteger period() { return thePeriod; }
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
    public double defaultJumpDistance() { return 0x1.0p64; }
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
    public double defaultLeapDistance() { return 0x1.0p96; }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   262
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   263
    private static final long[] JUMP_TABLE = { 0xdf900294d8f554a5L, 0x170865df4b3201fcL };
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   264
    
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   265
    private static final long[] LEAP_TABLE = { 0xd2a98b26625eee7bL, 0xdddf9b1090aa7ac1L };
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   266
   
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   267
/* This is the jump function for the generator. It is equivalent
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   268
   to 2**64 calls to nextLong(); it can be used to generate 2**64
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   269
   non-overlapping subsequences for parallel computations. */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   270
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   271
    public void jump() { jumpAlgorithm(JUMP_TABLE); }
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
/* This is the long-jump function for the generator. It is equivalent to
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   274
   2**96 calls to next(); it can be used to generate 2**32 starting points,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   275
   from each of which jump() will generate 2**32 non-overlapping
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   276
   subsequences for parallel distributed computations. */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   277
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   278
    public void leap() { jumpAlgorithm(LEAP_TABLE); }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   279
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   280
    private void jumpAlgorithm(long[] table) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   281
	long s0 = 0, s1 = 0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   282
	for (int i = 0; i < table.length; i++) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   283
	    for (int b = 0; b < 64; b++) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   284
		if ((table[i] & (1L << b)) != 0) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   285
		    s0 ^= x0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   286
		    s1 ^= x1;
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
		nextLong();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   289
	    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   290
	    x0 = s0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   291
	    x1 = s1;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   292
	}
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
}