newrandom/Random.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) 1995, 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.io.*;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    29
import java.math.BigInteger;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    30
import java.util.concurrent.atomic.AtomicLong;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    31
import java.util.function.DoubleConsumer;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    32
import java.util.function.IntConsumer;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    33
import java.util.function.LongConsumer;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    34
import java.util.stream.DoubleStream;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    35
import java.util.stream.IntStream;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    36
import java.util.stream.LongStream;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    37
import java.util.stream.StreamSupport;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    38
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    39
import sun.misc.Unsafe;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    40
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    41
/**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    42
 * An instance of this class is used to generate a stream of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    43
 * pseudorandom numbers. The class uses a 48-bit seed, which is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    44
 * modified using a linear congruential formula. (See Donald Knuth,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    45
 * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    46
 * <p>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    47
 * If two instances of {@code Random} are created with the same
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    48
 * seed, and the same sequence of method calls is made for each, they
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    49
 * will generate and return identical sequences of numbers. In order to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    50
 * guarantee this property, particular algorithms are specified for the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    51
 * class {@code Random}. Java implementations must use all the algorithms
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    52
 * shown here for the class {@code Random}, for the sake of absolute
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    53
 * portability of Java code. However, subclasses of class {@code Random}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    54
 * are permitted to use other algorithms, so long as they adhere to the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    55
 * general contracts for all the methods.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    56
 * <p>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    57
 * The algorithms implemented by class {@code Random} use a
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    58
 * {@code protected} utility method that on each invocation can supply
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    59
 * up to 32 pseudorandomly generated bits.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    60
 * <p>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    61
 * Many applications will find the method {@link Math#random} simpler to use.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    62
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    63
 * <p>Instances of {@code java.util.Random} are threadsafe.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    64
 * However, the concurrent use of the same {@code java.util.Random}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    65
 * instance across threads may encounter contention and consequent
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    66
 * poor performance. Consider instead using
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    67
 * {@link java.util.concurrent.ThreadLocalRandom} in multithreaded
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    68
 * designs.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    69
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    70
 * <p>Instances of {@code java.util.Random} are not cryptographically
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    71
 * secure.  Consider instead using {@link java.security.SecureRandom} to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    72
 * get a cryptographically secure pseudo-random number generator for use
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    73
 * by security-sensitive applications.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    74
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    75
 * @author  Frank Yellin
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    76
 * @since   1.0
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    77
 */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    78
