src/java.base/share/classes/java/util/random/RandomNumberGenerator.java
author jlaskey
Thu, 27 Jun 2019 18:30:27 -0300
branchJDK-8193209-branch
changeset 57437 f02ffcb61dce
parent 57388 src/java.base/share/classes/java/util/Rng.java@b1e6bc96af3d
permissions -rw-r--r--
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     1
/*
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     4
 *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    10
 *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    15
 * accompanied this code).
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    16
 *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    20
 *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    23
 * questions.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    24
 */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    25
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    26
package java.util.random;
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    27
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    28
import java.math.BigInteger;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    29
import java.util.stream.DoubleStream;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    30
import java.util.stream.IntStream;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    31
import java.util.stream.LongStream;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    32
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    33
/**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    34
 * The {@link RandomNumberGenerator} interface is designed to provide a common protocol for objects
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    35
 * that generate random or (more typically) pseudorandom sequences of numbers (or Boolean values).
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    36
 * Such a sequence may be obtained by either repeatedly invoking a method that returns a single
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    37
 * (pseudo)randomly chosen value, or by invoking a method that returns a stream of (pseudo)randomly
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    38
 * chosen values.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    39
 * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    40
 * Ideally, given an implicitly or explicitly specified range of values, each value would be chosen
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    41
 * independently and uniformly from that range. In practice, one may have to settle for some
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    42
 * approximation to independence and uniformity.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    43
 * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    44
 * In the case of {@code int}, {@code long}, and {@link Boolean} values, if there is no explicit
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    45
 * specification of range, then the range includes all possible values of the type.  In the case of
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    46
 * {@code float} and {@code double} values, a value is always chosen from the set of
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    47
 * 2<sup><i>w</i></sup> values between 0.0 (inclusive) and 1.0 (exclusive), where <i>w</i> is 23 for
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    48
 * {@code float} values and 52 for {@code double} values, such that adjacent values differ by
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    49
 * 2<sup>&minus;<i>w</i></sup>; if an explicit range is specified, then the chosen number is
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    50
 * computationally scaled and translated so as to appear to have been chosen from that range.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    51
 * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    52
 * Each method that returns a stream produces a stream of values each of which is chosen in the same
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    53
 * manner as for a method that returns a single (pseudo)randomly chosen value.  For example, if
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    54
 * {@code r} implements {@link RandomNumberGenerator}, then the method call {@code r.ints(100)}
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    55
 * returns a stream of 100 {@code int} values.  These are not necessarily the exact same values that
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    56
 * would have been returned if instead {@code r.nextInt()} had been called 100 times; all that is
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    57
 * guaranteed is that each value in the stream is chosen in a similar (pseudo)random manner from the
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    58
 * same range.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    59
 * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    60
 * Every object that implements the {@link RandomNumberGenerator} interface is assumed to contain a
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    61
 * finite amount of state.  Using such an object to generate a pseudorandomly chosen value alters
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    62
 * its state.  The number of distinct possible states of such an object is called its
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    63
 * <i>period</i>.  (Some implementations of the {@link RandomNumberGenerator} interface
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    64
 * may be truly random rather than pseudorandom, for example relying on the statistical behavior of
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    65
 * a physical object to derive chosen values.  Such implementations do not have a fixed period.)
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    66
 * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    67
 * As a rule, objects that implement the {@link RandomNumberGenerator} interface need not be
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    68
 * thread-safe.  It is recommended that multithreaded applications use either {@link
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    69
 * ThreadLocalRandom} or (preferably) pseudorandom number generators that implement the {@link
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    70
 * SplittableRNG} or {@link JumpableRNG} interface.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    71
 * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    72
 * To implement this interface, a class only needs to provide concrete definitions for the methods
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    73
 * {@code nextLong()} and {@code period()}. Default implementations are provided for all other
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    74
 * methods (but it may be desirable to override some of them, especially {@code nextInt()} if the
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    75
 * underlying algorithm is {@code int}-based). Moerover, it may be preferable instead to implement
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    76
 * another interface such as {@link JumpableRNG} or {@link LeapableRNG}, or to extend an abstract
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    77
 * class such as {@link AbstractSplittableRNG} or {@link AbstractArbitrarilyJumpableRNG}.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    78
 * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    79
 * Objects that implement {@link RandomNumberGenerator} are typically not cryptographically secure.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    80
 * Consider instead using {@link java.security.SecureRandom} to get a cryptographically secure
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    81
 * pseudorandom number generator for use by security-sensitive applications.  Note, however, that
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    82
 * {@code java.security.SecureRandom} does implement the {@link RandomNumberGenerator} interface, so
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    83
 * that instances of {@code java.security.SecureRandom} may be used interchangeably with other types
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    84
 * of pseudorandom generators in applications that do not require a secure generator.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    85
 *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    86
 * @since 14
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    87
 */
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    88
public interface RandomNumberGenerator {
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    89
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    90
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    91
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    92
     * {@code double} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    93
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    94
     * @return a stream of pseudorandomly chosen {@code double} values
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
    95
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    96
     * @implNote It is permitted to implement this method in a manner
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    97
     * equivalent to {@code doubles(Long.MAX_VALUE)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    98
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    99
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   100
     * that repeatedly calls {@code nextDouble()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   101
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   102
    default DoubleStream doubles() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   103
        return DoubleStream.generate(this::nextDouble).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   104
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   105
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   106
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   107
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   108
     * {@code double} values, where each value is between the specified
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   109
     * origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   110
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   111
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   112
     * @param randomNumberBound the upper bound (exclusive) for each value produced
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   113
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   114
     * @return a stream of pseudorandomly chosen {@code double} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   115
     *         the specified origin (inclusive) and the specified bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   116
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   117
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   118
     *         is greater than or equal to {@code randomNumberBound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   119
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   120
     * @implNote It is permitted to implement this method in a manner
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   121
     *           equivalent to
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   122
     *           {@code doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   123
     * @implNote The default implementation produces a sequential stream that repeatedly
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   124
     *           calls {@code nextDouble(randomNumberOrigin, randomNumberBound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   125
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   126
    default DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   127
        RNGSupport.checkRange(randomNumberOrigin, randomNumberBound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   128
        return DoubleStream.generate(() -> nextDouble(randomNumberOrigin, randomNumberBound)).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   129
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   130
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   131
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   132
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   133
     * pseudorandomly chosen {@code double} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   134
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   135
     * @param streamSize the number of values to generate
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   136
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   137
     * @return a stream of pseudorandomly chosen {@code double} values
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   138
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   139
     * @throws IllegalArgumentException if {@code streamSize} is
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   140
     *         less than zero
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   141
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   142
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   143
     * that repeatedly calls {@code nextDouble()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   144
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   145
    default DoubleStream doubles(long streamSize) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   146
        RNGSupport.checkStreamSize(streamSize);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   147
        return doubles().limit(streamSize);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   148
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   149
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   150
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   151
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   152
     * pseudorandomly chosen {@code double} values, where each value is between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   153
     * the specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   154
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   155
     * @param streamSize the number of values to generate
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   156
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   157
     * @param randomNumberBound the upper bound (exclusive) for each value produced
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   158
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   159
     * @return a stream of pseudorandomly chosen {@code double} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   160
     *         the specified origin (inclusive) and the specified bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   161
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   162
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   163
     *         less than zero, or {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   164
     *         is greater than or equal to {@code randomNumberBound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   165
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   166
     * @implNote The default implementation produces a sequential stream
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   167
     * that repeatedly calls {@code nextDouble(randomNumberOrigin, randomNumberBound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   168
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   169
    default DoubleStream doubles(long streamSize, double randomNumberOrigin,
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   170
                          double randomNumberBound) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   171
        RNGSupport.checkStreamSize(streamSize);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   172
        RNGSupport.checkRange(randomNumberOrigin, randomNumberBound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   173
        return doubles(randomNumberOrigin, randomNumberBound).limit(streamSize);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   174
    }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   175
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   176
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   177
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   178
     * {@code int} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   179
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   180
     * @return a stream of pseudorandomly chosen {@code int} values
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   181
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   182
     * @implNote It is permitted to implement this method in a manner
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   183
     * equivalent to {@code ints(Long.MAX_VALUE)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   184
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   185
     * that repeatedly calls {@code nextInt()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   186
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   187
    default IntStream ints() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   188
        return IntStream.generate(this::nextInt).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   189
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   190
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   191
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   192
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   193
     * {@code int} values, where each value is between the specified
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   194
     * origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   195
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   196
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   197
     * @param randomNumberBound the upper bound (exclusive) for each value produced
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   198
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   199
     * @return a stream of pseudorandomly chosen {@code int} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   200
     *         the specified origin (inclusive) and the specified bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   201
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   202
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   203
     *         is greater than or equal to {@code randomNumberBound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   204
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   205
     * @implNote It is permitted to implement this method in a manner equivalent to
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   206
     *           {@code ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   207
     * @implNote The default implementation produces a sequential stream that repeatedly
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   208
     *           calls {@code nextInt(randomNumberOrigin, randomNumberBound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   209
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   210
    default IntStream ints(int randomNumberOrigin, int randomNumberBound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   211
        RNGSupport.checkRange(randomNumberOrigin, randomNumberBound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   212
        return IntStream.generate(() -> nextInt(randomNumberOrigin, randomNumberBound)).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   213
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   214
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   215
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   216
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   217
     * pseudorandomly chosen {@code int} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   218
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   219
     * @param streamSize the number of values to generate
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   220
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   221
     * @return a stream of pseudorandomly chosen {@code int} values
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   222
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   223
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   224
     *         less than zero
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   225
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   226
     * @implNote The default implementation produces a sequential stream
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   227
     *           that repeatedly calls {@code nextInt()}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   228
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   229
    default IntStream ints(long streamSize) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   230
        RNGSupport.checkStreamSize(streamSize);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   231
        return ints().limit(streamSize);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   232
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   233
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   234
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   235
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   236
     * pseudorandomly chosen {@code int} values, where each value is between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   237
     * the specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   238
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   239
     * @param streamSize the number of values to generate
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   240
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   241
     * @param randomNumberBound the upper bound (exclusive) for each value produced
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   242
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   243
     * @return a stream of pseudorandomly chosen {@code int} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   244
     *         the specified origin (inclusive) and the specified bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   245
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   246
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   247
     *         less than zero, or {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   248
     *         is greater than or equal to {@code randomNumberBound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   249
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   250
     * @implNote The default implementation produces a sequential stream that repeatedly
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   251
     *           calls {@code nextInt(randomNumberOrigin, randomNumberBound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   252
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   253
    default IntStream ints(long streamSize, int randomNumberOrigin,
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   254
                          int randomNumberBound) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   255
        RNGSupport.checkStreamSize(streamSize);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   256
        RNGSupport.checkRange(randomNumberOrigin, randomNumberBound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   257
        return ints(randomNumberOrigin, randomNumberBound).limit(streamSize);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   258
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   259
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   260
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   261
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   262
     * {@code long} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   263
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   264
     * @return a stream of pseudorandomly chosen {@code long} values
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   265
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   266
     * @implNote It is permitted to implement this method in a manner
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   267
     *           equivalent to {@code longs(Long.MAX_VALUE)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   268
     * @implNote The default implementation produces a sequential stream
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   269
     *           that repeatedly calls {@code nextLong()}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   270
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   271
    default LongStream longs() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   272
        return LongStream.generate(this::nextLong).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   273
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   274
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   275
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   276
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   277
     * {@code long} values, where each value is between the specified
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   278
     * origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   279
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   280
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   281
     * @param randomNumberBound the upper bound (exclusive) for each value produced
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   282
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   283
     * @return a stream of pseudorandomly chosen {@code long} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   284
     *         the specified origin (inclusive) and the specified bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   285
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   286
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   287
     *         is greater than or equal to {@code randomNumberBound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   288
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   289
     * @implNote It is permitted to implement this method in a manner
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   290
     *           equivalent to {@code longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   291
     * @implNote The default implementation produces a sequential stream that repeatedly
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   292
     *           calls {@code nextLong(randomNumberOrigin, randomNumberBound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   293
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   294
    default LongStream longs(long randomNumberOrigin, long randomNumberBound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   295
        RNGSupport.checkRange(randomNumberOrigin, randomNumberBound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   296
        return LongStream.generate(() -> nextLong(randomNumberOrigin, randomNumberBound)).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   297
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   298
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   299
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   300
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   301
     * pseudorandomly chosen {@code long} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   302
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   303
     * @param streamSize the number of values to generate
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   304
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   305
     * @return a stream of pseudorandomly chosen {@code long} values
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   306
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   307
     * @throws IllegalArgumentException if {@code streamSize} is
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   308
     *         less than zero
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   309
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   310
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   311
     * that repeatedly calls {@code nextLong()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   312
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   313
    default LongStream longs(long streamSize) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   314
        RNGSupport.checkStreamSize(streamSize);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   315
        return longs().limit(streamSize);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   316
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   317
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   318
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   319
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   320
     * pseudorandomly chosen {@code long} values, where each value is between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   321
     * the specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   322
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   323
     * @param streamSize the number of values to generate
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   324
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   325
     * @param randomNumberBound the upper bound (exclusive) for each value produced
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   326
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   327
     * @return a stream of pseudorandomly chosen {@code long} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   328
     *         the specified origin (inclusive) and the specified bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   329
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   330
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   331
     *         less than zero, or {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   332
     *         is greater than or equal to {@code randomNumberBound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   333
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   334
     * @implNote The default implementation produces a sequential stream that repeatedly
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   335
     *            calls {@code nextLong(randomNumberOrigin, randomNumberBound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   336
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   337
    default LongStream longs(long streamSize, long randomNumberOrigin,
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   338
                          long randomNumberBound) {
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   339
        RNGSupport.checkStreamSize(streamSize);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   340
        RNGSupport.checkRange(randomNumberOrigin, randomNumberBound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   341
        return longs(randomNumberOrigin, randomNumberBound).limit(streamSize);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   342
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   343
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   344
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   345
     * Returns a pseudorandomly chosen {@code boolean} value.
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   346
     * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   347
     * The default implementation tests the high-order bit (sign bit) of a value produced by {@code
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   348
     * nextInt()}, on the grounds that some algorithms for pseudorandom number generation produce
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   349
     * values whose high-order bits have better statistical quality than the low-order bits.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   350
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   351
     * @return a pseudorandomly chosen {@code boolean} value
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   352
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   353
    default boolean nextBoolean() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   354
        return nextInt() < 0;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   355
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   356
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   357
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   358
     * Returns a pseudorandom {@code float} value between zero (inclusive) and one (exclusive).
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   359
     * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   360
     * The default implementation uses the 24 high-order bits from a call to {@code nextInt()}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   361
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   362
     * @return a pseudorandom {@code float} value between zero (inclusive) and one (exclusive)
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   363
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   364
    default float nextFloat() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   365
        return (nextInt() >>> 8) * 0x1.0p-24f;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   366
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   367
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   368
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   369
     * Returns a pseudorandomly chosen {@code float} value between zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   370
     * (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   371
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   372
     * @param bound the upper bound (exclusive) for the returned value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   373
     *        Must be positive and finite
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   374
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   375
     * @return a pseudorandomly chosen {@code float} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   376
     *         zero (inclusive) and the bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   377
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   378
     * @throws IllegalArgumentException if {@code bound} is not
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   379
     *         positive and finite
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   380
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   381
     * @implNote The default implementation simply calls
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   382
     *     {@code RNGSupport.checkBound(bound)} and then
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   383
     *     {@code RNGSupport.boundedNextFloat(this, bound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   384
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   385
    default float nextFloat(float bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   386
        RNGSupport.checkBound(bound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   387
        return RNGSupport.boundedNextFloat(this, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   388
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   389
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   390
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   391
     * Returns a pseudorandomly chosen {@code float} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   392
     * specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   393
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   394
     * @param origin the least value that can be returned
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   395
     * @param bound the upper bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   396
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   397
     * @return a pseudorandomly chosen {@code float} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   398
     *         origin (inclusive) and the bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   399
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   400
     * @throws IllegalArgumentException unless {@code origin} is finite,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   401
     *         {@code bound} is finite, and {@code origin} is less than
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   402
     *         {@code bound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   403
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   404
     * @implNote The default implementation simply calls
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   405
     *           {@code RNGSupport.checkRange(origin, bound)} and then
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   406
     *          {@code RNGSupport.boundedNextFloat(this, origin, bound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   407
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   408
    default float nextFloat(float origin, float bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   409
        RNGSupport.checkRange(origin, bound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   410
        return RNGSupport.boundedNextFloat(this, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   411
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   412
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   413
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   414
     * Returns a pseudorandom {@code double} value between zero (inclusive) and one (exclusive).
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   415
     * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   416
     * The default implementation uses the 53 high-order bits from a call to {@code nextLong()}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   417
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   418
     * @return a pseudorandom {@code double} value between zero (inclusive) and one (exclusive)
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   419
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   420
    default double nextDouble() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   421
        return (nextLong() >>> 11) * 0x1.0p-53;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   422
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   423
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   424
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   425
     * Returns a pseudorandomly chosen {@code double} value between zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   426
     * (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   427
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   428
     * @param bound the upper bound (exclusive) for the returned value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   429
     *        Must be positive and finite
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   430
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   431
     * @return a pseudorandomly chosen {@code double} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   432
     *         zero (inclusive) and the bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   433
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   434
     * @throws IllegalArgumentException if {@code bound} is not
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   435
     *         positive and finite
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   436
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   437
     * @implNote The default implementation simply calls
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   438
     *           {@code RNGSupport.checkBound(bound)} and then
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   439
     *           {@code RNGSupport.boundedNextDouble(this, bound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   440
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   441
    default double nextDouble(double bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   442
        RNGSupport.checkBound(bound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   443
        return RNGSupport.boundedNextDouble(this, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   444
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   445
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   446
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   447
     * Returns a pseudorandomly chosen {@code double} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   448
     * specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   449
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   450
     * @param origin the least value that can be returned
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   451
     * @param bound the upper bound (exclusive) for the returned value
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   452
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   453
     * @return a pseudorandomly chosen {@code double} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   454
     *         origin (inclusive) and the bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   455
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   456
     * @throws IllegalArgumentException unless {@code origin} is finite,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   457
     *         {@code bound} is finite, and {@code origin} is less than
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   458
     *         {@code bound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   459
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   460
     * @implNote The default implementation simply calls
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   461
     *           {@code RNGSupport.checkRange(origin, bound)} and then
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   462
     *           {@code RNGSupport.boundedNextDouble(this, origin, bound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   463
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   464
    default double nextDouble(double origin, double bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   465
        RNGSupport.checkRange(origin, bound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   466
        return RNGSupport.boundedNextDouble(this, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   467
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   468
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   469
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   470
     * Returns a pseudorandomly chosen {@code int} value.
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   471
     * <p>
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   472
     * The default implementation uses the 32 high-order bits from a call to {@code nextLong()}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   473
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   474
     * @return a pseudorandomly chosen {@code int} value
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   475
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   476
    default public int nextInt() {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   477
        return (int)(nextLong() >>> 32);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   478
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   479
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   480
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   481
     * Returns a pseudorandomly chosen {@code int} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   482
     * zero (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   483
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   484
     * @param bound the upper bound (exclusive) for the returned value. Must be positive.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   485
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   486
     * @return a pseudorandomly chosen {@code int} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   487
     *         zero (inclusive) and the bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   488
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   489
     * @throws IllegalArgumentException if {@code bound} is not positive
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   490
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   491
     * @implNote The default implementation simply calls
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   492
     *           {@code RNGSupport.checkBound(bound)} and then
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   493
     *           {@code RNGSupport.boundedNextInt(this, bound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   494
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   495
    default int nextInt(int bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   496
        RNGSupport.checkBound(bound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   497
        return RNGSupport.boundedNextInt(this, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   498
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   499
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   500
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   501
     * Returns a pseudorandomly chosen {@code int} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   502
     * specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   503
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   504
     * @param origin the least value that can be returned
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   505
     * @param bound the upper bound (exclusive) for the returned value
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   506
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   507
     * @return a pseudorandomly chosen {@code int} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   508
     *         origin (inclusive) and the bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   509
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   510
     * @throws IllegalArgumentException if {@code origin} is greater than
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   511
     *         or equal to {@code bound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   512
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   513
     * @implNote The default implementation simply calls
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   514
     *           {@code RNGSupport.checkRange(origin, bound)} and then
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   515
     *           {@code RNGSupport.boundedNextInt(this, origin, bound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   516
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   517
    default int nextInt(int origin, int bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   518
        RNGSupport.checkRange(origin, bound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   519
        return RNGSupport.boundedNextInt(this, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   520
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   521
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   522
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   523
     * Returns a pseudorandomly chosen {@code long} value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   524
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   525
     * @return a pseudorandomly chosen {@code long} value
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   526
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   527
    long nextLong();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   528
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   529
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   530
     * Returns a pseudorandomly chosen {@code long} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   531
     * zero (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   532
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   533
     * @param bound the upper bound (exclusive) for the returned value.  Must be positive.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   534
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   535
     * @return a pseudorandomly chosen {@code long} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   536
     *         zero (inclusive) and the bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   537
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   538
     * @throws IllegalArgumentException if {@code bound} is not positive
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   539
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   540
     * @implNote The default implementation simply calls
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   541
     *           {@code RNGSupport.checkBound(bound)} and then
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   542
     *           {@code RNGSupport.boundedNextLong(this, bound)}.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   543
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   544
    default long nextLong(long bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   545
        RNGSupport.checkBound(bound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   546
        return RNGSupport.boundedNextLong(this, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   547
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   548
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   549
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   550
     * Returns a pseudorandomly chosen {@code long} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   551
     * specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   552
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   553
     * @param origin the least value that can be returned
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   554
     * @param bound the upper bound (exclusive) for the returned value
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   555
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   556
     * @return a pseudorandomly chosen {@code long} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   557
     *         origin (inclusive) and the bound (exclusive)
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   558
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   559
     * @throws IllegalArgumentException if {@code origin} is greater than
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   560
     *         or equal to {@code bound}
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   561
     *
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   562
     * @implNote The default implementation simply calls
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   563
     *           {@code RNGSupport.checkRange(origin, bound)} and then
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   564
     *           {@code RNGSupport.boundedNextInt(this, origin, bound)}.
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   565
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   566
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   567
    default long nextLong(long origin, long bound) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   568
        RNGSupport.checkRange(origin, bound);
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   569
        return RNGSupport.boundedNextLong(this, origin, bound);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   570
    }
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   571
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   572
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   573
     * Returns a {@code double} value pseudorandomly chosen from
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   574
     * a Gaussian (normal) distribution whose mean is 0 and whose
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   575
     * standard deviation is 1.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   576
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   577
     * @return a {@code double} value pseudorandomly chosen from a
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   578
     *         Gaussian distribution
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   579
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   580
    default double nextGaussian() {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   581
        return RNGSupport.computeNextGaussian(this);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   582
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   583
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   584
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   585
     * Returns a {@code double} value pseudorandomly chosen from
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   586
     * a Gaussian (normal) distribution with a mean and
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   587
     * standard deviation specified by the arguments.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   588
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   589
     * @param mean the mean of the Gaussian distribution to be drawn from
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   590
     * @param stddev the standard deviation (square root of the variance)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   591
     *        of the Gaussian distribution to be drawn from
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   592
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   593
     * @return a {@code double} value pseudorandomly chosen from the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   594
     *         specified Gaussian distribution
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   595
     *
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   596
     * @throws IllegalArgumentException if {@code stddev} is negative
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   597
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   598
    default double nextGaussian(double mean, double stddev) {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   599
        if (stddev < 0.0) throw new IllegalArgumentException("standard deviation must be non-negative");
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   600
        return mean + stddev * RNGSupport.computeNextGaussian(this);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   601
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   602
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   603
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   604
     * Returns a nonnegative {@code double} value pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   605
     * from an exponential distribution whose mean is 1.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   606
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   607
     * @return a nonnegative {@code double} value pseudorandomly chosen from an
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   608
     *         exponential distribution
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   609
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   610
    default double nextExponential() {
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   611
        return RNGSupport.computeNextExponential(this);
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   612
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   613
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   614
    /**
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   615
     * Returns the period of this {@link RandomNumberGenerator} object.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   616
     *
57437
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   617
     * @return a {@link BigInteger} whose value is the number of distinct possible states of this
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   618
     *         {@link RandomNumberGenerator} object, or 0 if unknown, or negative if extremely
f02ffcb61dce Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents: 57388
diff changeset
   619
     *         large.
57388
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   620
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   621
    BigInteger period();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   622
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   623
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   624
     * The value (0) returned by the {@code period()} method if the period is unknown.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   625
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   626
    static final BigInteger UNKNOWN_PERIOD = BigInteger.ZERO;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   627
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   628
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   629
     * The (negative) value returned by the {@code period()} method if this generator
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   630
     * has no period because it is truly random rather than just pseudorandom.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   631
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   632
    static final BigInteger TRULY_RANDOM = BigInteger.valueOf(-1);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   633
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   634
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   635
     * The (negative) value that may be returned by the {@code period()} method
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   636
     * if this generator has a huge period (larger than 2**(2**16)).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   637
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   638
    static final BigInteger HUGE_PERIOD = BigInteger.valueOf(-2);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   639
}