src/java.base/share/classes/java/util/Random.java
author jlaskey
Thu, 14 Nov 2019 12:39:49 -0400
branchJDK-8193209-branch
changeset 59086 214afc7a1e02
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
[mq]: refresh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52220
diff changeset
     2
 * Copyright (c) 1995, 2019, 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;
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
    27
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
    29
import java.math.BigInteger;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
    30
import java.util.Spliterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.concurrent.atomic.AtomicLong;
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    32
import java.util.function.DoubleConsumer;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    33
import java.util.function.IntConsumer;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    34
import java.util.function.LongConsumer;
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
    35
import java.util.random.RandomGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
    36
import java.util.random.RandomSupport;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
    37
import java.util.random.RandomSupport.AbstractSpliteratorGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
    38
import java.util.random.RandomSupport.RandomSpliterator;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    39
import java.util.stream.DoubleStream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    40
import java.util.stream.IntStream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    41
import java.util.stream.LongStream;
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    42
import java.util.stream.StreamSupport;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    43
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32649
diff changeset
    44
import jdk.internal.misc.Unsafe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * An instance of this class is used to generate a stream of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * pseudorandom numbers. The class uses a 48-bit seed, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * modified using a linear congruential formula. (See Donald Knuth,
1818
7847313afae6 6792545: Typo in java.util.Collection JavaDoc
darcy
parents: 715
diff changeset
    50
 * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * If two instances of {@code Random} are created with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * seed, and the same sequence of method calls is made for each, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * will generate and return identical sequences of numbers. In order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * guarantee this property, particular algorithms are specified for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * class {@code Random}. Java implementations must use all the algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * shown here for the class {@code Random}, for the sake of absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * portability of Java code. However, subclasses of class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * are permitted to use other algorithms, so long as they adhere to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * general contracts for all the methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The algorithms implemented by class {@code Random} use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * {@code protected} utility method that on each invocation can supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * up to 32 pseudorandomly generated bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Many applications will find the method {@link Math#random} simpler to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    68
 * <p>Instances of {@code java.util.Random} are threadsafe.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    69
 * However, the concurrent use of the same {@code java.util.Random}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    70
 * instance across threads may encounter contention and consequent
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    71
 * poor performance. Consider instead using
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    72
 * {@link java.util.concurrent.ThreadLocalRandom} in multithreaded
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    73
 * designs.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    74
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    75
 * <p>Instances of {@code java.util.Random} are not cryptographically
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    76
 * secure.  Consider instead using {@link java.security.SecureRandom} to
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    77
 * get a cryptographically secure pseudo-random number generator for use
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    78
 * by security-sensitive applications.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    79
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @author  Frank Yellin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @since   1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
    84
class Random extends AbstractSpliteratorGenerator implements java.io.Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** use serialVersionUID from JDK 1.1 for interoperability */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52220
diff changeset
    86
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static final long serialVersionUID = 3905348978240129619L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The internal state associated with this pseudorandom number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * (The specs for the methods in this class describe the ongoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * computation of this value.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private final AtomicLong seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    96
    private static final long multiplier = 0x5DEECE66DL;
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    97
    private static final long addend = 0xBL;
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    98
    private static final long mask = (1L << 48) - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 19609
diff changeset
   100
    private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53)
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   101
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   102
    // IllegalArgumentException messages
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   103
    static final String BadBound = "bound must be positive";
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   104
    static final String BadRange = "bound must be greater than origin";
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   105
    static final String BadSize  = "size must be non-negative";
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   106
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Creates a new random number generator. This constructor sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * the seed of the random number generator to a value very likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * to be distinct from any other invocation of this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   112
    public Random() {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   113
        this(seedUniquifier() ^ System.nanoTime());
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   114
    }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   115
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   116
    private static long seedUniquifier() {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   117
        // L'Ecuyer, "Tables of Linear Congruential Generators of
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   118
        // Different Sizes and Good Lattice Structure", 1999
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   119
        for (;;) {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   120
            long current = seedUniquifier.get();
49954
deefa2120bc4 8201634: Random seedUniquifier uses incorrect LCG
martin
parents: 47216
diff changeset
   121
            long next = current * 1181783497276652981L;
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   122
            if (seedUniquifier.compareAndSet(current, next))
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   123
                return next;
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   124
        }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   125
    }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   126
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   127
    private static final AtomicLong seedUniquifier
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   128
        = new AtomicLong(8682522807148012L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Creates a new random number generator using a single {@code long} seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * The seed is the initial value of the internal state of the pseudorandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * number generator which is maintained by method {@link #next}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>The invocation {@code new Random(seed)} is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * rnd.setSeed(seed);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param seed the initial seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see   #setSeed(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public Random(long seed) {
10056
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   144
        if (getClass() == Random.class)
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   145
            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
   146
        else {
51759
ac6e9a2ebc04 8210786: Typo s/overriden/overridden/ in several places
igerasim
parents: 49954
diff changeset
   147
            // subclass might have overridden setSeed
10056
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   148
            this.seed = new AtomicLong();
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   149
            setSeed(seed);
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   150
        }
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   151
    }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   152
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   153
    private static long initialScramble(long seed) {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   154
        return (seed ^ multiplier) & mask;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Sets the seed of this random number generator using a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * {@code long} seed. The general contract of {@code setSeed} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * that it alters the state of this random number generator object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * so as to be in exactly the same state as if it had just been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * created with the argument {@code seed} as a seed. The method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * {@code setSeed} is implemented by class {@code Random} by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * atomically updating the seed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *  <pre>{@code (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * and clearing the {@code haveNextNextGaussian} flag used by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * #nextGaussian}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>The implementation of {@code setSeed} by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * happens to use only 48 bits of the given seed. In general, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * an overriding method may use all 64 bits of the {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * argument as a seed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param seed the initial seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   176
    public synchronized void setSeed(long seed) {
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   177
        this.seed.set(initialScramble(seed));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Generates the next pseudorandom number. Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * override this, as this is used by all other methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <p>The general contract of {@code next} is that it returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * {@code int} value and if the argument {@code bits} is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * {@code 1} and {@code 32} (inclusive), then that many low-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * bits of the returned value will be (approximately) independently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * chosen bit values, each of which is (approximately) equally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * likely to be {@code 0} or {@code 1}. The method {@code next} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * implemented by class {@code Random} by atomically updating the seed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *  <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * and returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *  <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * This is a linear congruential pseudorandom number generator, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * defined by D. H. Lehmer and described by Donald E. Knuth in
43003
b1c1a42602d6 8164923: Error in the documentation for java.lang.Random
rriggs
parents: 33674
diff changeset
   198
     * <i>The Art of Computer Programming,</i> Volume 2:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <i>Seminumerical Algorithms</i>, section 3.2.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param  bits random bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return the next pseudorandom value from this random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *         generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected int next(int bits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        long oldseed, nextseed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        AtomicLong seed = this.seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            oldseed = seed.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            nextseed = (oldseed * multiplier + addend) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } while (!seed.compareAndSet(oldseed, nextseed));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return (int)(nextseed >>> (48 - bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   216
    /*
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   217
     * Period of Random is 2**48
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   218
     */
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   219
    private static final BigInteger PERIOD = BigInteger.valueOf(1L<<48);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   220
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   221
    /**
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   222
     * Returns the period of this random number generator.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   223
     *
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   224
     * @return the period of this random number generator.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   225
     */
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   226
    public BigInteger period() {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   227
        return PERIOD;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   228
    }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   229
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Generates random bytes and places them into a user-supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * byte array.  The number of random bytes produced is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * the length of the byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p>The method {@code nextBytes} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * public void nextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *   for (int i = 0; i < bytes.length; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *     for (int rnd = nextInt(), n = Math.min(bytes.length - i, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *          n-- > 0; rnd >>= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *       bytes[i++] = (byte)rnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param  bytes the byte array to fill with random bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @throws NullPointerException if the byte array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void nextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        for (int i = 0, len = bytes.length; i < len; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            for (int rnd = nextInt(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                     n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                 n-- > 0; rnd >>= Byte.SIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                bytes[i++] = (byte)rnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Returns the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * value from this random number generator's sequence. The general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * contract of {@code nextInt} is that one {@code int} value is
19074
84a8d23e8f32 8020539: Clean up doclint problems in java.util package, part 2
bpb
parents: 18156
diff changeset
   261
     * pseudorandomly generated and returned. All 2<sup>32</sup> possible
84a8d23e8f32 8020539: Clean up doclint problems in java.util package, part 2
bpb
parents: 18156
diff changeset
   262
     * {@code int} values are produced with (approximately) equal probability.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <p>The method {@code nextInt} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * public int nextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *   return next(32);
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
     * @return the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *         value from this random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public int nextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   279
     * Returns a pseudorandom {@code int} value between zero (inclusive)
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   280
     * and the specified bound (exclusive).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   282
     * @param bound the upper bound (exclusive).  Must be positive.
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   283
     * @return a pseudorandom {@code int} value between zero
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   284
     *         (inclusive) and the bound (exclusive)
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   285
     * @throws IllegalArgumentException if {@code bound} is not positive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   287
    public int nextInt(int bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   288
        if (bound <= 0)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   289
            throw new IllegalArgumentException(BadBound);
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   290
        // Specialize internalNextInt for origin 0
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   291
        int r = nextInt();
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   292
        int m = bound - 1;
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   293
        if ((bound & m) == 0) // power of two
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   294
            r &= m;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   295
        else { // reject over-represented candidates
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   296
            for (int u = r >>> 1;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   297
                 u + m - (r = u % bound) < 0;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   298
                 u = nextInt() >>> 1)
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   299
                ;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   300
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   301
        return r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Returns the next pseudorandom, uniformly distributed {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * value from this random number generator's sequence. The general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * contract of {@code nextLong} is that one {@code long} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p>The method {@code nextLong} 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 long nextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *   return ((long)next(32) << 32) + next(32);
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
     * Because class {@code Random} uses a seed with only 48 bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * this algorithm will not return all possible {@code long} values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return the next pseudorandom, uniformly distributed {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *         value from this random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public long nextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // it's okay that the bottom word remains signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return ((long)(next(32)) << 32) + next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Returns the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * {@code boolean} value from this random number generator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * sequence. The general contract of {@code nextBoolean} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * {@code boolean} value is pseudorandomly generated and returned.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * values {@code true} and {@code false} are produced with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <p>The method {@code nextBoolean} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * public boolean nextBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *   return next(1) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *         {@code boolean} value from this random number generator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *         sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public boolean nextBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return next(1) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Returns the next pseudorandom, uniformly distributed {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * value between {@code 0.0} and {@code 1.0} from this random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <p>The general contract of {@code nextFloat} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * {@code float} value, chosen (approximately) uniformly from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is
19074
84a8d23e8f32 8020539: Clean up doclint problems in java.util package, part 2
bpb
parents: 18156
diff changeset
   360
     * pseudorandomly generated and returned. All 2<sup>24</sup> possible
84a8d23e8f32 8020539: Clean up doclint problems in java.util package, part 2
bpb
parents: 18156
diff changeset
   361
     * {@code float} values of the form <i>m&nbsp;x&nbsp;</i>2<sup>-24</sup>,
84a8d23e8f32 8020539: Clean up doclint problems in java.util package, part 2
bpb
parents: 18156
diff changeset
   362
     * where <i>m</i> is a positive integer less than 2<sup>24</sup>, are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * produced with (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <p>The method {@code nextFloat} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * public float nextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *   return next(24) / ((float)(1 << 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * because the next method is only approximately an unbiased source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * independently chosen bits. If it were a perfect source of randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * chosen bits, then the algorithm shown would choose {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * values from the stated range with perfect uniformity.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * [In early versions of Java, the result was incorrectly calculated as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *   return next(30) / ((float)(1 << 30));}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * This might seem to be equivalent, if not better, but in fact it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * introduced a slight nonuniformity because of the bias in the rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * of floating-point numbers: it was slightly more likely that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * low-order bit of the significand would be 0 than that it would be 1.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return the next pseudorandom, uniformly distributed {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *         value between {@code 0.0} and {@code 1.0} from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *         random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public float nextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return next(24) / ((float)(1 << 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Returns the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * {@code double} value between {@code 0.0} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * {@code 1.0} from this random number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <p>The general contract of {@code nextDouble} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * {@code double} value, chosen (approximately) uniformly from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * range {@code 0.0d} (inclusive) to {@code 1.0d} (exclusive), is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <p>The method {@code nextDouble} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * public double nextDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *   return (((long)next(26) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *     / (double)(1L << 53);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * because the {@code next} method is only approximately an unbiased
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * source of independently chosen bits. If it were a perfect source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * randomly chosen bits, then the algorithm shown would choose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * {@code double} values from the stated range with perfect uniformity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p>[In early versions of Java, the result was incorrectly calculated as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *   return (((long)next(27) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *     / (double)(1L << 54);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * This might seem to be equivalent, if not better, but in fact it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * introduced a large nonuniformity because of the bias in the rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * of floating-point numbers: it was three times as likely that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * low-order bit of the significand would be 0 than that it would be 1!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * This nonuniformity probably doesn't matter much in practice, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * strive for perfection.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @return the next pseudorandom, uniformly distributed {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *         value between {@code 0.0} and {@code 1.0} from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *         random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @see Math#random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public double nextDouble() {
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   433
        return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    private double nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    private boolean haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Returns the next pseudorandom, Gaussian ("normally") distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * {@code double} value with mean {@code 0.0} and standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * deviation {@code 1.0} from this random number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * The general contract of {@code nextGaussian} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * {@code double} value, chosen from (approximately) the usual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * normal distribution with mean {@code 0.0} and standard deviation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * {@code 1.0}, is pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * <p>The method {@code nextGaussian} is implemented by class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * {@code Random} as if by a threadsafe version of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *  <pre> {@code
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
     * public double nextGaussian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *   if (haveNextNextGaussian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *     haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *     return nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *     double v1, v2, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *     do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *       v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *       v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *       s = v1 * v1 + v2 * v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *     } while (s >= 1 || s == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *     double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *     nextNextGaussian = v2 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *     haveNextNextGaussian = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *     return v1 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * This uses the <i>polar method</i> of G. E. P. Box, M. E. Muller, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * G. Marsaglia, as described by Donald E. Knuth in <i>The Art of
43003
b1c1a42602d6 8164923: Error in the documentation for java.lang.Random
rriggs
parents: 33674
diff changeset
   474
     * Computer Programming</i>, Volume 2: <i>Seminumerical Algorithms</i>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * section 3.4.1, subsection C, algorithm P. Note that it generates two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * independent values at the cost of only one call to {@code StrictMath.log}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * and one call to {@code StrictMath.sqrt}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @return the next pseudorandom, Gaussian ("normally") distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *         {@code double} value with mean {@code 0.0} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *         standard deviation {@code 1.0} from this random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *         generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   484
    public synchronized double nextGaussian() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // See Knuth, ACP, Section 3.4.1 Algorithm C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (haveNextNextGaussian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            double v1, v2, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                v1 = 2 * nextDouble() - 1; // between -1 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                v2 = 2 * nextDouble() - 1; // between -1 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                s = v1 * v1 + v2 * v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            } while (s >= 1 || s == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            nextNextGaussian = v2 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            haveNextNextGaussian = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return v1 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   503
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Serializable fields for Random.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @serialField    seed long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *              seed for random computations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @serialField    nextNextGaussian double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *              next Gaussian to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @serialField      haveNextNextGaussian boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *              nextNextGaussian is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52220
diff changeset
   513
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        new ObjectStreamField("seed", Long.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        new ObjectStreamField("nextNextGaussian", Double.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Reconstitute the {@code Random} instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52220
diff changeset
   524
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        // The seed is read in as {@code long} for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // historical reasons, but it is converted to an AtomicLong.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   532
        long seedVal = fields.get("seed", -1L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (seedVal < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
          throw new java.io.StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                              "Random: invalid seed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        resetSeed(seedVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        nextNextGaussian = fields.get("nextNextGaussian", 0.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Save the {@code Random} instance to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 52220
diff changeset
   544
    @java.io.Serial
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   545
    private synchronized void writeObject(ObjectOutputStream s)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        // set the values of the Serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        // The seed is serialized as a long for historical reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        fields.put("seed", seed.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        fields.put("nextNextGaussian", nextNextGaussian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        fields.put("haveNextNextGaussian", haveNextNextGaussian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // save them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    // Support for resetting seed while deserializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    private static final long seedOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            seedOffset = unsafe.objectFieldOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                (Random.class.getDeclaredField("seed"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        } catch (Exception ex) { throw new Error(ex); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    private void resetSeed(long seedVal) {
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 51759
diff changeset
   570
        unsafe.putReferenceVolatile(this, seedOffset, new AtomicLong(seedVal));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
59086
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   572
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   573
    // Methods required by class AbstractSpliteratorGenerator
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   574
    public Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   575
        return new RandomIntsSpliterator(this, index, fence, origin, bound);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   576
    }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   577
    public Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   578
        return new RandomLongsSpliterator(this, index, fence, origin, bound);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   579
    }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   580
    public Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   581
        return new RandomDoublesSpliterator(this, index, fence, origin, bound);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   582
    }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   583
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   584
    /**
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   585
     * Spliterators for producing streams. These are based on abstract spliterator classes provided
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   586
     * by class AbstractSpliteratorGenerator. Each one needs to define only a constructor and two
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   587
     * methods.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   588
     */
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   589
    static class RandomIntsSpliterator extends RandomSupport.RandomSpliterator
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   590
                                       implements Spliterator.OfInt {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   591
        final AbstractSpliteratorGenerator generatingGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   592
        final int origin;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   593
        final int bound;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   594
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   595
        RandomIntsSpliterator(AbstractSpliteratorGenerator generatingGenerator,
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   596
                              long index, long fence, int origin, int bound) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   597
            super(index, fence);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   598
            this.generatingGenerator = generatingGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   599
            this.origin = origin; this.bound = bound;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   600
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   601
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   602
        public Spliterator.OfInt trySplit() {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   603
            long i = index, m = (i + fence) >>> 1;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   604
            if (m <= i) return null;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   605
            index = m;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   606
            // The same generatingGenerator is used, with no splitting or copying.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   607
            return new RandomIntsSpliterator(generatingGenerator, i, m, origin, bound);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   608
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   609
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   610
        public boolean tryAdvance(IntConsumer consumer) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   611
            if (consumer == null) throw new NullPointerException();
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   612
            long i = index, f = fence;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   613
            if (i < f) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   614
                consumer.accept(RandomSupport.boundedNextInt(generatingGenerator, origin, bound));
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   615
                index = i + 1;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   616
                return true;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   617
            }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   618
            else return false;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   619
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   620
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   621
        public void forEachRemaining(IntConsumer consumer) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   622
            if (consumer == null) throw new NullPointerException();
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   623
            long i = index, f = fence;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   624
            if (i < f) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   625
                index = f;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   626
                RandomGenerator r = generatingGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   627
                int o = origin, b = bound;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   628
                do {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   629
                    consumer.accept(RandomSupport.boundedNextInt(r, o, b));
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   630
                } while (++i < f);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   631
            }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   632
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   633
    }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   634
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   635
    /**
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   636
     * Spliterator for long streams.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   637
     */
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   638
    static class RandomLongsSpliterator extends RandomSupport.RandomSpliterator
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   639
                                        implements Spliterator.OfLong {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   640
        final AbstractSpliteratorGenerator generatingGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   641
        final long origin;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   642
        final long bound;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   643
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   644
        RandomLongsSpliterator(AbstractSpliteratorGenerator generatingGenerator,
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   645
                               long index, long fence, long origin, long bound) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   646
            super(index, fence);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   647
            this.generatingGenerator = generatingGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   648
            this.origin = origin; this.bound = bound;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   649
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   650
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   651
        public Spliterator.OfLong trySplit() {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   652
            long i = index, m = (i + fence) >>> 1;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   653
            if (m <= i) return null;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   654
            index = m;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   655
            // The same generatingGenerator is used, with no splitting or copying.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   656
            return new RandomLongsSpliterator(generatingGenerator, i, m, origin, bound);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   657
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   658
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   659
        public boolean tryAdvance(LongConsumer consumer) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   660
            if (consumer == null) throw new NullPointerException();
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   661
            long i = index, f = fence;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   662
            if (i < f) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   663
                consumer.accept(RandomSupport.boundedNextLong(generatingGenerator, origin, bound));
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   664
                index = i + 1;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   665
                return true;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   666
            }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   667
            else return false;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   668
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   669
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   670
        public void forEachRemaining(LongConsumer consumer) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   671
            if (consumer == null) throw new NullPointerException();
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   672
            long i = index, f = fence;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   673
            if (i < f) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   674
                index = f;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   675
                RandomGenerator r = generatingGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   676
                long o = origin, b = bound;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   677
                do {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   678
                    consumer.accept(RandomSupport.boundedNextLong(r, o, b));
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   679
                } while (++i < f);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   680
            }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   681
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   682
    }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   683
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   684
    /**
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   685
     * Spliterator for double streams.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   686
     */
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   687
    static class RandomDoublesSpliterator extends RandomSupport.RandomSpliterator
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   688
                                          implements Spliterator.OfDouble {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   689
        final AbstractSpliteratorGenerator generatingGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   690
        final double origin;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   691
        final double bound;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   692
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   693
        RandomDoublesSpliterator(AbstractSpliteratorGenerator generatingGenerator,
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   694
                                 long index, long fence, double origin, double bound) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   695
            super(index, fence);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   696
            this.generatingGenerator = generatingGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   697
            this.origin = origin; this.bound = bound;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   698
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   699
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   700
        public Spliterator.OfDouble trySplit() {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   701
            long i = index, m = (i + fence) >>> 1;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   702
            if (m <= i) return null;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   703
            index = m;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   704
            // The same generatingGenerator is used, with no splitting or copying.
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   705
            return new RandomDoublesSpliterator(generatingGenerator, i, m, origin, bound);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   706
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   707
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   708
        public boolean tryAdvance(DoubleConsumer consumer) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   709
            if (consumer == null) throw new NullPointerException();
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   710
            long i = index, f = fence;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   711
            if (i < f) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   712
                consumer.accept(RandomSupport.boundedNextDouble(generatingGenerator, origin, bound));
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   713
                index = i + 1;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   714
                return true;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   715
            }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   716
            else return false;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   717
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   718
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   719
        public void forEachRemaining(DoubleConsumer consumer) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   720
            if (consumer == null) throw new NullPointerException();
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   721
            long i = index, f = fence;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   722
            if (i < f) {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   723
                index = f;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   724
                RandomGenerator r = generatingGenerator;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   725
                double o = origin, b = bound;
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   726
                do {
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   727
                    consumer.accept(RandomSupport.boundedNextDouble(r, o, b));
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   728
                } while (++i < f);
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   729
            }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   730
        }
214afc7a1e02 [mq]: refresh
jlaskey
parents: 57956
diff changeset
   731
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
}