jdk/src/share/classes/java/util/Random.java
author psandoz
Wed, 28 Aug 2013 22:11:14 +0200
changeset 19609 108f52a7438f
parent 19074 84a8d23e8f32
child 20540 1376a380b9ba
permissions -rw-r--r--
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom Reviewed-by: mduigou Contributed-by: Doug Lea <dl@cs.oswego.edu>, Paul Sandoz <paul.sandoz@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;
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    29
import java.util.function.DoubleConsumer;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    30
import java.util.function.IntConsumer;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    31
import java.util.function.LongConsumer;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    32
import java.util.stream.DoubleStream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    33
import java.util.stream.IntStream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    34
import java.util.stream.LongStream;
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    35
import java.util.stream.StreamSupport;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * An instance of this class is used to generate a stream of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * pseudorandom numbers. The class uses a 48-bit seed, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * modified using a linear congruential formula. (See Donald Knuth,
1818
7847313afae6 6792545: Typo in java.util.Collection JavaDoc
darcy
parents: 715
diff changeset
    43
 * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * If two instances of {@code Random} are created with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * seed, and the same sequence of method calls is made for each, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * will generate and return identical sequences of numbers. In order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * guarantee this property, particular algorithms are specified for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * class {@code Random}. Java implementations must use all the algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * shown here for the class {@code Random}, for the sake of absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * portability of Java code. However, subclasses of class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * are permitted to use other algorithms, so long as they adhere to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * general contracts for all the methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The algorithms implemented by class {@code Random} use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@code protected} utility method that on each invocation can supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * up to 32 pseudorandomly generated bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Many applications will find the method {@link Math#random} simpler to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    61
 * <p>Instances of {@code java.util.Random} are threadsafe.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    62
 * However, the concurrent use of the same {@code java.util.Random}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    63
 * instance across threads may encounter contention and consequent
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    64
 * poor performance. Consider instead using
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    65
 * {@link java.util.concurrent.ThreadLocalRandom} in multithreaded
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    66
 * designs.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    67
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    68
 * <p>Instances of {@code java.util.Random} are not cryptographically
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    69
 * secure.  Consider instead using {@link java.security.SecureRandom} to
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    70
 * get a cryptographically secure pseudo-random number generator for use
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    71
 * by security-sensitive applications.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1818
diff changeset
    72
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @author  Frank Yellin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @since   1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
class Random implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** use serialVersionUID from JDK 1.1 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static final long serialVersionUID = 3905348978240129619L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The internal state associated with this pseudorandom number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * (The specs for the methods in this class describe the ongoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * computation of this value.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private final AtomicLong seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    88
    private static final long multiplier = 0x5DEECE66DL;
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    89
    private static final long addend = 0xBL;
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    90
    private static final long mask = (1L << 48) - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    92
    private static final double DOUBLE_UNIT = 1.0 / (1L << 53);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    93
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    94
    // IllegalArgumentException messages
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    95
    static final String BadBound = "bound must be positive";
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    96
    static final String BadRange = "bound must be greater than origin";
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    97
    static final String BadSize  = "size must be non-negative";
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
    98
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Creates a new random number generator. This constructor sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * the seed of the random number generator to a value very likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * to be distinct from any other invocation of this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   104
    public Random() {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   105
        this(seedUniquifier() ^ System.nanoTime());
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 long seedUniquifier() {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   109
        // L'Ecuyer, "Tables of Linear Congruential Generators of
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   110
        // Different Sizes and Good Lattice Structure", 1999
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   111
        for (;;) {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   112
            long current = seedUniquifier.get();
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   113
            long next = current * 181783497276652981L;
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   114
            if (seedUniquifier.compareAndSet(current, next))
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   115
                return next;
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   116
        }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   117
    }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   118
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   119
    private static final AtomicLong seedUniquifier
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   120
        = new AtomicLong(8682522807148012L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Creates a new random number generator using a single {@code long} seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * The seed is the initial value of the internal state of the pseudorandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * number generator which is maintained by method {@link #next}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p>The invocation {@code new Random(seed)} is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * rnd.setSeed(seed);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param seed the initial seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see   #setSeed(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public Random(long seed) {
10056
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   136
        if (getClass() == Random.class)
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   137
            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
   138
        else {
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   139
            // subclass might have overriden setSeed
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   140
            this.seed = new AtomicLong();
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   141
            setSeed(seed);
7464dc04ae22 7051516: ThreadLocalRandom seed is never initialized so all instances generate the same sequence
dl
parents: 7668
diff changeset
   142
        }
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   143
    }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   144
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   145
    private static long initialScramble(long seed) {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   146
        return (seed ^ multiplier) & mask;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Sets the seed of this random number generator using a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * {@code long} seed. The general contract of {@code setSeed} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * that it alters the state of this random number generator object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * so as to be in exactly the same state as if it had just been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * created with the argument {@code seed} as a seed. The method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * {@code setSeed} is implemented by class {@code Random} by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * atomically updating the seed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *  <pre>{@code (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * and clearing the {@code haveNextNextGaussian} flag used by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * #nextGaussian}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>The implementation of {@code setSeed} by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * happens to use only 48 bits of the given seed. In general, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * an overriding method may use all 64 bits of the {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * argument as a seed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param seed the initial seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    synchronized public void setSeed(long seed) {
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 4110
diff changeset
   169
        this.seed.set(initialScramble(seed));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Generates the next pseudorandom number. Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * override this, as this is used by all other methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <p>The general contract of {@code next} is that it returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * {@code int} value and if the argument {@code bits} is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * {@code 1} and {@code 32} (inclusive), then that many low-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * bits of the returned value will be (approximately) independently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * chosen bit values, each of which is (approximately) equally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * likely to be {@code 0} or {@code 1}. The method {@code next} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * implemented by class {@code Random} by atomically updating the seed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *  <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * and returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *  <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * This is a linear congruential pseudorandom number generator, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * defined by D. H. Lehmer and described by Donald E. Knuth in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <i>The Art of Computer Programming,</i> Volume 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <i>Seminumerical Algorithms</i>, section 3.2.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param  bits random bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return the next pseudorandom value from this random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *         generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    protected int next(int bits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        long oldseed, nextseed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        AtomicLong seed = this.seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            oldseed = seed.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            nextseed = (oldseed * multiplier + addend) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } while (!seed.compareAndSet(oldseed, nextseed));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return (int)(nextseed >>> (48 - bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Generates random bytes and places them into a user-supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * byte array.  The number of random bytes produced is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * the length of the byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <p>The method {@code nextBytes} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *  <pre> {@code
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; i < bytes.length; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *     for (int rnd = nextInt(), n = Math.min(bytes.length - i, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *          n-- > 0; rnd >>= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *       bytes[i++] = (byte)rnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param  bytes the byte array to fill with random bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws NullPointerException if the byte array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void nextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        for (int i = 0, len = bytes.length; i < len; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            for (int rnd = nextInt(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                     n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                 n-- > 0; rnd >>= Byte.SIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                bytes[i++] = (byte)rnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   236
     * The form of nextLong used by LongStream Spliterators.  If
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   237
     * origin is greater than bound, acts as unbounded form of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   238
     * nextLong, else as bounded form.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   239
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   240
     * @param origin the least value, unless greater than bound
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   241
     * @param bound the upper bound (exclusive), must not equal origin
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   242
     * @return a pseudorandom value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   243
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   244
    final long internalNextLong(long origin, long bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   245
        long r = nextLong();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   246
        if (origin < bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   247
            long n = bound - origin, m = n - 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   248
            if ((n & m) == 0L)  // power of two
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   249
                r = (r & m) + origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   250
            else if (n > 0L) {  // reject over-represented candidates
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   251
                for (long u = r >>> 1;            // ensure nonnegative
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   252
                     u + m - (r = u % n) < 0L;    // rejection check
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   253
                     u = nextLong() >>> 1) // retry
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   254
                    ;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   255
                r += origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   256
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   257
            else {              // range not representable as long
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   258
                while (r < origin || r >= bound)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   259
                    r = nextLong();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   260
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   261
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   262
        return r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   263
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   264
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   265
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   266
     * The form of nextInt used by IntStream Spliterators.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   267
     * For the unbounded case: uses nextInt().
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   268
     * For the bounded case with representable range: uses nextInt(int bound)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   269
     * For the bounded case with unrepresentable range: uses nextInt()
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   270
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   271
     * @param origin the least value, unless greater than bound
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   272
     * @param bound the upper bound (exclusive), must not equal origin
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   273
     * @return a pseudorandom value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   274
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   275
    final int internalNextInt(int origin, int bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   276
        if (origin < bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   277
            int n = bound - origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   278
            if (n > 0) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   279
                return nextInt(n) + origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   280
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   281
            else {  // range not representable as int
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   282
                int r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   283
                do {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   284
                    r = nextInt();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   285
                } while (r < origin || r >= bound);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   286
                return r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   287
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   288
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   289
        else {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   290
            return nextInt();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   291
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   292
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   293
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   294
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   295
     * The form of nextDouble used by DoubleStream Spliterators.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   296
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   297
     * @param origin the least value, unless greater than bound
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   298
     * @param bound the upper bound (exclusive), must not equal origin
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   299
     * @return a pseudorandom value
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
    final double internalNextDouble(double origin, double bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   302
        double r = nextDouble();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   303
        if (origin < bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   304
            r = r * (bound - origin) + origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   305
            if (r >= bound) // correct for rounding
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   306
                r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   307
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   308
        return r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   309
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   310
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   311
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Returns the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * value from this random number generator's sequence. The general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * 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
   315
     * 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
   316
     * {@code int} values are produced with (approximately) equal probability.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>The method {@code nextInt} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * public int nextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *   return next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @return the next pseudorandom, uniformly distributed {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *         value from this random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public int nextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Returns a pseudorandom, uniformly distributed {@code int} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * between 0 (inclusive) and the specified value (exclusive), drawn from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * this random number generator's sequence.  The general contract of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * {@code nextInt} is that one {@code int} value in the specified range
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   337
     * is pseudorandomly generated and returned.  All {@code bound} possible
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * {@code int} values are produced with (approximately) equal
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   339
     * probability.  The method {@code nextInt(int bound)} is implemented by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * class {@code Random} as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *  <pre> {@code
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   342
     * public int nextInt(int bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   343
     *   if (bound <= 0)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   344
     *     throw new IllegalArgumentException("bound must be positive");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   346
     *   if ((bound & -bound) == bound)  // i.e., bound is a power of 2
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   347
     *     return (int)((bound * (long)next(31)) >> 31);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *   int bits, val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *   do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *       bits = next(31);
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   352
     *       val = bits % bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   353
     *   } while (bits - val + (bound-1) < 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *   return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * because the next method is only approximately an unbiased source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * independently chosen bits.  If it were a perfect source of randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * chosen bits, then the algorithm shown would choose {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * values from the stated range with perfect uniformity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * The algorithm is slightly tricky.  It rejects values that would result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * in an uneven distribution (due to the fact that 2^31 is not divisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * by n). The probability of a value being rejected depends on n.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * worst case is n=2^30+1, for which the probability of a reject is 1/2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * and the expected number of iterations before the loop terminates is 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * The algorithm treats the case where n is a power of two specially: it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * returns the correct number of high-order bits from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * pseudo-random number generator.  In the absence of special treatment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * the correct number of <i>low-order</i> bits would be returned.  Linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * congruential pseudo-random number generators such as the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * implemented by this class are known to have short periods in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * sequence of values of their low-order bits.  Thus, this special case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * greatly increases the length of the sequence of values returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * successive calls to this method if n is a small power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   379
     * @param bound the upper bound (exclusive).  Must be positive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @return the next pseudorandom, uniformly distributed {@code int}
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   381
     *         value between zero (inclusive) and {@code bound} (exclusive)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *         from this random number generator's sequence
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   383
     * @throws IllegalArgumentException if bound is not positive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   386
    public int nextInt(int bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   387
        if (bound <= 0)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   388
            throw new IllegalArgumentException(BadBound);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   390
        int r = next(31);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   391
        int m = bound - 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   392
        if ((bound & m) == 0)  // i.e., bound is a power of 2
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   393
            r = (int)((bound * (long)r) >> 31);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   394
        else {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   395
            for (int u = r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   396
                 u - (r = u % bound) + m < 0;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   397
                 u = next(31))
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   398
                ;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   399
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   400
        return r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Returns the next pseudorandom, uniformly distributed {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * value from this random number generator's sequence. The general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * contract of {@code nextLong} is that one {@code long} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <p>The method {@code nextLong} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * public long nextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *   return ((long)next(32) << 32) + next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Because class {@code Random} uses a seed with only 48 bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * this algorithm will not return all possible {@code long} values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @return the next pseudorandom, uniformly distributed {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *         value from this random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public long nextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // it's okay that the bottom word remains signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return ((long)(next(32)) << 32) + next(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Returns the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * {@code boolean} value from this random number generator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * sequence. The general contract of {@code nextBoolean} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * {@code boolean} value is pseudorandomly generated and returned.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * values {@code true} and {@code false} are produced with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * <p>The method {@code nextBoolean} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * public boolean nextBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *   return next(1) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @return the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *         {@code boolean} value from this random number generator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *         sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public boolean nextBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return next(1) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Returns the next pseudorandom, uniformly distributed {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * value between {@code 0.0} and {@code 1.0} from this random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <p>The general contract of {@code nextFloat} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * {@code float} value, chosen (approximately) uniformly from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * 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
   459
     * 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
   460
     * {@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
   461
     * where <i>m</i> is a positive integer less than 2<sup>24</sup>, are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * produced with (approximately) equal probability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * <p>The method {@code nextFloat} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * public float nextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *   return next(24) / ((float)(1 << 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * because the next method is only approximately an unbiased source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * independently chosen bits. If it were a perfect source of randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * chosen bits, then the algorithm shown would choose {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * values from the stated range with perfect uniformity.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * [In early versions of Java, the result was incorrectly calculated as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *   return next(30) / ((float)(1 << 30));}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * This might seem to be equivalent, if not better, but in fact it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * introduced a slight nonuniformity because of the bias in the rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * of floating-point numbers: it was slightly more likely that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * low-order bit of the significand would be 0 than that it would be 1.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @return the next pseudorandom, uniformly distributed {@code float}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *         value between {@code 0.0} and {@code 1.0} from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *         random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public float nextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return next(24) / ((float)(1 << 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Returns the next pseudorandom, uniformly distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * {@code double} value between {@code 0.0} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * {@code 1.0} from this random number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <p>The general contract of {@code nextDouble} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * {@code double} value, chosen (approximately) uniformly from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * range {@code 0.0d} (inclusive) to {@code 1.0d} (exclusive), is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * <p>The method {@code nextDouble} is implemented by class {@code Random}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * as if by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * public double nextDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *   return (((long)next(26) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *     / (double)(1L << 53);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <p>The hedge "approximately" is used in the foregoing description only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * because the {@code next} method is only approximately an unbiased
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * source of independently chosen bits. If it were a perfect source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * randomly chosen bits, then the algorithm shown would choose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * {@code double} values from the stated range with perfect uniformity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <p>[In early versions of Java, the result was incorrectly calculated as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *   return (((long)next(27) << 27) + next(27))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *     / (double)(1L << 54);}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * This might seem to be equivalent, if not better, but in fact it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * introduced a large nonuniformity because of the bias in the rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * of floating-point numbers: it was three times as likely that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * low-order bit of the significand would be 0 than that it would be 1!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * This nonuniformity probably doesn't matter much in practice, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * strive for perfection.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return the next pseudorandom, uniformly distributed {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *         value between {@code 0.0} and {@code 1.0} from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *         random number generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @see Math#random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public double nextDouble() {
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   532
        return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private double nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private boolean haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Returns the next pseudorandom, Gaussian ("normally") distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * {@code double} value with mean {@code 0.0} and standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * deviation {@code 1.0} from this random number generator's sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * The general contract of {@code nextGaussian} is that one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * {@code double} value, chosen from (approximately) the usual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * normal distribution with mean {@code 0.0} and standard deviation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * {@code 1.0}, is pseudorandomly generated and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <p>The method {@code nextGaussian} is implemented by class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * {@code Random} as if by a threadsafe version of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * private double nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * private boolean haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * public double nextGaussian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *   if (haveNextNextGaussian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *     haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *     return nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *     double v1, v2, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *     do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *       v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *       v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *       s = v1 * v1 + v2 * v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *     } while (s >= 1 || s == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *     double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *     nextNextGaussian = v2 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *     haveNextNextGaussian = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *     return v1 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * This uses the <i>polar method</i> of G. E. P. Box, M. E. Muller, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * G. Marsaglia, as described by Donald E. Knuth in <i>The Art of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Computer Programming</i>, Volume 3: <i>Seminumerical Algorithms</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * section 3.4.1, subsection C, algorithm P. Note that it generates two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * independent values at the cost of only one call to {@code StrictMath.log}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * and one call to {@code StrictMath.sqrt}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @return the next pseudorandom, Gaussian ("normally") distributed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *         {@code double} value with mean {@code 0.0} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *         standard deviation {@code 1.0} from this random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *         generator's sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    synchronized public double nextGaussian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // See Knuth, ACP, Section 3.4.1 Algorithm C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (haveNextNextGaussian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            haveNextNextGaussian = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return nextNextGaussian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            double v1, v2, s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                v1 = 2 * nextDouble() - 1; // between -1 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                v2 = 2 * nextDouble() - 1; // between -1 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                s = v1 * v1 + v2 * v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            } while (s >= 1 || s == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            nextNextGaussian = v2 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            haveNextNextGaussian = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            return v1 * multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   602
    // stream methods, coded in a way intended to better isolate for
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   603
    // maintenance purposes the small differences across forms.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   604
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   606
     * Returns a stream producing the given {@code streamSize} number of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   607
     * pseudorandom {@code int} values.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   608
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   609
     * <p>A pseudorandom {@code int} value is generated as if it's the result of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   610
     * calling the method {@link #nextInt()}.
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   611
     *
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   612
     * @param streamSize the number of values to generate
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   613
     * @return a stream of pseudorandom {@code int} values
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   614
     * @throws IllegalArgumentException if {@code streamSize} is
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   615
     *         less than zero
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   616
     * @since 1.8
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   617
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   618
    public IntStream ints(long streamSize) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   619
        if (streamSize < 0L)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   620
            throw new IllegalArgumentException(BadSize);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   621
        return StreamSupport.intStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   622
                (new RandomIntsSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   623
                         (this, 0L, streamSize, Integer.MAX_VALUE, 0),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   624
                 false);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   625
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   626
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   627
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   628
     * Returns an effectively unlimited stream of pseudorandom {@code int}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   629
     * values.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   630
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   631
     * <p>A pseudorandom {@code int} value is generated as if it's the result of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   632
     * calling the method {@link #nextInt()}.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   633
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   634
     * @implNote This method is implemented to be equivalent to {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   635
     * ints(Long.MAX_VALUE)}.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   636
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   637
     * @return a stream of pseudorandom {@code int} values
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   638
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   639
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   640
    public IntStream ints() {
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   641
        return StreamSupport.intStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   642
                (new RandomIntsSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   643
                         (this, 0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   644
                 false);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   645
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   646
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   647
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   648
     * Returns a stream producing the given {@code streamSize} number
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   649
     * of pseudorandom {@code int} values, each conforming to the given
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   650
     * origin (inclusive) and bound (exclusive).
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   651
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   652
     * <p>A pseudorandom {@code int} value is generated as if it's the result of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   653
     * calling the following method with the origin and bound:
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   654
     * <pre> {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   655
     * int nextInt(int origin, int bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   656
     *   int n = bound - origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   657
     *   if (n > 0) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   658
     *     return nextInt(n) + origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   659
     *   }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   660
     *   else {  // range not representable as int
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   661
     *     int r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   662
     *     do {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   663
     *       r = nextInt();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   664
     *     } while (r < origin || r >= bound);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   665
     *     return r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   666
     *   }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   667
     * }}</pre>
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   668
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   669
     * @param streamSize the number of values to generate
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   670
     * @param randomNumberOrigin the origin (inclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   671
     * @param randomNumberBound the bound (exclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   672
     * @return a stream of pseudorandom {@code int} values,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   673
     *         each with the given origin (inclusive) and bound (exclusive)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   674
     * @throws IllegalArgumentException if {@code streamSize} is
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   675
     *         less than zero, or {@code randomNumberOrigin}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   676
     *         is greater than or equal to {@code randomNumberBound}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   677
     * @since 1.8
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   678
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   679
    public IntStream ints(long streamSize, int randomNumberOrigin,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   680
                          int randomNumberBound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   681
        if (streamSize < 0L)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   682
            throw new IllegalArgumentException(BadSize);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   683
        if (randomNumberOrigin >= randomNumberBound)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   684
            throw new IllegalArgumentException(BadRange);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   685
        return StreamSupport.intStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   686
                (new RandomIntsSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   687
                         (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   688
                 false);
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   689
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   690
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   691
    /**
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   692
     * Returns an effectively unlimited stream of pseudorandom {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   693
     * int} values, each conforming to the given origin (inclusive) and bound
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   694
     * (exclusive).
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   695
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   696
     * <p>A pseudorandom {@code int} value is generated as if it's the result of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   697
     * calling the following method with the origin and bound:
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   698
     * <pre> {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   699
     * int nextInt(int origin, int bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   700
     *   int n = bound - origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   701
     *   if (n > 0) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   702
     *     return nextInt(n) + origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   703
     *   }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   704
     *   else {  // range not representable as int
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   705
     *     int r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   706
     *     do {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   707
     *       r = nextInt();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   708
     *     } while (r < origin || r >= bound);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   709
     *     return r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   710
     *   }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   711
     * }}</pre>
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   712
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   713
     * @implNote This method is implemented to be equivalent to {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   714
     * ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   715
     *
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   716
     * @param randomNumberOrigin the origin (inclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   717
     * @param randomNumberBound the bound (exclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   718
     * @return a stream of pseudorandom {@code int} values,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   719
     *         each with the given origin (inclusive) and bound (exclusive)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   720
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   721
     *         is greater than or equal to {@code randomNumberBound}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   722
     * @since 1.8
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   723
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   724
    public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   725
        if (randomNumberOrigin >= randomNumberBound)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   726
            throw new IllegalArgumentException(BadRange);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   727
        return StreamSupport.intStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   728
                (new RandomIntsSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   729
                         (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   730
                 false);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   731
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   732
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   733
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   734
     * Returns a stream producing the given {@code streamSize} number of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   735
     * pseudorandom {@code long} values.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   736
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   737
     * <p>A pseudorandom {@code long} value is generated as if it's the result
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   738
     * of calling the method {@link #nextLong()}.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   739
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   740
     * @param streamSize the number of values to generate
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   741
     * @return a stream of pseudorandom {@code long} values
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   742
     * @throws IllegalArgumentException if {@code streamSize} is
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   743
     *         less than zero
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   744
     * @since 1.8
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   745
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   746
    public LongStream longs(long streamSize) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   747
        if (streamSize < 0L)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   748
            throw new IllegalArgumentException(BadSize);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   749
        return StreamSupport.longStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   750
                (new RandomLongsSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   751
                         (this, 0L, streamSize, Long.MAX_VALUE, 0L),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   752
                 false);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   753
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   754
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   755
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   756
     * Returns an effectively unlimited stream of pseudorandom {@code long}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   757
     * values.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   758
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   759
     * <p>A pseudorandom {@code long} value is generated as if it's the result
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   760
     * of calling the method {@link #nextLong()}.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   761
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   762
     * @implNote This method is implemented to be equivalent to {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   763
     * longs(Long.MAX_VALUE)}.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   764
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   765
     * @return a stream of pseudorandom {@code long} values
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   766
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   767
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   768
    public LongStream longs() {
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   769
        return StreamSupport.longStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   770
                (new RandomLongsSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   771
                         (this, 0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   772
                 false);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   773
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   774
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   775
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   776
     * Returns a stream producing the given {@code streamSize} number of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   777
     * pseudorandom {@code long}, each conforming to the given origin
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   778
     * (inclusive) and bound (exclusive).
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   779
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   780
     * <p>A pseudorandom {@code long} value is generated as if it's the result
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   781
     * of calling the following method with the origin and bound:
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   782
     * <pre> {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   783
     * long nextLong(long origin, long bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   784
     *   long r = nextLong();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   785
     *   long n = bound - origin, m = n - 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   786
     *   if ((n & m) == 0L)  // power of two
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   787
     *     r = (r & m) + origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   788
     *   else if (n > 0L) {  // reject over-represented candidates
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   789
     *     for (long u = r >>> 1;            // ensure nonnegative
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   790
     *          u + m - (r = u % n) < 0L;    // rejection check
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   791
     *          u = nextLong() >>> 1) // retry
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   792
     *         ;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   793
     *     r += origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   794
     *   }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   795
     *   else {              // range not representable as long
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   796
     *     while (r < origin || r >= bound)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   797
     *       r = nextLong();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   798
     *   }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   799
     *   return r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   800
     * }}</pre>
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   801
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   802
     * @param streamSize the number of values to generate
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   803
     * @param randomNumberOrigin the origin (inclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   804
     * @param randomNumberBound the bound (exclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   805
     * @return a stream of pseudorandom {@code long} values,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   806
     *         each with the given origin (inclusive) and bound (exclusive)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   807
     * @throws IllegalArgumentException if {@code streamSize} is
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   808
     *         less than zero, or {@code randomNumberOrigin}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   809
     *         is greater than or equal to {@code randomNumberBound}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   810
     * @since 1.8
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   811
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   812
    public LongStream longs(long streamSize, long randomNumberOrigin,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   813
                            long randomNumberBound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   814
        if (streamSize < 0L)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   815
            throw new IllegalArgumentException(BadSize);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   816
        if (randomNumberOrigin >= randomNumberBound)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   817
            throw new IllegalArgumentException(BadRange);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   818
        return StreamSupport.longStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   819
                (new RandomLongsSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   820
                         (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   821
                 false);
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   822
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   823
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   824
    /**
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   825
     * Returns an effectively unlimited stream of pseudorandom {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   826
     * long} values, each conforming to the given origin (inclusive) and bound
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   827
     * (exclusive).
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   828
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   829
     * <p>A pseudorandom {@code long} value is generated as if it's the result
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   830
     * of calling the following method with the origin and bound:
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   831
     * <pre> {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   832
     * long nextLong(long origin, long bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   833
     *   long r = nextLong();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   834
     *   long n = bound - origin, m = n - 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   835
     *   if ((n & m) == 0L)  // power of two
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   836
     *     r = (r & m) + origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   837
     *   else if (n > 0L) {  // reject over-represented candidates
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   838
     *     for (long u = r >>> 1;            // ensure nonnegative
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   839
     *          u + m - (r = u % n) < 0L;    // rejection check
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   840
     *          u = nextLong() >>> 1) // retry
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   841
     *         ;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   842
     *     r += origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   843
     *   }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   844
     *   else {              // range not representable as long
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   845
     *     while (r < origin || r >= bound)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   846
     *       r = nextLong();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   847
     *   }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   848
     *   return r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   849
     * }}</pre>
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   850
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   851
     * @implNote This method is implemented to be equivalent to {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   852
     * longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   853
     *
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   854
     * @param randomNumberOrigin the origin (inclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   855
     * @param randomNumberBound the bound (exclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   856
     * @return a stream of pseudorandom {@code long} values,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   857
     *         each with the given origin (inclusive) and bound (exclusive)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   858
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   859
     *         is greater than or equal to {@code randomNumberBound}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   860
     * @since 1.8
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   861
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   862
    public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   863
        if (randomNumberOrigin >= randomNumberBound)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   864
            throw new IllegalArgumentException(BadRange);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   865
        return StreamSupport.longStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   866
                (new RandomLongsSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   867
                         (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   868
                 false);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   869
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   870
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   871
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   872
     * Returns a stream producing the given {@code streamSize} number of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   873
     * pseudorandom {@code double} values, each between zero
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   874
     * (inclusive) and one (exclusive).
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   875
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   876
     * <p>A pseudorandom {@code double} value is generated as if it's the result
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   877
     * of calling the method {@link #nextDouble()}}.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   878
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   879
     * @param streamSize the number of values to generate
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   880
     * @return a stream of {@code double} values
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   881
     * @throws IllegalArgumentException if {@code streamSize} is
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   882
     *         less than zero
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   883
     * @since 1.8
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   884
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   885
    public DoubleStream doubles(long streamSize) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   886
        if (streamSize < 0L)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   887
            throw new IllegalArgumentException(BadSize);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   888
        return StreamSupport.doubleStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   889
                (new RandomDoublesSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   890
                         (this, 0L, streamSize, Double.MAX_VALUE, 0.0),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   891
                 false);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   892
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   893
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   894
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   895
     * Returns an effectively unlimited stream of pseudorandom {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   896
     * double} values, each between zero (inclusive) and one
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   897
     * (exclusive).
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   898
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   899
     * <p>A pseudorandom {@code double} value is generated as if it's the result
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   900
     * of calling the method {@link #nextDouble()}}.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   901
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   902
     * @implNote This method is implemented to be equivalent to {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   903
     * doubles(Long.MAX_VALUE)}.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   904
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   905
     * @return a stream of pseudorandom {@code double} values
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   906
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   907
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   908
    public DoubleStream doubles() {
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   909
        return StreamSupport.doubleStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   910
                (new RandomDoublesSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   911
                         (this, 0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   912
                 false);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   913
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   914
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   915
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   916
     * Returns a stream producing the given {@code streamSize} number of
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   917
     * pseudorandom {@code double} values, each conforming to the given origin
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   918
     * (inclusive) and bound (exclusive).
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   919
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   920
     * <p>A pseudorandom {@code double} value is generated as if it's the result
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   921
     * of calling the following method with the origin and bound:
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   922
     * <pre> {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   923
     * double nextDouble(double origin, double bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   924
     *   double r = nextDouble();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   925
     *   r = r * (bound - origin) + origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   926
     *   if (r >= bound) // correct for rounding
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   927
     *     r = Math.nextDown(bound);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   928
     *   return r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   929
     * }}</pre>
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   930
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   931
     * @param streamSize the number of values to generate
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   932
     * @param randomNumberOrigin the origin (inclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   933
     * @param randomNumberBound the bound (exclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   934
     * @return a stream of pseudorandom {@code double} values,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   935
     *         each with the given origin (inclusive) and bound (exclusive)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   936
     * @throws IllegalArgumentException if {@code streamSize} is
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   937
     *         less than zero
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   938
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   939
     *         is greater than or equal to {@code randomNumberBound}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   940
     * @since 1.8
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   941
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   942
    public DoubleStream doubles(long streamSize, double randomNumberOrigin,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   943
                                double randomNumberBound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   944
        if (streamSize < 0L)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   945
            throw new IllegalArgumentException(BadSize);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   946
        if (!(randomNumberOrigin < randomNumberBound))
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   947
            throw new IllegalArgumentException(BadRange);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   948
        return StreamSupport.doubleStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   949
                (new RandomDoublesSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   950
                         (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   951
                 false);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   952
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   953
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   954
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   955
     * Returns an effectively unlimited stream of pseudorandom {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   956
     * double} values, each conforming to the given origin (inclusive) and bound
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   957
     * (exclusive).
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   958
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   959
     * <p>A pseudorandom {@code double} value is generated as if it's the result
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   960
     * of calling the following method with the origin and bound:
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   961
     * <pre> {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   962
     * double nextDouble(double origin, double bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   963
     *   double r = nextDouble();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   964
     *   r = r * (bound - origin) + origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   965
     *   if (r >= bound) // correct for rounding
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   966
     *     r = Math.nextDown(bound);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   967
     *   return r;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   968
     * }}</pre>
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   969
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   970
     * @implNote This method is implemented to be equivalent to {@code
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   971
     * doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   972
     *
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   973
     * @param randomNumberOrigin the origin (inclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   974
     * @param randomNumberBound the bound (exclusive) of each random value
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   975
     * @return a stream of pseudorandom {@code double} values,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   976
     *         each with the given origin (inclusive) and bound (exclusive)
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   977
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   978
     *         is greater than or equal to {@code randomNumberBound}
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   979
     * @since 1.8
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   980
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   981
    public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   982
        if (!(randomNumberOrigin < randomNumberBound))
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   983
            throw new IllegalArgumentException(BadRange);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   984
        return StreamSupport.doubleStream
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   985
                (new RandomDoublesSpliterator
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   986
                         (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   987
                 false);
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   988
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   989
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
   990
    /**
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   991
     * Spliterator for int streams.  We multiplex the four int
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   992
     * versions into one class by treating a bound less than origin as
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   993
     * unbounded, and also by treating "infinite" as equivalent to
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   994
     * Long.MAX_VALUE. For splits, it uses the standard divide-by-two
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   995
     * approach. The long and double versions of this class are
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   996
     * identical except for types.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   997
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   998
    static final class RandomIntsSpliterator implements Spliterator.OfInt {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
   999
        final Random rng;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1000
        long index;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1001
        final long fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1002
        final int origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1003
        final int bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1004
        RandomIntsSpliterator(Random rng, long index, long fence,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1005
                              int origin, int bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1006
            this.rng = rng; this.index = index; this.fence = fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1007
            this.origin = origin; this.bound = bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1008
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1009
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1010
        public RandomIntsSpliterator trySplit() {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1011
            long i = index, m = (i + fence) >>> 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1012
            return (m <= i) ? null :
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1013
                   new RandomIntsSpliterator(rng, i, index = m, origin, bound);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1014
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1015
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1016
        public long estimateSize() {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1017
            return fence - index;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1018
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1019
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1020
        public int characteristics() {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1021
            return (Spliterator.SIZED | Spliterator.SUBSIZED |
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1022
                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1023
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1024
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1025
        public boolean tryAdvance(IntConsumer consumer) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1026
            if (consumer == null) throw new NullPointerException();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1027
            long i = index, f = fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1028
            if (i < f) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1029
                consumer.accept(rng.internalNextInt(origin, bound));
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1030
                index = i + 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1031
                return true;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1032
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1033
            return false;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1034
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1035
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1036
        public void forEachRemaining(IntConsumer consumer) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1037
            if (consumer == null) throw new NullPointerException();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1038
            long i = index, f = fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1039
            if (i < f) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1040
                index = f;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1041
                Random r = rng;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1042
                int o = origin, b = bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1043
                do {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1044
                    consumer.accept(r.internalNextInt(o, b));
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1045
                } while (++i < f);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1046
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1047
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1048
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1049
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1050
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1051
     * Spliterator for long streams.
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1052
     */
19609
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1053
    static final class RandomLongsSpliterator implements Spliterator.OfLong {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1054
        final Random rng;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1055
        long index;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1056
        final long fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1057
        final long origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1058
        final long bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1059
        RandomLongsSpliterator(Random rng, long index, long fence,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1060
                               long origin, long bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1061
            this.rng = rng; this.index = index; this.fence = fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1062
            this.origin = origin; this.bound = bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1063
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1064
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1065
        public RandomLongsSpliterator trySplit() {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1066
            long i = index, m = (i + fence) >>> 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1067
            return (m <= i) ? null :
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1068
                   new RandomLongsSpliterator(rng, i, index = m, origin, bound);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1069
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1070
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1071
        public long estimateSize() {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1072
            return fence - index;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1073
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1074
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1075
        public int characteristics() {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1076
            return (Spliterator.SIZED | Spliterator.SUBSIZED |
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1077
                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1078
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1079
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1080
        public boolean tryAdvance(LongConsumer consumer) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1081
            if (consumer == null) throw new NullPointerException();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1082
            long i = index, f = fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1083
            if (i < f) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1084
                consumer.accept(rng.internalNextLong(origin, bound));
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1085
                index = i + 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1086
                return true;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1087
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1088
            return false;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1089
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1090
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1091
        public void forEachRemaining(LongConsumer consumer) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1092
            if (consumer == null) throw new NullPointerException();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1093
            long i = index, f = fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1094
            if (i < f) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1095
                index = f;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1096
                Random r = rng;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1097
                long o = origin, b = bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1098
                do {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1099
                    consumer.accept(r.internalNextLong(o, b));
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1100
                } while (++i < f);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1101
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1102
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1103
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1104
    }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1105
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1106
    /**
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1107
     * Spliterator for double streams.
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1108
     */
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1109
    static final class RandomDoublesSpliterator implements Spliterator.OfDouble {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1110
        final Random rng;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1111
        long index;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1112
        final long fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1113
        final double origin;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1114
        final double bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1115
        RandomDoublesSpliterator(Random rng, long index, long fence,
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1116
                                 double origin, double bound) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1117
            this.rng = rng; this.index = index; this.fence = fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1118
            this.origin = origin; this.bound = bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1119
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1120
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1121
        public RandomDoublesSpliterator trySplit() {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1122
            long i = index, m = (i + fence) >>> 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1123
            return (m <= i) ? null :
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1124
                   new RandomDoublesSpliterator(rng, i, index = m, origin, bound);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1125
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1126
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1127
        public long estimateSize() {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1128
            return fence - index;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1129
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1130
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1131
        public int characteristics() {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1132
            return (Spliterator.SIZED | Spliterator.SUBSIZED |
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1133
                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1134
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1135
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1136
        public boolean tryAdvance(DoubleConsumer consumer) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1137
            if (consumer == null) throw new NullPointerException();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1138
            long i = index, f = fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1139
            if (i < f) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1140
                consumer.accept(rng.internalNextDouble(origin, bound));
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1141
                index = i + 1;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1142
                return true;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1143
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1144
            return false;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1145
        }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1146
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1147
        public void forEachRemaining(DoubleConsumer consumer) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1148
            if (consumer == null) throw new NullPointerException();
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1149
            long i = index, f = fence;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1150
            if (i < f) {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1151
                index = f;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1152
                Random r = rng;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1153
                double o = origin, b = bound;
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1154
                do {
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1155
                    consumer.accept(r.internalNextDouble(o, b));
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1156
                } while (++i < f);
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1157
            }
108f52a7438f 8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom
psandoz
parents: 19074
diff changeset
  1158
        }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1159
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1160
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1161
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * Serializable fields for Random.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @serialField    seed long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *              seed for random computations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @serialField    nextNextGaussian double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *              next Gaussian to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @serialField      haveNextNextGaussian boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *              nextNextGaussian is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        new ObjectStreamField("seed", Long.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        new ObjectStreamField("nextNextGaussian", Double.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * Reconstitute the {@code Random} instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        // The seed is read in as {@code long} for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        // historical reasons, but it is converted to an AtomicLong.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1188
        long seedVal = fields.get("seed", -1L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (seedVal < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
          throw new java.io.StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                              "Random: invalid seed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        resetSeed(seedVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        nextNextGaussian = fields.get("nextNextGaussian", 0.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * Save the {@code Random} instance to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    synchronized private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        // set the values of the Serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        // The seed is serialized as a long for historical reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        fields.put("seed", seed.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        fields.put("nextNextGaussian", nextNextGaussian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        fields.put("haveNextNextGaussian", haveNextNextGaussian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        // save them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    // Support for resetting seed while deserializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    private static final long seedOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            seedOffset = unsafe.objectFieldOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                (Random.class.getDeclaredField("seed"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        } catch (Exception ex) { throw new Error(ex); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    private void resetSeed(long seedVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
}