newrandom/SplittableRandom.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) 2013, 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 SplittableRandom} supports methods for
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    35
 * producing pseudorandom numbers of type {@code int}, {@code long},
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    36
 * and {@code double} with similar usages as for class
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    37
 * {@link java.util.Random} but differs in the following ways:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    38
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    39
 * <ul>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    40
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    41
 * <li>Series of generated values pass the DieHarder suite testing
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    42
 * independence and uniformity properties of random number generators.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    43
 * (Most recently validated with <a
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    44
 * href="http://www.phy.duke.edu/~rgb/General/dieharder.php"> version
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    45
 * 3.31.1</a>.) These tests validate only the methods for certain
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    46
 * types and ranges, but similar properties are expected to hold, at
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    47
 * least approximately, for others as well. The <em>period</em>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    48
 * (length of any series of generated values before it repeats) is at
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    49
 * least 2<sup>64</sup>. </li>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    50
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    51
 * <li> Method {@link #split} constructs and returns a new
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    52
 * SplittableRandom instance that shares no mutable state with the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    53
 * current instance. However, with very high probability, the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    54
 * values collectively generated by the two objects have the same
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    55
 * statistical properties as if the same quantity of values were
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    56
 * generated by a single thread using a single {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    57
 * SplittableRandom} object.  </li>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    58
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    59
 * <li>Instances of SplittableRandom are <em>not</em> thread-safe.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    60
 * They are designed to be split, not shared, across threads. For
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    61
 * example, a {@link java.util.concurrent.ForkJoinTask
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    62
 * fork/join-style} computation using random numbers might include a
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    63
 * construction of the form {@code new
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    64
 * Subtask(aSplittableRandom.split()).fork()}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    65
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    66
 * <li>This class provides additional methods for generating random
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    67
 * streams, that employ the above techniques when used in {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    68
 * stream.parallel()} mode.</li>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    69
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    70
 * </ul>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    71
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    72
 * <p>Instances of {@code SplittableRandom} are not cryptographically
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    73
 * secure.  Consider instead using {@link java.security.SecureRandom}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    74
 * in security-sensitive applications. Additionally,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    75
 * default-constructed instances do not use a cryptographically random
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    76
 * seed unless the {@linkplain System#getProperty system property}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    77
 * {@code java.util.secureRandomSeed} is set to {@code true}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    78
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    79
 * @author  Guy Steele
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    80
 * @author  Doug Lea
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    81
 * @since   1.8
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    82
 */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    83
public final class SplittableRandom extends AbstractSplittableRng {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    84
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    85
    /*
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    86
     * Implementation Overview.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    87
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    88
     * This algorithm was inspired by the "DotMix" algorithm by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    89
     * Leiserson, Schardl, and Sukha "Deterministic Parallel
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    90
     * Random-Number Generation for Dynamic-Multithreading Platforms",
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    91
     * PPoPP 2012, as well as those in "Parallel random numbers: as
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    92
     * easy as 1, 2, 3" by Salmon, Morae, Dror, and Shaw, SC 2011.  It
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    93
     * differs mainly in simplifying and cheapening operations.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    94
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    95
     * The primary update step (method nextSeed()) is to add a
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    96
     * constant ("gamma") to the current (64 bit) seed, forming a
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    97
     * simple sequence.  The seed and the gamma values for any two
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    98
     * SplittableRandom instances are highly likely to be different.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    99
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   100
     * Methods nextLong, nextInt, and derivatives do not return the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   101
     * sequence (seed) values, but instead a hash-like bit-mix of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   102
     * their bits, producing more independently distributed sequences.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   103
     * For nextLong, the mix64 function is based on David Stafford's
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   104
     * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   105
     * "Mix13" variant of the "64-bit finalizer" function in Austin
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   106
     * Appleby's MurmurHash3 algorithm (see
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   107
     * http://code.google.com/p/smhasher/wiki/MurmurHash3). The mix32
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   108
     * function is based on Stafford's Mix04 mix function, but returns
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   109
     * the upper 32 bits cast as int.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   110
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   111
     * The split operation uses the current generator to form the seed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   112
     * and gamma for another SplittableRandom.  To conservatively
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   113
     * avoid potential correlations between seed and value generation,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   114
     * gamma selection (method mixGamma) uses different
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   115
     * (Murmurhash3's) mix constants.  To avoid potential weaknesses
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   116
     * in bit-mixing transformations, we restrict gammas to odd values
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   117
     * with at least 24 0-1 or 1-0 bit transitions.  Rather than
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   118
     * rejecting candidates with too few or too many bits set, method
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   119
     * mixGamma flips some bits (which has the effect of mapping at
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   120
     * most 4 to any given gamma value).  This reduces the effective
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   121
     * set of 64bit odd gamma values by about 2%, and serves as an
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   122
     * automated screening for sequence constant selection that is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   123
     * left as an empirical decision in some other hashing and crypto
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   124
     * algorithms.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   125
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   126
     * The resulting generator thus transforms a sequence in which
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   127
     * (typically) many bits change on each step, with an inexpensive
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   128
     * mixer with good (but less than cryptographically secure)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   129
     * avalanching.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   130
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   131
     * The default (no-argument) constructor, in essence, invokes
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   132
     * split() for a common "defaultGen" SplittableRandom.  Unlike
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   133
     * other cases, this split must be performed in a thread-safe
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   134
     * manner, so we use an AtomicLong to represent the seed rather
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   135
     * than use an explicit SplittableRandom. To bootstrap the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   136
     * defaultGen, we start off using a seed based on current time
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   137
     * unless the java.util.secureRandomSeed property is set. This
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   138
     * serves as a slimmed-down (and insecure) variant of SecureRandom
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   139
     * that also avoids stalls that may occur when using /dev/random.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   140
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   141
     * It is a relatively simple matter to apply the basic design here
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   142
     * to use 128 bit seeds. However, emulating 128bit arithmetic and
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   143
     * carrying around twice the state add more overhead than appears
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   144
     * warranted for current usages.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   145
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   146
     * File organization: First the non-public methods that constitute
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   147
     * the main algorithm, then the main public methods, followed by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   148
     * some custom spliterator classes needed for stream methods.
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
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   152
     * The golden ratio scaled to 64bits, used as the initial gamma
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   153
     * value for (unsplit) SplittableRandoms.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   154
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   155
    private static final long GOLDEN_GAMMA = 0x9e3779b97f4a7c15L;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   156
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   157
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   158
     * The seed. Updated only via method nextSeed.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   159
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   160
    private long seed;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   161
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   162
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   163
     * The step value.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   164
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   165
    private final long gamma;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   166
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   167
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   168
     * Internal constructor used by all others except default constructor.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   169
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   170
    private SplittableRandom(long seed, long gamma) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   171
        this.seed = seed;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   172
        this.gamma = gamma;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   173
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   174
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   175
    /* The implementation of AbstractSplittableRng requires this. */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   176
    // SplittableRandom getThis() { return this; }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   177
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   178
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   179
     * Computes Stafford variant 13 of 64bit mix function.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   180
     * http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   181
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   182
    private static long mix64(long z) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   183
        z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   184
        z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   185
        return z ^ (z >>> 31);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   186
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   187
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   188
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   189
     * Returns the 32 high bits of Stafford variant 4 mix64 function as int.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   190
     * http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   191
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   192
    private static int mix32(long z) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   193
        z = (z ^ (z >>> 33)) * 0x62a9d9ed799705f5L;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   194
        return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   195
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   196
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   197
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   198
     * Returns the gamma value to use for a new split instance.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   199
     * Uses the 64bit mix function from MurmurHash3.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   200
     * https://github.com/aappleby/smhasher/wiki/MurmurHash3     
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   201
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   202
    private static long mixGamma(long z) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   203
        z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   204
        z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   205
        z = (z ^ (z >>> 33)) | 1L;                  // force to be odd
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   206
        int n = Long.bitCount(z ^ (z >>> 1));       // ensure enough transitions
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   207
        return (n < 24) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   208
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   209
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   210
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   211
     * Adds gamma to seed.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   212
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   213
    private long nextSeed() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   214
        return seed += gamma;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   215
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   216
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   217
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   218
     * The seed generator for default constructors.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   219
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   220
    private static final AtomicLong defaultGen = new AtomicLong(RngSupport.initialSeed());
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   221
    
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   222
    /* ---------------- public methods ---------------- */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   223
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   224
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   225
     * Creates a new SplittableRandom instance using the specified
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   226
     * initial seed. SplittableRandom instances created with the same
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   227
     * seed in the same program generate identical sequences of values.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   228
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   229
     * @param seed the initial seed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   230
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   231
    public SplittableRandom(long seed) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   232
        this(seed, GOLDEN_GAMMA);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   233
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   234
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   235
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   236
     * Creates a new SplittableRandom instance that is likely to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   237
     * generate sequences of values that are statistically independent
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   238
     * of those of any other instances in the current program; and
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   239
     * may, and typically does, vary across program invocations.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   240
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   241
    public SplittableRandom() { // emulate defaultGen.split()
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   242
        long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   243
        this.seed = mix64(s);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   244
        this.gamma = mixGamma(s + GOLDEN_GAMMA);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   245
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   246
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   247
    //    public SplittableRandom copy() { return new SplittableRandom(seed, gamma); }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   248
    
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   249
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   250
     * Constructs and returns a new SplittableRandom instance that
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   251
     * shares no mutable state with this instance. However, with very
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   252
     * high probability, the set of values collectively generated by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   253
     * the two objects has the same statistical properties as if the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   254
     * same quantity of values were generated by a single thread using
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   255
     * a single SplittableRandom object.  Either or both of the two
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   256
     * objects may be further split using the {@code split()} method,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   257
     * and the same expected statistical properties apply to the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   258
     * entire set of generators constructed by such recursive
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   259
     * splitting.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   260
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   261
     * @return the new SplittableRandom instance
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   262
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   263
    public SplittableRandom split() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   264
        return new SplittableRandom(nextLong(), mixGamma(nextSeed()));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   265
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   266
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   267
    public SplittableRandom split(SplittableRng source) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   268
        return new SplittableRandom(source.nextLong(), mixGamma(source.nextLong()));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   269
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   270
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   271
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   272
     * Returns a pseudorandom {@code int} value.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   273
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   274
     * @return a pseudorandom {@code int} value
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   275
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   276
    public int nextInt() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   277
        return mix32(nextSeed());
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
     * Returns a pseudorandom {@code long} value.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   282
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   283
     * @return a pseudorandom {@code long} value
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   284
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   285
    public long nextLong() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   286
        return mix64(nextSeed());
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   287
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   288
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   289
    static final BigInteger thePeriod = BigInteger.ONE.shiftLeft(64);  // Period is 2**64
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   290
    public BigInteger period() { return thePeriod; }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   291
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   292
}