jdk/src/share/classes/java/util/Random.java
author mduigou
Thu, 02 May 2013 09:18:56 -0700
changeset 17421 f3fbcfe6e2cf
parent 14342 8435a30053c1
child 18156 edb590d448c5
permissions -rw-r--r--
8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile Reviewed-by: mduigou, henryjen, alanb, martin, psandoz Contributed-by: akhil.arora@oracle.com, brian.goetz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10056
diff changeset
     2
 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5468
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5468
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5468
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5468
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5468
diff changeset
    23
 * questions.
2
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;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    29
import java.util.stream.DoubleStream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    30
import java.util.stream.IntStream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    31
import java.util.stream.LongStream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * An instance of this class is used to generate a stream of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * pseudorandom numbers. The class uses a 48-bit seed, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * modified using a linear congruential formula. (See Donald Knuth,
1818
7847313afae6 6792545: Typo in java.util.Collection JavaDoc
darcy
parents: 715
diff changeset
    39
 * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * If two instances of {@code Random} are created with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * seed, and the same sequence of method calls is made for each, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * will generate and return identical sequences of numbers. In order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * guarantee this property, particular algorithms are specified for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * class {@code Random}. Java implementations must use all the algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * shown here for the class {@code Random}, for the sake of absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * portability of Java code. However, subclasses of class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * are permitted to use other algorithms, so long as they adhere to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * general contracts for all the methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * The algorithms implemented by class {@code Random} use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@code protected} utility method that on each invocation can supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * up to 32 pseudorandomly generated bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Many applications will find the method {@link Math#random} simpler to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    57
 * <p>Instances of {@code java.util.Random} are threadsafe.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    58
 * However, the concurrent use of the same {@code java.util.Random}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    59
 * instance across threads may encounter contention and consequent
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    60
 * poor performance. Consider instead using
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    61
 * {@link java.util.concurrent.ThreadLocalRandom} in multithreaded
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    62
 * designs.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    63
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    64
 * <p>Instances of {@code java.util.Random} are not cryptographically
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    65
 * secure.  Consider instead using {@link java.security.SecureRandom} to
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    66
 * get a cryptographically secure pseudo-random number generator for use
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    67
 * by security-sensitive applications.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    68
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @author  Frank Yellin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @since   1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
class Random implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /** use serialVersionUID from JDK 1.1 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static final long serialVersionUID = 3905348978240129619L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The internal state associated with this pseudorandom number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * (The specs for the methods in this class describe the ongoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * computation of this value.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private final AtomicLong seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    84
    private static final long multiplier = 0x5DEECE66DL;
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    85
    private static final long addend = 0xBL;
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    86
    private static final long mask = (1L << 48) - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Creates a new random number generator. This constructor sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * the seed of the random number generator to a value very likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * to be distinct from any other invocation of this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
    93
    public Random() {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
    94
        this(seedUniquifier() ^ System.nanoTime());
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
    95
    }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
    96
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
    97
    private static long seedUniquifier() {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
    98
        // L'Ecuyer, "Tables of Linear Congruential Generators of
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
    99
        // Different Sizes and Good Lattice Structure", 1999
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   100
        for (;;) {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   101
            long current = seedUniquifier.get();
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   102
            long next = current * 181783497276652981L;
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   103
            if (seedUniquifier.compareAndSet(current, next))
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   104
                return next;
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   105
        }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   106
    }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   107
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   108
    private static final AtomicLong seedUniquifier
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   109
        = new AtomicLong(8682522807148012L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Creates a new random number generator using a single {@code long} seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * The seed is the initial value of the internal state of the pseudorandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * number generator which is maintained by method {@link #next}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>The invocation {@code new Random(seed)} is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * rnd.setSeed(seed);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param seed the initial seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see   #setSeed(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public Random(long seed) {
10056
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   125
        if (getClass() == Random.class)
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   126
            this.seed = new AtomicLong(initialScramble(seed));
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   127
        else {
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   128
            // subclass might have overriden setSeed
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   129
            this.seed = new AtomicLong();
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   130
            setSeed(seed);
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   131
        }
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   132
    }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   133
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   134
    private static long initialScramble(long seed) {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   135
        return (seed ^ multiplier) & mask;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Sets the seed of this random number generator using a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * {@code long} seed. The general contract of {@code setSeed} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * that it alters the state of this random number generator object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * so as to be in exactly the same state as if it had just been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * created with the argument {@code seed} as a seed. The method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * {@code setSeed} is implemented by class {@code Random} by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * atomically updating the seed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *  <pre>{@code (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * and clearing the {@code haveNextNextGaussian} flag used by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * #nextGaussian}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p>The implementation of {@code setSeed} by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * happens to use only 48 bits of the given seed. In general, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * an overriding method may use all 64 bits of the {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * argument as a seed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param seed the initial seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    synchronized public void setSeed(long seed) {
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   158
        this.seed.set(initialScramble(seed));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Generates the next pseudorandom number. Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * override this, as this is used by all other methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>The general contract of {@code next} is that it returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * {@code int} value and if the argument {@code bits} is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * {@code 1} and {@code 32} (inclusive), then that many low-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * bits of the returned value will be (approximately) independently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * chosen bit values, each of which is (approximately) equally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * likely to be {@code 0} or {@code 1}. The method {@code next} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * implemented by class {@code Random} by atomically updating the seed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *  <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * and returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *  <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * This is a linear congruential pseudorandom number generator, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * defined by D. H. Lehmer and described by Donald E. Knuth in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <i>The Art of Computer Programming,</i> Volume 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <i>Seminumerical Algorithms</i>, section 3.2.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param  bits random bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @return the next pseudorandom value from this random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected int next(int bits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        long oldseed, nextseed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        AtomicLong seed = this.seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            oldseed = seed.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            nextseed = (oldseed * multiplier + addend) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } while (!seed.compareAndSet(oldseed, nextseed));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return (int)(nextseed >>> (48 - bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Generates random bytes and places them into a user-supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * byte array.  The number of random bytes produced is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * the length of the byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p>The method {@code nextBytes} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * public void nextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *   for (int i = 0; i < bytes.length; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *     for (int rnd = nextInt(), n = Math.min(bytes.length - i, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *          n-- > 0; rnd >>= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *       bytes[i++] = (byte)rnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param  bytes the byte array to fill with random bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @throws NullPointerException if the byte array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public void nextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        for (int i = 0, len = bytes.length; i < len; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            for (int rnd = nextInt(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                     n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                 n-- > 0; rnd >>= Byte.SIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                bytes[i++] = (byte)rnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Returns the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * value from this random number generator's sequence. The general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * contract of {@code nextInt} is that one {@code int} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * pseudorandomly generated and returned. All 2<font size="-1"><sup>32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * </sup></font> possible {@code int} values are produced with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p>The method {@code nextInt} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * public int nextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *   return next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *         value from this random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public int nextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Returns a pseudorandom, uniformly distributed {@code int} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * between 0 (inclusive) and the specified value (exclusive), drawn from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * this random number generator's sequence.  The general contract of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * {@code nextInt} is that one {@code int} value in the specified range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * is pseudorandomly generated and returned.  All {@code n} possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * {@code int} values are produced with (approximately) equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * probability.  The method {@code nextInt(int n)} is implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * class {@code Random} as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * public int nextInt(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *   if (n <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *     throw new IllegalArgumentException("n must be positive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *   if ((n & -n) == n)  // i.e., n is a power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *     return (int)((n * (long)next(31)) >> 31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *   int bits, val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *   do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *       bits = next(31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *       val = bits % n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *   } while (bits - val + (n-1) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *   return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * because the next method is only approximately an unbiased source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * independently chosen bits.  If it were a perfect source of randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * chosen bits, then the algorithm shown would choose {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * values from the stated range with perfect uniformity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * The algorithm is slightly tricky.  It rejects values that would result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * in an uneven distribution (due to the fact that 2^31 is not divisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * by n). The probability of a value being rejected depends on n.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * worst case is n=2^30+1, for which the probability of a reject is 1/2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * and the expected number of iterations before the loop terminates is 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * The algorithm treats the case where n is a power of two specially: it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * returns the correct number of high-order bits from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * pseudo-random number generator.  In the absence of special treatment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * the correct number of <i>low-order</i> bits would be returned.  Linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * congruential pseudo-random number generators such as the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * implemented by this class are known to have short periods in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * sequence of values of their low-order bits.  Thus, this special case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * greatly increases the length of the sequence of values returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * successive calls to this method if n is a small power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param n the bound on the random number to be returned.  Must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *        positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *         value between {@code 0} (inclusive) and {@code n} (exclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *         from this random number generator's sequence
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   298
     * @throws IllegalArgumentException if n is not positive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public int nextInt(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (n <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw new IllegalArgumentException("n must be positive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if ((n & -n) == n)  // i.e., n is a power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return (int)((n * (long)next(31)) >> 31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int bits, val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            bits = next(31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            val = bits % n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } while (bits - val + (n-1) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Returns the next pseudorandom, uniformly distributed {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * value from this random number generator's sequence. The general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * contract of {@code nextLong} is that one {@code long} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <p>The method {@code nextLong} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * public long nextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *   return ((long)next(32) << 32) + next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Because class {@code Random} uses a seed with only 48 bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * this algorithm will not return all possible {@code long} values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @return the next pseudorandom, uniformly distributed {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *         value from this random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public long nextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // it's okay that the bottom word remains signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return ((long)(next(32)) << 32) + next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Returns the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * {@code boolean} value from this random number generator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * sequence. The general contract of {@code nextBoolean} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * {@code boolean} value is pseudorandomly generated and returned.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * values {@code true} and {@code false} are produced with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <p>The method {@code nextBoolean} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * public boolean nextBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *   return next(1) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *         {@code boolean} value from this random number generator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *         sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public boolean nextBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return next(1) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Returns the next pseudorandom, uniformly distributed {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * value between {@code 0.0} and {@code 1.0} from this random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <p>The general contract of {@code nextFloat} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * {@code float} value, chosen (approximately) uniformly from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * pseudorandomly generated and returned. All 2<font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * size="-1"><sup>24</sup></font> possible {@code float} values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * of the form <i>m&nbsp;x&nbsp</i>2<font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * size="-1"><sup>-24</sup></font>, where <i>m</i> is a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * integer less than 2<font size="-1"><sup>24</sup> </font>, are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * produced with (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <p>The method {@code nextFloat} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * public float nextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *   return next(24) / ((float)(1 << 24));
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 next method is only approximately an unbiased source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * independently chosen bits. If it were a perfect source of randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * chosen bits, then the algorithm shown would choose {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * values from the stated range with perfect uniformity.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * [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 next(30) / ((float)(1 << 30));}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * This might seem to be equivalent, if not better, but in fact it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * introduced a slight nonuniformity because of the bias in the rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * of floating-point numbers: it was slightly more likely that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * low-order bit of the significand would be 0 than that it would be 1.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return the next pseudorandom, uniformly distributed {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *         value between {@code 0.0} and {@code 1.0} from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *         random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public float nextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return next(24) / ((float)(1 << 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Returns the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * {@code double} value between {@code 0.0} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * {@code 1.0} from this random number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * <p>The general contract of {@code nextDouble} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * {@code double} value, chosen (approximately) uniformly from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * range {@code 0.0d} (inclusive) to {@code 1.0d} (exclusive), is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <p>The method {@code nextDouble} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * public double nextDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *   return (((long)next(26) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *     / (double)(1L << 53);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * because the {@code next} method is only approximately an unbiased
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * source of independently chosen bits. If it were a perfect source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * randomly chosen bits, then the algorithm shown would choose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * {@code double} values from the stated range with perfect uniformity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <p>[In early versions of Java, the result was incorrectly calculated as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *   return (((long)next(27) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *     / (double)(1L << 54);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * This might seem to be equivalent, if not better, but in fact it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * introduced a large nonuniformity because of the bias in the rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * of floating-point numbers: it was three times as likely that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * low-order bit of the significand would be 0 than that it would be 1!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * This nonuniformity probably doesn't matter much in practice, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * strive for perfection.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @return the next pseudorandom, uniformly distributed {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *         value between {@code 0.0} and {@code 1.0} from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *         random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @see Math#random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public double nextDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return (((long)(next(26)) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            / (double)(1L << 53);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    private double nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    private boolean haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Returns the next pseudorandom, Gaussian ("normally") distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * {@code double} value with mean {@code 0.0} and standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * deviation {@code 1.0} from this random number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * The general contract of {@code nextGaussian} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * {@code double} value, chosen from (approximately) the usual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * normal distribution with mean {@code 0.0} and standard deviation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * {@code 1.0}, is pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * <p>The method {@code nextGaussian} is implemented by class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * {@code Random} as if by a threadsafe version of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * private double nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * private boolean haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * public double nextGaussian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *   if (haveNextNextGaussian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *     haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *     return nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *     double v1, v2, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *     do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *       v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *       v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *       s = v1 * v1 + v2 * v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *     } while (s >= 1 || s == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *     double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *     nextNextGaussian = v2 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *     haveNextNextGaussian = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *     return v1 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * This uses the <i>polar method</i> of G. E. P. Box, M. E. Muller, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * G. Marsaglia, as described by Donald E. Knuth in <i>The Art of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Computer Programming</i>, Volume 3: <i>Seminumerical Algorithms</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * section 3.4.1, subsection C, algorithm P. Note that it generates two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * independent values at the cost of only one call to {@code StrictMath.log}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * and one call to {@code StrictMath.sqrt}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @return the next pseudorandom, Gaussian ("normally") distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *         {@code double} value with mean {@code 0.0} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *         standard deviation {@code 1.0} from this random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *         generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    synchronized public double nextGaussian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        // See Knuth, ACP, Section 3.4.1 Algorithm C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (haveNextNextGaussian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            double v1, v2, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                v1 = 2 * nextDouble() - 1; // between -1 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                v2 = 2 * nextDouble() - 1; // between -1 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                s = v1 * v1 + v2 * v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            } while (s >= 1 || s == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            nextNextGaussian = v2 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            haveNextNextGaussian = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            return v1 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   520
     * Returns a stream of pseudorandom, uniformly distributed
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   521
     * {@code integer} values from this random number generator's
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   522
     * sequence. Values are obtained as needed by calling
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   523
     * {@link #nextInt()}.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   524
     *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   525
     * @return an infinite stream of {@code integer} values
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   526
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   527
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   528
    public IntStream ints() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   529
        return IntStream.generate(this::nextInt);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   530
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   531
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   532
    /**
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   533
     * Returns a stream of pseudorandom, uniformly distributed
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   534
     * {@code long} values from this random number generator's
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   535
     * sequence. Values are obtained as needed by calling
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   536
     * {@link #nextLong()}.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   537
     *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   538
     * @return an infinite stream of {@code long} values
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   539
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   540
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   541
    public LongStream longs() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   542
        return LongStream.generate(this::nextLong);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   543
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   544
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   545
    /**
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   546
     * Returns a stream of pseudorandom, uniformly distributed
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   547
     * {@code double} values between {@code 0.0} and {@code 1.0}
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   548
     * from this random number generator's sequence. Values are
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   549
     * obtained as needed by calling {@link #nextDouble()}.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   550
     *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   551
     * @return an infinite stream of {@code double} values
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   552
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   553
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   554
    public DoubleStream doubles() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   555
        return DoubleStream.generate(this::nextDouble);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   556
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   557
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   558
    /**
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   559
     * Returns a stream of pseudorandom, Gaussian ("normally")
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   560
     * distributed {@code double} values with mean {@code 0.0}
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   561
     * and standard deviation {@code 1.0} from this random number
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   562
     * generator's sequence. Values are obtained as needed by
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   563
     * calling {@link #nextGaussian()}.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   564
     *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   565
     * @return an infinite stream of {@code double} values
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   566
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   567
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   568
    public DoubleStream gaussians() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   569
        return DoubleStream.generate(this::nextGaussian);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   570
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   571
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   572
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Serializable fields for Random.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @serialField    seed long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *              seed for random computations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @serialField    nextNextGaussian double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *              next Gaussian to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @serialField      haveNextNextGaussian boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *              nextNextGaussian is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        new ObjectStreamField("seed", Long.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        new ObjectStreamField("nextNextGaussian", Double.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Reconstitute the {@code Random} instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        // The seed is read in as {@code long} for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        // historical reasons, but it is converted to an AtomicLong.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   599
        long seedVal = fields.get("seed", -1L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (seedVal < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
          throw new java.io.StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                              "Random: invalid seed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        resetSeed(seedVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        nextNextGaussian = fields.get("nextNextGaussian", 0.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * Save the {@code Random} instance to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    synchronized private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        // set the values of the Serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // The seed is serialized as a long for historical reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        fields.put("seed", seed.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        fields.put("nextNextGaussian", nextNextGaussian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        fields.put("haveNextNextGaussian", haveNextNextGaussian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        // save them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    // Support for resetting seed while deserializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    private static final long seedOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            seedOffset = unsafe.objectFieldOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                (Random.class.getDeclaredField("seed"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        } catch (Exception ex) { throw new Error(ex); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    private void resetSeed(long seedVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
}