public
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    79
    class Random extends AbstractSharedRng implements java.io.Serializable {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    80
    /** use serialVersionUID from JDK 1.1 for interoperability */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    81
    static final long serialVersionUID = 3905348978240129619L;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    82
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    83
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    84
     * The internal state associated with this pseudorandom number generator.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    85
     * (The specs for the methods in this class describe the ongoing
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    86
     * computation of this value.)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    87
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    88
    private final AtomicLong seed;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    89
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    90
    private static final long multiplier = 0x5DEECE66DL;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    91
    private static final long addend = 0xBL;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    92
    private static final long mask = (1L << 48) - 1;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    93
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    94
    private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    95
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    96
    // IllegalArgumentException messages
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    97
    static final String BadBound = "bound must be positive";
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    98
    static final String BadRange = "bound must be greater than origin";
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    99
    static final String BadSize  = "size must be non-negative";
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   100
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   101
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   102
     * Creates a new random number generator. This constructor sets
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   103
     * the seed of the random number generator to a value very likely
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   104
     * to be distinct from any other invocation of this constructor.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   105
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   106
    public Random() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   107
        this(seedUniquifier() ^ System.nanoTime());
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   108
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   109
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   110
    private static long seedUniquifier() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   111
        // L'Ecuyer, "Tables of Linear Congruential Generators of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   112
        // Different Sizes and Good Lattice Structure", 1999
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   113
        for (;;) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   114
            long current = seedUniquifier.get();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   115
            long next = current * 181783497276652981L;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   116
            if (seedUniquifier.compareAndSet(current, next))
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   117
                return next;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   118
        }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   119
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   120
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   121
    private static final AtomicLong seedUniquifier
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   122
        = new AtomicLong(8682522807148012L);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   123
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   124
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   125
     * Creates a new random number generator using a single {@code long} seed.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   126
     * The seed is the initial value of the internal state of the pseudorandom
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   127
     * number generator which is maintained by method {@link #next}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   128
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   129
     * <p>The invocation {@code new Random(seed)} is equivalent to:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   130
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   131
     * Random rnd = new Random();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   132
     * rnd.setSeed(seed);}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   133
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   134
     * @param seed the initial seed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   135
     * @see   #setSeed(long)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   136
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   137
    public Random(long seed) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   138
        if (getClass() == Random.class)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   139
            this.seed = new AtomicLong(initialScramble(seed));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   140
        else {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   141
            // subclass might have overriden setSeed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   142
            this.seed = new AtomicLong();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   143
            setSeed(seed);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   144
        }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   145
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   146
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   147
    private static long initialScramble(long seed) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   148
        return (seed ^ multiplier) & mask;
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
     * Sets the seed of this random number generator using a single
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   153
     * {@code long} seed. The general contract of {@code setSeed} is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   154
     * that it alters the state of this random number generator object
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   155
     * so as to be in exactly the same state as if it had just been
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   156
     * created with the argument {@code seed} as a seed. The method
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   157
     * {@code setSeed} is implemented by class {@code Random} by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   158
     * atomically updating the seed to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   159
     *  <pre>{@code (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   160
     * and clearing the {@code haveNextNextGaussian} flag used by {@link
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   161
     * #nextGaussian}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   162
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   163
     * <p>The implementation of {@code setSeed} by class {@code Random}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   164
     * happens to use only 48 bits of the given seed. In general, however,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   165
     * an overriding method may use all 64 bits of the {@code long}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   166
     * argument as a seed value.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   167
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   168
     * @param seed the initial seed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   169
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   170
    synchronized public void setSeed(long seed) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   171
        this.seed.set(initialScramble(seed));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   172
        haveNextNextGaussian = false;
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
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   176
     * Generates the next pseudorandom number. Subclasses should
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   177
     * override this, as this is used by all other methods.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   178
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   179
     * <p>The general contract of {@code next} is that it returns an
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   180
     * {@code int} value and if the argument {@code bits} is between
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   181
     * {@code 1} and {@code 32} (inclusive), then that many low-order
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   182
     * bits of the returned value will be (approximately) independently
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   183
     * chosen bit values, each of which is (approximately) equally
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   184
     * likely to be {@code 0} or {@code 1}. The method {@code next} is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   185
     * implemented by class {@code Random} by atomically updating the seed to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   186
     *  <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   187
     * and returning
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   188
     *  <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   189
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   190
     * This is a linear congruential pseudorandom number generator, as
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   191
     * defined by D. H. Lehmer and described by Donald E. Knuth in
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   192
     * <i>The Art of Computer Programming,</i> Volume 3:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   193
     * <i>Seminumerical Algorithms</i>, section 3.2.1.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   194
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   195
     * @param  bits random bits
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   196
     * @return the next pseudorandom value from this random number
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   197
     *         generator's sequence
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   198
     * @since  1.1
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   199
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   200
    protected int next(int bits) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   201
        long oldseed, nextseed;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   202
        AtomicLong seed = this.seed;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   203
        do {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   204
            oldseed = seed.get();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   205
            nextseed = (oldseed * multiplier + addend) & mask;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   206
        } while (!seed.compareAndSet(oldseed, nextseed));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   207
        return (int)(nextseed >>> (48 - bits));
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
    static final BigInteger thePeriod = BigInteger.valueOf(1L<<48);  // Period is 2**48
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   211
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   212
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   213
     * Returns the period of this random number generator.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   214
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   215
     * @return the period of this random number generator.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   216
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   217
    public BigInteger period() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   218
	// Here we also take care of checking for instances of class SecureRandom,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   219
	// just so as not to bother the implementors of that class.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   220
	// (Any specific instance of SecureRandom can of course override this method.)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   221
	// The cast to (Object) is of course needed only during development.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   222
	return ((Object)this instanceof java.security.SecureRandom) ? Rng.HUGE_PERIOD : thePeriod;
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
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   226
     * Generates random bytes and places them into a user-supplied
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   227
     * byte array.  The number of random bytes produced is equal to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   228
     * the length of the byte array.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   229
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   230
     * <p>The method {@code nextBytes} is implemented by class {@code Random}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   231
     * as if by:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   232
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   233
     * public void nextBytes(byte[] bytes) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   234
     *   for (int i = 0; i < bytes.length; )
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   235
     *     for (int rnd = nextInt(), n = Math.min(bytes.length - i, 4);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   236
     *          n-- > 0; rnd >>= 8)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   237
     *       bytes[i++] = (byte)rnd;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   238
     * }}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   239
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   240
     * @param  bytes the byte array to fill with random bytes
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   241
     * @throws NullPointerException if the byte array is null
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   242
     * @since  1.1
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   243
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   244
    public void nextBytes(byte[] bytes) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   245
        for (int i = 0, len = bytes.length; i < len; )
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   246
            for (int rnd = nextInt(),
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   247
                     n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   248
                 n-- > 0; rnd >>= Byte.SIZE)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   249
                bytes[i++] = (byte)rnd;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   250
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   251
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   252
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   253
     * Returns the next pseudorandom, uniformly distributed {@code int}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   254
     * value from this random number generator's sequence. The general
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   255
     * contract of {@code nextInt} is that one {@code int} value is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   256
     * pseudorandomly generated and returned. All 2<sup>32</sup> possible
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   257
     * {@code int} values are produced with (approximately) equal probability.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   258
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   259
     * <p>The method {@code nextInt} is implemented by class {@code Random}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   260
     * as if by:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   261
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   262
     * public int nextInt() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   263
     *   return next(32);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   264
     * }}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   265
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   266
     * @return the next pseudorandom, uniformly distributed {@code int}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   267
     *         value from this random number generator's sequence
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   268
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   269
    public int nextInt() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   270
        return next(32);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   271
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   272
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   273
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   274
     * Returns a pseudorandom {@code int} value between zero (inclusive)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   275
     * and the specified bound (exclusive).
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   276
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   277
     * @param bound the upper bound (exclusive).  Must be positive.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   278
     * @return a pseudorandom {@code int} value between zero
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   279
     *         (inclusive) and the bound (exclusive)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   280
     * @throws IllegalArgumentException if {@code bound} is not positive
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   281
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   282
    public int nextInt(int bound) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   283
        if (bound <= 0)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   284
            throw new IllegalArgumentException(BadBound);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   285
        // Specialize internalNextInt for origin 0
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   286
        int r = nextInt();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   287
        int m = bound - 1;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   288
        if ((bound & m) == 0) // power of two
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   289
            r &= m;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   290
        else { // reject over-represented candidates
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   291
            for (int u = r >>> 1;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   292
                 u + m - (r = u % bound) < 0;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   293
                 u = nextInt() >>> 1)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   294
                ;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   295
        }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   296
        return r;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   297
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   298
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   299
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   300
     * Returns the next pseudorandom, uniformly distributed {@code long}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   301
     * value from this random number generator's sequence. The general
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   302
     * contract of {@code nextLong} is that one {@code long} value is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   303
     * pseudorandomly generated and returned.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   304
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   305
     * <p>The method {@code nextLong} is implemented by class {@code Random}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   306
     * as if by:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   307
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   308
     * public long nextLong() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   309
     *   return ((long)next(32) << 32) + next(32);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   310
     * }}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   311
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   312
     * Because class {@code Random} uses a seed with only 48 bits,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   313
     * this algorithm will not return all possible {@code long} values.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   314
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   315
     * @return the next pseudorandom, uniformly distributed {@code long}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   316
     *         value from this random number generator's sequence
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   317
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   318
    public long nextLong() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   319
        // it's okay that the bottom word remains signed.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   320
        return ((long)(next(32)) << 32) + next(32);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   321
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   322
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   323
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   324
     * Returns the next pseudorandom, uniformly distributed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   325
     * {@code boolean} value from this random number generator's
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   326
     * sequence. The general contract of {@code nextBoolean} is that one
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   327
     * {@code boolean} value is pseudorandomly generated and returned.  The
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   328
     * values {@code true} and {@code false} are produced with
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   329
     * (approximately) equal probability.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   330
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   331
     * <p>The method {@code nextBoolean} is implemented by class {@code Random}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   332
     * as if by:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   333
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   334
     * public boolean nextBoolean() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   335
     *   return next(1) != 0;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   336
     * }}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   337
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   338
     * @return the next pseudorandom, uniformly distributed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   339
     *         {@code boolean} value from this random number generator's
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   340
     *         sequence
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   341
     * @since 1.2
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   342
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   343
    public boolean nextBoolean() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   344
        return next(1) != 0;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   345
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   346
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   347
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   348
     * Returns the next pseudorandom, uniformly distributed {@code float}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   349
     * value between {@code 0.0} and {@code 1.0} from this random
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   350
     * number generator's sequence.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   351
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   352
     * <p>The general contract of {@code nextFloat} is that one
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   353
     * {@code float} value, chosen (approximately) uniformly from the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   354
     * range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   355
     * pseudorandomly generated and returned. All 2<sup>24</sup> possible
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   356
     * {@code float} values of the form <i>m&nbsp;x&nbsp;</i>2<sup>-24</sup>,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   357
     * where <i>m</i> is a positive integer less than 2<sup>24</sup>, are
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   358
     * produced with (approximately) equal probability.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   359
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   360
     * <p>The method {@code nextFloat} is implemented by class {@code Random}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   361
     * as if by:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   362
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   363
     * public float nextFloat() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   364
     *   return next(24) / ((float)(1 << 24));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   365
     * }}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   366
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   367
     * <p>The hedge "approximately" is used in the foregoing description only
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   368
     * because the next method is only approximately an unbiased source of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   369
     * independently chosen bits. If it were a perfect source of randomly
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   370
     * chosen bits, then the algorithm shown would choose {@code float}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   371
     * values from the stated range with perfect uniformity.<p>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   372
     * [In early versions of Java, the result was incorrectly calculated as:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   373
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   374
     *   return next(30) / ((float)(1 << 30));}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   375
     * This might seem to be equivalent, if not better, but in fact it
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   376
     * introduced a slight nonuniformity because of the bias in the rounding
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   377
     * of floating-point numbers: it was slightly more likely that the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   378
     * low-order bit of the significand would be 0 than that it would be 1.]
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   379
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   380
     * @return the next pseudorandom, uniformly distributed {@code float}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   381
     *         value between {@code 0.0} and {@code 1.0} from this
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   382
     *         random number generator's sequence
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   383
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   384
    public float nextFloat() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   385
        return next(24) / ((float)(1 << 24));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   386
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   387
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   388
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   389
     * Returns the next pseudorandom, uniformly distributed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   390
     * {@code double} value between {@code 0.0} and
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   391
     * {@code 1.0} from this random number generator's sequence.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   392
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   393
     * <p>The general contract of {@code nextDouble} is that one
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   394
     * {@code double} value, chosen (approximately) uniformly from the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   395
     * range {@code 0.0d} (inclusive) to {@code 1.0d} (exclusive), is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   396
     * pseudorandomly generated and returned.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   397
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   398
     * <p>The method {@code nextDouble} is implemented by class {@code Random}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   399
     * as if by:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   400
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   401
     * public double nextDouble() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   402
     *   return (((long)next(26) << 27) + next(27))
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   403
     *     / (double)(1L << 53);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   404
     * }}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   405
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   406
     * <p>The hedge "approximately" is used in the foregoing description only
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   407
     * because the {@code next} method is only approximately an unbiased
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   408
     * source of independently chosen bits. If it were a perfect source of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   409
     * randomly chosen bits, then the algorithm shown would choose
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   410
     * {@code double} values from the stated range with perfect uniformity.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   411
     * <p>[In early versions of Java, the result was incorrectly calculated as:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   412
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   413
     *   return (((long)next(27) << 27) + next(27))
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   414
     *     / (double)(1L << 54);}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   415
     * This might seem to be equivalent, if not better, but in fact it
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   416
     * introduced a large nonuniformity because of the bias in the rounding
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   417
     * of floating-point numbers: it was three times as likely that the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   418
     * low-order bit of the significand would be 0 than that it would be 1!
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   419
     * This nonuniformity probably doesn't matter much in practice, but we
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   420
     * strive for perfection.]
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   421
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   422
     * @return the next pseudorandom, uniformly distributed {@code double}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   423
     *         value between {@code 0.0} and {@code 1.0} from this
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   424
     *         random number generator's sequence
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   425
     * @see Math#random
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   426
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   427
    public double nextDouble() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   428
        return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   429
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   430
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   431
    private double nextNextGaussian;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   432
    private boolean haveNextNextGaussian = false;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   433
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   434
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   435
     * Returns the next pseudorandom, Gaussian ("normally") distributed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   436
     * {@code double} value with mean {@code 0.0} and standard
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   437
     * deviation {@code 1.0} from this random number generator's sequence.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   438
     * <p>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   439
     * The general contract of {@code nextGaussian} is that one
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   440
     * {@code double} value, chosen from (approximately) the usual
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   441
     * normal distribution with mean {@code 0.0} and standard deviation
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   442
     * {@code 1.0}, is pseudorandomly generated and returned.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   443
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   444
     * <p>The method {@code nextGaussian} is implemented by class
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   445
     * {@code Random} as if by a threadsafe version of the following:
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   446
     *  <pre> {@code
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   447
     * private double nextNextGaussian;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   448
     * private boolean haveNextNextGaussian = false;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   449
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   450
     * public double nextGaussian() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   451
     *   if (haveNextNextGaussian) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   452
     *     haveNextNextGaussian = false;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   453
     *     return nextNextGaussian;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   454
     *   } else {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   455
     *     double v1, v2, s;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   456
     *     do {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   457
     *       v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   458
     *       v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   459
     *       s = v1 * v1 + v2 * v2;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   460
     *     } while (s >= 1 || s == 0);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   461
     *     double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   462
     *     nextNextGaussian = v2 * multiplier;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   463
     *     haveNextNextGaussian = true;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   464
     *     return v1 * multiplier;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   465
     *   }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   466
     * }}</pre>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   467
     * This uses the <i>polar method</i> of G. E. P. Box, M. E. Muller, and
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   468
     * G. Marsaglia, as described by Donald E. Knuth in <i>The Art of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   469
     * Computer Programming</i>, Volume 3: <i>Seminumerical Algorithms</i>,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   470
     * section 3.4.1, subsection C, algorithm P. Note that it generates two
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   471
     * independent values at the cost of only one call to {@code StrictMath.log}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   472
     * and one call to {@code StrictMath.sqrt}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   473
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   474
     * @return the next pseudorandom, Gaussian ("normally") distributed
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   475
     *         {@code double} value with mean {@code 0.0} and
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   476
     *         standard deviation {@code 1.0} from this random number
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   477
     *         generator's sequence
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   478
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   479
    synchronized public double nextGaussian() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   480
        // See Knuth, ACP, Section 3.4.1 Algorithm C.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   481
        if (haveNextNextGaussian) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   482
            haveNextNextGaussian = false;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   483
            return nextNextGaussian;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   484
        } else {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   485
            double v1, v2, s;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   486
            do {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   487
                v1 = 2 * nextDouble() - 1; // between -1 and 1
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   488
                v2 = 2 * nextDouble() - 1; // between -1 and 1
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   489
                s = v1 * v1 + v2 * v2;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   490
            } while (s >= 1 || s == 0);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   491
            double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   492
            nextNextGaussian = v2 * multiplier;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   493
            haveNextNextGaussian = true;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   494
            return v1 * multiplier;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   495
        }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   496
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   497
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   498
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   499
     * Serializable fields for Random.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   500
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   501
     * @serialField    seed long
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   502
     *              seed for random computations
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   503
     * @serialField    nextNextGaussian double
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   504
     *              next Gaussian to be returned
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   505
     * @serialField      haveNextNextGaussian boolean
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   506
     *              nextNextGaussian is valid
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   507
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   508
    private static final ObjectStreamField[] serialPersistentFields = {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   509
        new ObjectStreamField("seed", Long.TYPE),
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   510
        new ObjectStreamField("nextNextGaussian", Double.TYPE),
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   511
        new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   512
    };
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   513
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   514
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   515
     * Reconstitute the {@code Random} instance from a stream (that is,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   516
     * deserialize it).
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   517
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   518
    private void readObject(java.io.ObjectInputStream s)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   519
        throws java.io.IOException, ClassNotFoundException {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   520
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   521
        ObjectInputStream.GetField fields = s.readFields();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   522
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   523
        // The seed is read in as {@code long} for
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   524
        // historical reasons, but it is converted to an AtomicLong.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   525
        long seedVal = fields.get("seed", -1L);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   526
        if (seedVal < 0)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   527
          throw new java.io.StreamCorruptedException(
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   528
                              "Random: invalid seed");
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   529
        resetSeed(seedVal);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   530
        nextNextGaussian = fields.get("nextNextGaussian", 0.0);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   531
        haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   532
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   533
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   534
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   535
     * Save the {@code Random} instance to a stream.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   536
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   537
    synchronized private void writeObject(ObjectOutputStream s)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   538
        throws IOException {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   539
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   540
        // set the values of the Serializable fields
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   541
        ObjectOutputStream.PutField fields = s.putFields();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   542
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   543
        // The seed is serialized as a long for historical reasons.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   544
        fields.put("seed", seed.get());
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   545
        fields.put("nextNextGaussian", nextNextGaussian);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   546
        fields.put("haveNextNextGaussian", haveNextNextGaussian);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   547
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   548
        // save them
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   549
        s.writeFields();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   550
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   551
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   552
    // Support for resetting seed while deserializing
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   553
    private static final Unsafe unsafe = Unsafe.getUnsafe();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   554
    private static final long seedOffset;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   555
    static {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   556
        try {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   557
            seedOffset = unsafe.objectFieldOffset
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   558
                (Random.class.getDeclaredField("seed"));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   559
        } catch (Exception ex) { throw new Error(ex); }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   560
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   561
    private void resetSeed(long seedVal) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   562
        unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   563
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   564
}