newrandom/Xoshiro256StarStar.java
author briangoetz
Thu, 23 May 2019 16:45:56 -0400
branchbriangoetz-test-branch
changeset 57369 6d87e9f7a1ec
permissions -rwxr-xr-x
Initial comment in newrandom/
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57369
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     1
/*
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     3
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     4
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     5
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     6
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     7
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     8
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     9
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    10
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    11
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    12
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    13
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    14
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    15
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    16
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    17
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    18
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    19
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    20
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    21
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    22
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    23
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    24
 */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    25
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    26
// package java.util;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    27
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    28
import java.math.BigInteger;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    29
import java.util.concurrent.atomic.AtomicLong;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    30
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    31
/**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    32
 * A generator of uniform pseudorandom values applicable for use in
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    33
 * (among other contexts) isolated parallel computations that may
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    34
 * generate subtasks.  Class {@code Xoshiro256StarStar} implements
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    35
 * interfaces {@link java.util.Rng} and {@link java.util.LeapableRng},
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    36
 * and therefore supports methods for producing pseudorandomly chosen
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    37
 * numbers of type {@code int}, {@code long}, {@code float}, and {@code double}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    38
 * as well as creating new {@code Xoshiro256StarStar} objects
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    39
 * by "jumping" or "leaping".
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    40
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    41
 * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    42
 * that measure independence and uniformity properties of random number generators.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    43
 * (Most recently validated with
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    44
 * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    45
 * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    46
 * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    47
 * method but also the result of bit-reversing each value produced by {@code nextLong()}.)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    48
 * These tests validate only the methods for certain
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    49
 * types and ranges, but similar properties are expected to hold, at
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    50
 * least approximately, for others as well.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    51
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    52
 * <p>The class {@code Xoshiro256StarStar} uses the {@code xoshiro256} algorithm,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    53
 * version 1.0 (parameters 17, 45), with the "**" scrambler (a mixing function).
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    54
 * Its state consists of four {@code long} fields {@code x0}, {@code x1}, {@code x2},
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    55
 * and {@code x3}, which can take on any values provided that they are not all zero.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    56
 * The period of this generator is 2<sup>256</sup>-1.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    57
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    58
 * <p>The 64-bit values produced by the {@code nextLong()} method are equidistributed.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    59
 * To be precise, over the course of the cycle of length 2<sup>256</sup>-1,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    60
 * each nonzero {@code long} value is generated 2<sup>192</sup> times,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    61
 * but the value 0 is generated only 2<sup>192</sup>-1 times.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    62
 * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    63
 * methods are likewise equidistributed.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    64
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    65
 * <p>In fact, the 64-bit values produced by the {@code nextLong()} method are 4-equidistributed.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    66
 * To be precise: consider the (overlapping) length-4 subsequences of the cycle of 64-bit
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    67
 * values produced by {@code nextLong()} (assuming no other methods are called that would
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    68
 * affect the state).  There are 2<sup>256</sup>-1 such subsequences, and each subsequence,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    69
 * which consists of 4 64-bit values, can have one of 2<sup>256</sup> values.  Of those
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    70
 * 2<sup>256</sup> subsequence values, each one is generated exactly once over the course
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    71
 * of the entire cycle, except that the subsequence (0, 0, 0, 0) never appears.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    72
 * The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    73
 * methods are likewise 4-equidistributed, but note that that the subsequence (0, 0, 0, 0)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    74
 * can also appear (but occurring somewhat less frequently than all other subsequences),
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    75
 * because the values produced by those methods have fewer than 64 randomly chosen bits.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    76
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    77
 * <p>Instances {@code Xoshiro256StarStar} are <em>not</em> thread-safe.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    78
 * They are designed to be used so that each thread as its own instance.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    79
 * The methods {@link #jump} and {@link #leap} and {@link #jumps} and {@link #leaps}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    80
 * can be used to construct new instances of {@code Xoshiro256StarStar} that traverse
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    81
 * other parts of the state cycle.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    82
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    83
 * <p>Instances of {@code Xoshiro256StarStar} are not cryptographically
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    84
 * secure.  Consider instead using {@link java.security.SecureRandom}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    85
 * in security-sensitive applications. Additionally,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    86
 * default-constructed instances do not use a cryptographically random
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    87
 * seed unless the {@linkplain System#getProperty system property}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    88
 * {@code java.util.secureRandomSeed} is set to {@code true}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    89
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    90
 * @author  Guy Steele
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    91
 * @since   1.9
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    92
 */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    93
