jdk/src/share/classes/java/util/Random.java
author darcy
Wed, 14 Jan 2009 16:23:29 -0800
changeset 1818 7847313afae6
parent 715 f16baef3a20e
child 4110 ac033ba6ede4
permissions -rw-r--r--
6792545: Typo in java.util.Collection JavaDoc 6655123: Incorrect ref to The Art of Computer Programming in doc for java.util.Random Summary: Fix a pair of typos. Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1995-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.concurrent.atomic.AtomicLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An instance of this class is used to generate a stream of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * pseudorandom numbers. The class uses a 48-bit seed, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * modified using a linear congruential formula. (See Donald Knuth,
1818
7847313afae6 6792545: Typo in java.util.Collection JavaDoc
darcy
parents: 715
diff changeset
    35
 * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * If two instances of {@code Random} are created with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * seed, and the same sequence of method calls is made for each, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * will generate and return identical sequences of numbers. In order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * guarantee this property, particular algorithms are specified for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * class {@code Random}. Java implementations must use all the algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * shown here for the class {@code Random}, for the sake of absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * portability of Java code. However, subclasses of class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * are permitted to use other algorithms, so long as they adhere to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * general contracts for all the methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The algorithms implemented by class {@code Random} use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * {@code protected} utility method that on each invocation can supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * up to 32 pseudorandomly generated bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Many applications will find the method {@link Math#random} simpler to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author  Frank Yellin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since   1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
class Random implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** use serialVersionUID from JDK 1.1 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final long serialVersionUID = 3905348978240129619L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * The internal state associated with this pseudorandom number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * (The specs for the methods in this class describe the ongoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * computation of this value.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final AtomicLong seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final static long multiplier = 0x5DEECE66DL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final static long addend = 0xBL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final static long mask = (1L << 48) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Creates a new random number generator. This constructor sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * the seed of the random number generator to a value very likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * to be distinct from any other invocation of this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public Random() { this(++seedUniquifier + System.nanoTime()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static volatile long seedUniquifier = 8682522807148012L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Creates a new random number generator using a single {@code long} seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The seed is the initial value of the internal state of the pseudorandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * number generator which is maintained by method {@link #next}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <p>The invocation {@code new Random(seed)} is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * rnd.setSeed(seed);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param seed the initial seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @see   #setSeed(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public Random(long seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.seed = new AtomicLong(0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        setSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Sets the seed of this random number generator using a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * {@code long} seed. The general contract of {@code setSeed} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * that it alters the state of this random number generator object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * so as to be in exactly the same state as if it had just been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * created with the argument {@code seed} as a seed. The method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * {@code setSeed} is implemented by class {@code Random} by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * atomically updating the seed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *  <pre>{@code (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * and clearing the {@code haveNextNextGaussian} flag used by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * #nextGaussian}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>The implementation of {@code setSeed} by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * happens to use only 48 bits of the given seed. In general, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * an overriding method may use all 64 bits of the {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * argument as a seed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param seed the initial seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    synchronized public void setSeed(long seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        seed = (seed ^ multiplier) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.seed.set(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Generates the next pseudorandom number. Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * override this, as this is used by all other methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p>The general contract of {@code next} is that it returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * {@code int} value and if the argument {@code bits} is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * {@code 1} and {@code 32} (inclusive), then that many low-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * bits of the returned value will be (approximately) independently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * chosen bit values, each of which is (approximately) equally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * likely to be {@code 0} or {@code 1}. The method {@code next} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * implemented by class {@code Random} by atomically updating the seed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *  <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * and returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *  <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * This is a linear congruential pseudorandom number generator, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * defined by D. H. Lehmer and described by Donald E. Knuth in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <i>The Art of Computer Programming,</i> Volume 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <i>Seminumerical Algorithms</i>, section 3.2.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param  bits random bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return the next pseudorandom value from this random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *         generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    protected int next(int bits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        long oldseed, nextseed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        AtomicLong seed = this.seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            oldseed = seed.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            nextseed = (oldseed * multiplier + addend) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } while (!seed.compareAndSet(oldseed, nextseed));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return (int)(nextseed >>> (48 - bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Generates random bytes and places them into a user-supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * byte array.  The number of random bytes produced is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * the length of the byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <p>The method {@code nextBytes} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * public void nextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *   for (int i = 0; i < bytes.length; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *     for (int rnd = nextInt(), n = Math.min(bytes.length - i, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *          n-- > 0; rnd >>= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *       bytes[i++] = (byte)rnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param  bytes the byte array to fill with random bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @throws NullPointerException if the byte array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void nextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (int i = 0, len = bytes.length; i < len; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            for (int rnd = nextInt(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                     n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                 n-- > 0; rnd >>= Byte.SIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                bytes[i++] = (byte)rnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * value from this random number generator's sequence. The general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * contract of {@code nextInt} is that one {@code int} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * pseudorandomly generated and returned. All 2<font size="-1"><sup>32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * </sup></font> possible {@code int} values are produced with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <p>The method {@code nextInt} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * public int nextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *   return next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *         value from this random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public int nextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Returns a pseudorandom, uniformly distributed {@code int} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * between 0 (inclusive) and the specified value (exclusive), drawn from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * this random number generator's sequence.  The general contract of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * {@code nextInt} is that one {@code int} value in the specified range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * is pseudorandomly generated and returned.  All {@code n} possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * {@code int} values are produced with (approximately) equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * probability.  The method {@code nextInt(int n)} is implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * class {@code Random} as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * public int nextInt(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *   if (n <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *     throw new IllegalArgumentException("n must be positive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *   if ((n & -n) == n)  // i.e., n is a power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *     return (int)((n * (long)next(31)) >> 31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *   int bits, val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *   do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *       bits = next(31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *       val = bits % n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *   } while (bits - val + (n-1) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *   return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * because the next method is only approximately an unbiased source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * independently chosen bits.  If it were a perfect source of randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * chosen bits, then the algorithm shown would choose {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * values from the stated range with perfect uniformity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * The algorithm is slightly tricky.  It rejects values that would result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * in an uneven distribution (due to the fact that 2^31 is not divisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * by n). The probability of a value being rejected depends on n.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * worst case is n=2^30+1, for which the probability of a reject is 1/2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * and the expected number of iterations before the loop terminates is 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * The algorithm treats the case where n is a power of two specially: it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * returns the correct number of high-order bits from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * pseudo-random number generator.  In the absence of special treatment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * the correct number of <i>low-order</i> bits would be returned.  Linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * congruential pseudo-random number generators such as the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * implemented by this class are known to have short periods in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * sequence of values of their low-order bits.  Thus, this special case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * greatly increases the length of the sequence of values returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * successive calls to this method if n is a small power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param n the bound on the random number to be returned.  Must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *        positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *         value between {@code 0} (inclusive) and {@code n} (exclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *         from this random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @exception IllegalArgumentException if n is not positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public int nextInt(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (n <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw new IllegalArgumentException("n must be positive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if ((n & -n) == n)  // i.e., n is a power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return (int)((n * (long)next(31)) >> 31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        int bits, val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            bits = next(31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            val = bits % n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } while (bits - val + (n-1) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Returns the next pseudorandom, uniformly distributed {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * value from this random number generator's sequence. The general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * contract of {@code nextLong} is that one {@code long} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <p>The method {@code nextLong} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * public long nextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *   return ((long)next(32) << 32) + next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Because class {@code Random} uses a seed with only 48 bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * this algorithm will not return all possible {@code long} values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return the next pseudorandom, uniformly distributed {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *         value from this random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public long nextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // it's okay that the bottom word remains signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return ((long)(next(32)) << 32) + next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Returns the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * {@code boolean} value from this random number generator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * sequence. The general contract of {@code nextBoolean} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * {@code boolean} value is pseudorandomly generated and returned.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * values {@code true} and {@code false} are produced with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p>The method {@code nextBoolean} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * public boolean nextBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *   return next(1) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @return the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *         {@code boolean} value from this random number generator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *         sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public boolean nextBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return next(1) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Returns the next pseudorandom, uniformly distributed {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * value between {@code 0.0} and {@code 1.0} from this random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <p>The general contract of {@code nextFloat} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * {@code float} value, chosen (approximately) uniformly from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * pseudorandomly generated and returned. All 2<font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * size="-1"><sup>24</sup></font> possible {@code float} values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * of the form <i>m&nbsp;x&nbsp</i>2<font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * size="-1"><sup>-24</sup></font>, where <i>m</i> is a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * integer less than 2<font size="-1"><sup>24</sup> </font>, are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * produced with (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <p>The method {@code nextFloat} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * public float nextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *   return next(24) / ((float)(1 << 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * because the next method is only approximately an unbiased source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * independently chosen bits. If it were a perfect source of randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * chosen bits, then the algorithm shown would choose {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * values from the stated range with perfect uniformity.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * [In early versions of Java, the result was incorrectly calculated as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *   return next(30) / ((float)(1 << 30));}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * This might seem to be equivalent, if not better, but in fact it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * introduced a slight nonuniformity because of the bias in the rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * of floating-point numbers: it was slightly more likely that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * low-order bit of the significand would be 0 than that it would be 1.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @return the next pseudorandom, uniformly distributed {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *         value between {@code 0.0} and {@code 1.0} from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *         random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public float nextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return next(24) / ((float)(1 << 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Returns the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * {@code double} value between {@code 0.0} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * {@code 1.0} from this random number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <p>The general contract of {@code nextDouble} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * {@code double} value, chosen (approximately) uniformly from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * range {@code 0.0d} (inclusive) to {@code 1.0d} (exclusive), is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <p>The method {@code nextDouble} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * public double nextDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *   return (((long)next(26) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *     / (double)(1L << 53);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * because the {@code next} method is only approximately an unbiased
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * source of independently chosen bits. If it were a perfect source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * randomly chosen bits, then the algorithm shown would choose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * {@code double} values from the stated range with perfect uniformity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <p>[In early versions of Java, the result was incorrectly calculated as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *   return (((long)next(27) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *     / (double)(1L << 54);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * This might seem to be equivalent, if not better, but in fact it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * introduced a large nonuniformity because of the bias in the rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * of floating-point numbers: it was three times as likely that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * low-order bit of the significand would be 0 than that it would be 1!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * This nonuniformity probably doesn't matter much in practice, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * strive for perfection.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @return the next pseudorandom, uniformly distributed {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *         value between {@code 0.0} and {@code 1.0} from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *         random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @see Math#random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public double nextDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return (((long)(next(26)) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            / (double)(1L << 53);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private double nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    private boolean haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Returns the next pseudorandom, Gaussian ("normally") distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * {@code double} value with mean {@code 0.0} and standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * deviation {@code 1.0} from this random number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * The general contract of {@code nextGaussian} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * {@code double} value, chosen from (approximately) the usual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * normal distribution with mean {@code 0.0} and standard deviation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * {@code 1.0}, is pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <p>The method {@code nextGaussian} is implemented by class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * {@code Random} as if by a threadsafe version of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * private double nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * private boolean haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * public double nextGaussian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *   if (haveNextNextGaussian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *     haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *     return nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *     double v1, v2, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *     do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *       v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *       v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *       s = v1 * v1 + v2 * v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *     } while (s >= 1 || s == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *     double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *     nextNextGaussian = v2 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *     haveNextNextGaussian = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *     return v1 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * This uses the <i>polar method</i> of G. E. P. Box, M. E. Muller, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * G. Marsaglia, as described by Donald E. Knuth in <i>The Art of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Computer Programming</i>, Volume 3: <i>Seminumerical Algorithms</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * section 3.4.1, subsection C, algorithm P. Note that it generates two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * independent values at the cost of only one call to {@code StrictMath.log}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * and one call to {@code StrictMath.sqrt}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @return the next pseudorandom, Gaussian ("normally") distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *         {@code double} value with mean {@code 0.0} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *         standard deviation {@code 1.0} from this random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *         generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    synchronized public double nextGaussian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // See Knuth, ACP, Section 3.4.1 Algorithm C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (haveNextNextGaussian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            double v1, v2, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                v1 = 2 * nextDouble() - 1; // between -1 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                v2 = 2 * nextDouble() - 1; // between -1 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                s = v1 * v1 + v2 * v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            } while (s >= 1 || s == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            nextNextGaussian = v2 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            haveNextNextGaussian = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            return v1 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Serializable fields for Random.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @serialField    seed long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *              seed for random computations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @serialField    nextNextGaussian double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *              next Gaussian to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @serialField      haveNextNextGaussian boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *              nextNextGaussian is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        new ObjectStreamField("seed", Long.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        new ObjectStreamField("nextNextGaussian", Double.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Reconstitute the {@code Random} instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // The seed is read in as {@code long} for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        // historical reasons, but it is converted to an AtomicLong.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   507
        long seedVal = fields.get("seed", -1L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (seedVal < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
          throw new java.io.StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                              "Random: invalid seed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        resetSeed(seedVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        nextNextGaussian = fields.get("nextNextGaussian", 0.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Save the {@code Random} instance to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    synchronized private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // set the values of the Serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // The seed is serialized as a long for historical reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        fields.put("seed", seed.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        fields.put("nextNextGaussian", nextNextGaussian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        fields.put("haveNextNextGaussian", haveNextNextGaussian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        // save them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    // Support for resetting seed while deserializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private static final long seedOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            seedOffset = unsafe.objectFieldOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                (Random.class.getDeclaredField("seed"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        } catch (Exception ex) { throw new Error(ex); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private void resetSeed(long seedVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
}