public final class Xoshiro256StarStar implements LeapableRng {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    94
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    95
    /*
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    96
     * Implementation Overview.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    97
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    98
     * This is an implementation of the xoroshiro128** algorithm written
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    99
     * in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org).
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   100
     * See http://xoshiro.di.unimi.it and these two papers:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   101
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   102
     *    Sebastiano Vigna. 2016. An Experimental Exploration of Marsaglia's
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   103
     *    xorshift Generators, Scrambled. ACM Transactions on Mathematical
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   104
     *    Software 42, 4, Article 30 (June 2016), 23 pages.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   105
     *    https://doi.org/10.1145/2845077
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   106
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   107
     *    David Blackman and Sebastiano Vigna.  2018.  Scrambled Linear
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   108
     *    Pseudorandom Number Generators.  Computing Research Repository (CoRR).
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   109
     *    http://arxiv.org/abs/1805.01407
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   110
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   111
     * The jump operation moves the current generator forward by 2*128
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   112
     * steps; this has the same effect as calling nextLong() 2**128
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   113
     * times, but is much faster.  Similarly, the leap operation moves
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   114
     * the current generator forward by 2*192 steps; this has the same
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   115
     * effect as calling nextLong() 2**192 times, but is much faster.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   116
     * The copy method may be used to make a copy of the current
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   117
     * generator.  Thus one may repeatedly and cumulatively copy and
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   118
     * jump to produce a sequence of generators whose states are well
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   119
     * spaced apart along the overall state cycle (indeed, the jumps()
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   120
     * and leaps() methods each produce a stream of such generators).
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   121
     * The generators can then be parceled out to other threads.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   122
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   123
     * File organization: First static fields, then instance
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   124
     * fields, then constructors, then instance methods.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   125
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   126
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   127
    /* ---------------- static fields ---------------- */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   128
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   129
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   130
     * The seed generator for default constructors.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   131
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   132
    private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   133
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   134
    /*
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   135
     * The period of this generator, which is 2**256 - 1.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   136
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   137
    private static final BigInteger thePeriod =
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   138
	BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   139
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   140
    /* ---------------- instance fields ---------------- */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   141
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   142
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   143
     * The per-instance state.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   144
     * At least one of the four fields x0, x1, x2, and x3 must be nonzero.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   145
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   146
    private long x0, x1, x2, x3;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   147
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   148
    /* ---------------- constructors ---------------- */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   149
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   150
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   151
     * Basic constructor that initializes all fields from parameters.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   152
     * It then adjusts the field values if necessary to ensure that
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   153
     * all constraints on the values of fields are met.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   154
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   155
    public Xoshiro256StarStar(long x0, long x1, long x2, long x3) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   156
	this.x0 = x0;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   157
        this.x1 = x1;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   158
        this.x2 = x2;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   159
        this.x3 = x3;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   160
	// If x0, x1, x2, and x3 are all zero, we must choose nonzero values.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   161
        if ((x0 | x1 | x2 | x3) == 0) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   162
	    // At least three of the four values generated here will be nonzero.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   163
	    this.x0 = RngSupport.mixStafford13(x0 += RngSupport.GOLDEN_RATIO_64);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   164
	    this.x1 = (x0 += RngSupport.GOLDEN_RATIO_64);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   165
	    this.x2 = (x0 += RngSupport.GOLDEN_RATIO_64);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   166
	    this.x3 = (x0 += RngSupport.GOLDEN_RATIO_64);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   167
	}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   168
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   169
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   170
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   171
     * Creates a new instance of {@code Xoshiro256StarStar} using the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   172
     * specified {@code long} value as the initial seed. Instances of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   173
     * {@code Xoshiro256StarStar} created with the same seed in the same
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   174
     * program generate identical sequences of values.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   175
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   176
     * @param seed the initial seed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   177
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   178
    public Xoshiro256StarStar(long seed) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   179
	// Using a value with irregularly spaced 1-bits to xor the seed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   180
	// argument tends to improve "pedestrian" seeds such as 0 or
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   181
	// other small integers.  We may as well use SILVER_RATIO_64.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   182
	//
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   183
	// The x values are then filled in as if by a SplitMix PRNG with
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   184
	// GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   185
        this(RngSupport.mixStafford13(seed ^= RngSupport.SILVER_RATIO_64),
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   186
	     RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   187
	     RngSupport.mixStafford13(seed += RngSupport.GOLDEN_RATIO_64),
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   188
	     RngSupport.mixStafford13(seed + RngSupport.GOLDEN_RATIO_64));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   189
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   190
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   191
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   192
     * Creates a new instance of {@code Xoshiro256StarStar} that is likely to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   193
     * generate sequences of values that are statistically independent
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   194
     * of those of any other instances in the current program execution,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   195
     * but may, and typically does, vary across program invocations.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   196
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   197
    public Xoshiro256StarStar() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   198
	// Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   199
        this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   200
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   201
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   202
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   203
     * Creates a new instance of {@code Xoshiro256StarStar} using the specified array of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   204
     * initial seed bytes. Instances of {@code Xoshiro256StarStar} created with the same
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   205
     * seed array in the same program execution generate identical sequences of values.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   206
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   207
     * @param seed the initial seed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   208
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   209
    public Xoshiro256StarStar(byte[] seed) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   210
	// Convert the seed to 4 long values, which are not all zero.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   211
	long[] data = RngSupport.convertSeedBytesToLongs(seed, 4, 4);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   212
	long x0 = data[0], x1 = data[1], x2 = data[2], x3 = data[3];
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   213
        this.x0 = x0;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   214
        this.x1 = x1;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   215
        this.x2 = x2;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   216
        this.x3 = x3;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   217
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   218
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   219
    /* ---------------- public methods ---------------- */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   220
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   221
    public Xoshiro256StarStar copy() { return new Xoshiro256StarStar(x0, x1, x2, x3); }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   222
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   223
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   224
     * Returns a pseudorandom {@code long} value.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   225
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   226
     * @return a pseudorandom {@code long} value
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   227
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   228
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   229
    public long nextLong() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   230
	final long z = x0;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   231
	long q0 = x0, q1 = x1, q2 = x2, q3 = x3;	
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   232
	{ long t = q1 << 17; q2 ^= q0; q3 ^= q1; q1 ^= q2; q0 ^= q3; q2 ^= t; q3 = Long.rotateLeft(q3, 45); }  // xoshiro256 1.0
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   233
	x0 = q0; x1 = q1; x2 = q2; x3 = q3;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   234
	return Long.rotateLeft(z * 5, 7) * 9;  // "starstar" mixing function
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   235
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   236
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   237
    public BigInteger period() { return thePeriod; }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   238
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   239
    
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   240
    public double defaultJumpDistance() { return 0x1.0p64; }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   241
    public double defaultLeapDistance() { return 0x1.0p96; }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   242
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   243
    private static final long[] JUMP_TABLE = {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   244
	0x180ec6d33cfd0abaL, 0xd5a61266f0c9392cL, 0xa9582618e03fc9aaL, 0x39abdc4529b1661cL };
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   245
    
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   246
    private static final long[] LEAP_TABLE = {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   247
	0x76e15d3efefdcbbfL, 0xc5004e441c522fb3L, 0x77710069854ee241L, 0x39109bb02acbe635L };
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   248
   
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   249
/* This is the jump function for the generator. It is equivalent
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   250
   to 2**128 calls to next(); it can be used to generate 2**128
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   251
   non-overlapping subsequences for parallel computations. */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   252
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   253
    public void jump() { jumpAlgorithm(JUMP_TABLE); }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   254
    
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   255
/* This is the long-jump function for the generator. It is equivalent to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   256
   2**192 calls to next(); it can be used to generate 2**64 starting points,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   257
   from each of which jump() will generate 2**64 non-overlapping
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   258
   subsequences for parallel distributed computations. */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   259
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   260
    public void leap() { jumpAlgorithm(LEAP_TABLE); }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   261
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   262
    private void jumpAlgorithm(long[] table) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   263
	long s0 = 0, s1 = 0, s2 = 0, s3 = 0;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   264
	for (int i = 0; i < table.length; i++) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   265
	    for (int b = 0; b < 64; b++) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   266
		if ((table[i] & (1L << b)) != 0) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   267
		    s0 ^= x0;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   268
		    s1 ^= x1;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   269
		    s2 ^= x2;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   270
		    s3 ^= x3;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   271
		}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   272
		nextLong();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   273
	    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   274
	    x0 = s0;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   275
	    x1 = s1;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   276
	    x2 = s2;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   277
	    x3 = s3;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   278
	}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   279
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   280
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   281
}