src/java.base/share/classes/java/util/Rng.java
author briangoetz
Tue, 04 Jun 2019 13:07:35 -0400
branchJDK-8193209-branch
changeset 57388 b1e6bc96af3d
permissions -rw-r--r--
Initial commit of new RNG code from GLS
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.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     3
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     4
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     5
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     6
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     7
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     8
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
     9
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    10
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    11
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    12
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    13
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    14
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    15
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    16
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    17
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    18
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    19
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    20
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    21
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    22
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    23
 *
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
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    26
package java.util;
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
/**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    34
 * The {@code Rng} interface is designed to provide a common protocol
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    35
 * for objects that generate random or (more typically) pseudorandom
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    36
 * sequences of numbers (or Boolean values).  Such a sequence may be
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    37
 * obtained by either repeatedly invoking a method that returns a
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    38
 * single (pseudo)randomly chosen value, or by invoking a method that
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    39
 * returns a stream of (pseudo)randomly chosen values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    40
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    41
 * <p>Ideally, given an implicitly or explicitly specified range of values,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    42
 * each value would be chosen independently and uniformly from that range.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    43
 * In practice, one may have to settle for some approximation to independence
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    44
 * and uniformity.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    45
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    46
 * <p>In the case of {@code int}, {@code long}, and {@code Boolean}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    47
 * values, if there is no explicit specification of range, then the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    48
 * range includes all possible values of the type.  In the case of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    49
 * {@code float} and {@code double} values, a value is always chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    50
 * from the set of 2<sup><i>w</i></sup> values between 0.0 (inclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    51
 * and 1.0 (exclusive), where <i>w</i> is 23 for {@code float}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    52
 * values and 52 for {@code double} values, such that adjacent values
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    53
 * differ by 2<sup>&minus;<i>w</i></sup>; if an explicit range is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    54
 * specified, then the chosen number is computationally scaled and
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    55
 * translated so as to appear to have been chosen from that range.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    56
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    57
 * <p>Each method that returns a stream produces a stream of values each of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    58
 * which is chosen in the same manner as for a method that
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    59
 * returns a single (pseudo)randomly chosen value.  For example, if {@code r}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    60
 * implements {@code Rng}, then the method call {@code r.ints(100)} returns
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    61
 * a stream of 100 {@code int} values.  These are not necessarily the exact
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    62
 * same values that would have been returned if instead {@code r.nextInt()}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    63
 * had been called 100 times; all that is guaranteed is that each value in
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    64
 * the stream is chosen in a similar (pseudo)random manner from the same range.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    65
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    66
 * <p>Every object that implements the {@code Rng} interface is assumed
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    67
 * to contain a finite amount of state.  Using such an object to
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    68
 * generate a pseudorandomly chosen value alters its state.  The
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    69
 * number of distinct possible states of such an object is called its
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    70
 * <i>period</i>.  (Some implementations of the {@code Rng} interface
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    71
 * may be truly random rather than pseudorandom, for example relying
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    72
 * on the statistical behavior of a physical object to derive chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    73
 * values.  Such implementations do not have a fixed period.)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    74
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    75
 * <p>As a rule, objects that implement the {@code Rng} interface need not
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    76
 * be thread-safe.  It is recommended that multithreaded applications
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    77
 * use either {@code ThreadLocalRandom} or (preferably) pseudorandom
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    78
 * number generators that implement the {@code SplittableRng} or
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    79
 * {@code JumpableRng} interface.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    80
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    81
 * To implement this interface, a class only needs to provide concrete
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    82
 * definitions for the methods {@code nextLong()} and {@code period()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    83
 * Default implementations are provided for all other methods
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    84
 * (but it may be desirable to override some of them, especially
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    85
 * {@code nextInt()} if the underlying algorithm is {@code int}-based).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    86
 * Moerover, it may be preferable instead to implement another interface
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    87
 * such as {@link java.util.JumpableRng} or {@link java.util.LeapableRng},
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    88
 * or to extend an abstract class such as {@link java.util.AbstractSplittableRng}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    89
 * or {@link java.util.AbstractArbitrarilyJumpableRng}.
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
 * <p>Objects that implement {@code java.util.Rng} are typically
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    92
 * not cryptographically secure.  Consider instead using
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    93
 * {@link java.security.SecureRandom} to get a cryptographically
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    94
 * secure pseudorandom number generator for use by
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    95
 * security-sensitive applications.  Note, however, that
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    96
 * {@code java.security.SecureRandom} does implement the {@code Rng}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    97
 * interface, so that instances of {@code java.security.SecureRandom}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    98
 * may be used interchangeably with other types of pseudorandom
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
    99
 * generators in applications that do not require a secure generator.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   100
 *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   101
 * @author  Guy Steele
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   102
 * @since   1.9
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   103
 */
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
public interface Rng {
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   108
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   109
     * {@code double} values.
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
     * @implNote It is permitted to implement this method in a manner
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   112
     * equivalent to {@code doubles(Long.MAX_VALUE)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   113
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   114
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   115
     * that repeatedly calls {@code nextDouble()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   116
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   117
     * @return a stream of pseudorandomly chosen {@code double} values
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   118
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   119
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   120
    default DoubleStream doubles() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   121
        return DoubleStream.generate(this::nextDouble).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   122
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   123
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   124
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   125
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   126
     * {@code double} values, where each value is between the specified
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   127
     * origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   128
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   129
     * @implNote It is permitted to implement this method in a manner
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   130
     *           equivalent to 
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   131
     * {@code doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   132
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   133
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   134
     * that repeatedly calls {@code nextDouble(randomNumberOrigin, randomNumberBound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   135
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   136
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   137
     * @param randomNumberBound the upper bound (exclusive) for each value produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   138
     * @return a stream of pseudorandomly chosen {@code double} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   139
     *         the specified origin (inclusive) and the specified bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   140
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   141
     *         is greater than or equal to {@code randomNumberBound}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   142
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   143
    default DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   144
	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   145
        return DoubleStream.generate(() -> nextDouble(randomNumberOrigin, randomNumberBound)).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   146
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   147
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
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   150
     * pseudorandomly chosen {@code double} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   151
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   152
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   153
     * that repeatedly calls {@code nextDouble()}.
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
     * @return a stream of pseudorandomly chosen {@code double} values
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   157
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   158
     *         less than zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   159
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   160
    default DoubleStream doubles(long streamSize) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   161
	RngSupport.checkStreamSize(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   162
	return doubles().limit(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   163
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   164
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   165
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   166
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   167
     * pseudorandomly chosen {@code double} values, where each value is between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   168
     * the specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   169
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   170
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   171
     * that repeatedly calls {@code nextDouble(randomNumberOrigin, randomNumberBound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   172
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   173
     * @param streamSize the number of values to generate
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   174
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   175
     * @param randomNumberBound the upper bound (exclusive) for each value produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   176
     * @return a stream of pseudorandomly chosen {@code double} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   177
     *         the specified origin (inclusive) and the specified bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   178
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   179
     *         less than zero, or {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   180
     *         is greater than or equal to {@code randomNumberBound}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   181
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   182
    default DoubleStream doubles(long streamSize, double randomNumberOrigin,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   183
			  double randomNumberBound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   184
	RngSupport.checkStreamSize(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   185
	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   186
	return doubles(randomNumberOrigin, randomNumberBound).limit(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   187
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   188
   
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
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   191
     * {@code int} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   192
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   193
     * @implNote It is permitted to implement this method in a manner
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   194
     * equivalent to {@code ints(Long.MAX_VALUE)}.
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
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   197
     * that repeatedly calls {@code nextInt()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   198
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   199
     * @return a stream of pseudorandomly chosen {@code int} values
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   200
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   201
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   202
    default IntStream ints() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   203
        return IntStream.generate(this::nextInt).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   204
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   205
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   206
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   207
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   208
     * {@code int} values, where each value is between the specified
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   209
     * origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   210
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   211
     * @implNote It is permitted to implement this method in a manner
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   212
     *           equivalent to 
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   213
     * {@code ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
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
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   216
     * that repeatedly calls {@code nextInt(randomNumberOrigin, randomNumberBound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   217
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   218
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   219
     * @param randomNumberBound the upper bound (exclusive) for each value produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   220
     * @return a stream of pseudorandomly chosen {@code int} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   221
     *         the specified origin (inclusive) and the specified bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   222
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   223
     *         is greater than or equal to {@code randomNumberBound}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   224
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   225
    default IntStream ints(int randomNumberOrigin, int randomNumberBound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   226
	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   227
        return IntStream.generate(() -> nextInt(randomNumberOrigin, randomNumberBound)).sequential();
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
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   230
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   231
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   232
     * pseudorandomly chosen {@code int} values.
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
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   235
     * that repeatedly calls {@code nextInt()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   236
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   237
     * @param streamSize the number of values to generate
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   238
     * @return a stream of pseudorandomly chosen {@code int} values
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   239
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   240
     *         less than zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   241
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   242
    default IntStream ints(long streamSize) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   243
	RngSupport.checkStreamSize(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   244
	return ints().limit(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   245
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   246
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   247
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   248
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   249
     * pseudorandomly chosen {@code int} values, where each value is between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   250
     * the specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   251
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   252
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   253
     * that repeatedly calls {@code nextInt(randomNumberOrigin, randomNumberBound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   254
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   255
     * @param streamSize the number of values to generate
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   256
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   257
     * @param randomNumberBound the upper bound (exclusive) for each value produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   258
     * @return a stream of pseudorandomly chosen {@code int} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   259
     *         the specified origin (inclusive) and the specified bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   260
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   261
     *         less than zero, or {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   262
     *         is greater than or equal to {@code randomNumberBound}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   263
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   264
    default IntStream ints(long streamSize, int randomNumberOrigin,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   265
			  int randomNumberBound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   266
	RngSupport.checkStreamSize(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   267
	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   268
	return ints(randomNumberOrigin, randomNumberBound).limit(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   269
    }
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   272
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   273
     * {@code long} values.
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
     * @implNote It is permitted to implement this method in a manner
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   276
     * equivalent to {@code longs(Long.MAX_VALUE)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   277
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   278
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   279
     * that repeatedly calls {@code nextLong()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   280
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   281
     * @return a stream of pseudorandomly chosen {@code long} values
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   282
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   283
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   284
    default LongStream longs() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   285
        return LongStream.generate(this::nextLong).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   286
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   287
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   288
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   289
     * Returns an effectively unlimited stream of pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   290
     * {@code long} values, where each value is between the specified
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   291
     * origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   292
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   293
     * @implNote It is permitted to implement this method in a manner
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   294
     *           equivalent to 
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   295
     * {@code longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   296
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   297
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   298
     * that repeatedly calls {@code nextLong(randomNumberOrigin, randomNumberBound)}.
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
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   301
     * @param randomNumberBound the upper bound (exclusive) for each value produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   302
     * @return a stream of pseudorandomly chosen {@code long} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   303
     *         the specified origin (inclusive) and the specified bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   304
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   305
     *         is greater than or equal to {@code randomNumberBound}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   306
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   307
    default LongStream longs(long randomNumberOrigin, long randomNumberBound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   308
	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   309
        return LongStream.generate(() -> nextLong(randomNumberOrigin, randomNumberBound)).sequential();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   310
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   311
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
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   314
     * pseudorandomly chosen {@code long} values.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   315
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   316
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   317
     * that repeatedly calls {@code nextLong()}.
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
     * @param streamSize the number of values to generate
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   320
     * @return a stream of pseudorandomly chosen {@code long} values
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   321
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   322
     *         less than zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   323
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   324
    default LongStream longs(long streamSize) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   325
	RngSupport.checkStreamSize(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   326
	return longs().limit(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   327
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   328
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   329
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   330
     * Returns a stream producing the given {@code streamSize} number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   331
     * pseudorandomly chosen {@code long} values, where each value is between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   332
     * the specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   333
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   334
     * @implNote The default implementation produces a sequential stream
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   335
     * that repeatedly calls {@code nextLong(randomNumberOrigin, randomNumberBound)}.
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
     * @param streamSize the number of values to generate
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   338
     * @param randomNumberOrigin the least value that can be produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   339
     * @param randomNumberBound the upper bound (exclusive) for each value produced
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   340
     * @return a stream of pseudorandomly chosen {@code long} values, each between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   341
     *         the specified origin (inclusive) and the specified bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   342
     * @throws IllegalArgumentException if {@code streamSize} is
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   343
     *         less than zero, or {@code randomNumberOrigin}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   344
     *         is greater than or equal to {@code randomNumberBound}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   345
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   346
    default LongStream longs(long streamSize, long randomNumberOrigin,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   347
			  long randomNumberBound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   348
	RngSupport.checkStreamSize(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   349
	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   350
	return longs(randomNumberOrigin, randomNumberBound).limit(streamSize);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   351
    }
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   354
     * Returns a pseudorandomly chosen {@code boolean} value.
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
     * <p>The default implementation tests the high-order bit (sign bit)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   357
     * of a value produced by {@code nextInt()}, on the grounds
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   358
     * that some algorithms for pseudorandom number generation
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   359
     * produce values whose high-order bits have better
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   360
     * statistical quality than the low-order bits.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   361
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   362
     * @return a pseudorandomly chosen {@code boolean} value
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 boolean nextBoolean() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   365
        return nextInt() < 0;
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 pseudorandom {@code float} value between zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   370
     * (inclusive) and one (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
     * The default implementation uses the 24 high-order bits
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   373
     * from a call to {@code nextInt()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   374
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   375
     * @return a pseudorandom {@code float} value between zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   376
     *         (inclusive) and one (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   377
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   378
    default float nextFloat() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   379
        return (nextInt() >>> 8) * 0x1.0p-24f;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   380
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   381
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   382
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   383
     * Returns a pseudorandomly chosen {@code float} value between zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   384
     * (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   385
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   386
     * @implNote The default implementation simply calls
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   387
     *     {@code RngSupport.checkBound(bound)} and then
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   388
     *     {@code RngSupport.boundedNextFloat(this, bound)}.
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
     * @param bound the upper bound (exclusive) for the returned value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   391
     *        Must be positive and finite
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   392
     * @return a pseudorandomly chosen {@code float} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   393
     *         zero (inclusive) and the bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   394
     * @throws IllegalArgumentException if {@code bound} is not
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   395
     *         positive and finite
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   396
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   397
    default float nextFloat(float bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   398
	RngSupport.checkBound(bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   399
	return RngSupport.boundedNextFloat(this, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   400
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   401
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   402
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   403
     * Returns a pseudorandomly chosen {@code float} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   404
     * specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   405
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   406
     * @implNote The default implementation simply calls
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   407
     *     {@code RngSupport.checkRange(origin, bound)} and then
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   408
     *     {@code RngSupport.boundedNextFloat(this, origin, bound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   409
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   410
     * @param origin the least value that can be returned
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   411
     * @param bound the upper bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   412
     * @return a pseudorandomly chosen {@code float} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   413
     *         origin (inclusive) and the bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   414
     * @throws IllegalArgumentException unless {@code origin} is finite,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   415
     *         {@code bound} is finite, and {@code origin} is less than
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   416
     *         {@code bound}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   417
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   418
    default float nextFloat(float origin, float bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   419
	RngSupport.checkRange(origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   420
        return RngSupport.boundedNextFloat(this, origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   421
    }
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
     * Returns a pseudorandom {@code double} value between zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   425
     * (inclusive) and one (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   426
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   427
     * The default implementation uses the 53 high-order bits
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   428
     * from a call to {@code nextLong()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   429
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   430
     * @return a pseudorandom {@code double} value between zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   431
     *         (inclusive) and one (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   432
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   433
    default double nextDouble() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   434
        return (nextLong() >>> 11) * 0x1.0p-53;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   435
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   436
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   437
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   438
     * Returns a pseudorandomly chosen {@code double} value between zero
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   439
     * (inclusive) and the specified bound (exclusive).
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
     * @implNote The default implementation simply calls
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   442
     *     {@code RngSupport.checkBound(bound)} and then
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   443
     *     {@code RngSupport.boundedNextDouble(this, bound)}.
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
     * @param bound the upper bound (exclusive) for the returned value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   446
     *        Must be positive and finite
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   447
     * @return a pseudorandomly chosen {@code double} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   448
     *         zero (inclusive) and the bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   449
     * @throws IllegalArgumentException if {@code bound} is not
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   450
     *         positive and finite
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   451
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   452
    default double nextDouble(double bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   453
	RngSupport.checkBound(bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   454
	return RngSupport.boundedNextDouble(this, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   455
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   456
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   457
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   458
     * Returns a pseudorandomly chosen {@code double} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   459
     * specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   460
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   461
     * @implNote The default implementation simply calls
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   462
     *     {@code RngSupport.checkRange(origin, bound)} and then
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   463
     *     {@code RngSupport.boundedNextDouble(this, origin, bound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   464
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   465
     * @param origin the least value that can be returned
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   466
     * @param bound the upper bound (exclusive) for the returned value
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   467
     * @return a pseudorandomly chosen {@code double} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   468
     *         origin (inclusive) and the bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   469
     * @throws IllegalArgumentException unless {@code origin} is finite,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   470
     *         {@code bound} is finite, and {@code origin} is less than
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   471
     *         {@code bound}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   472
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   473
    default double nextDouble(double origin, double bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   474
	RngSupport.checkRange(origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   475
        return RngSupport.boundedNextDouble(this, origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   476
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   477
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
     * Returns a pseudorandomly chosen {@code int} value.
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
     * The default implementation uses the 32 high-order bits
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   482
     * from a call to {@code nextLong()}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   483
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   484
     * @return a pseudorandomly chosen {@code int} value
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
    default public int nextInt() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   487
	return (int)(nextLong() >>> 32);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   488
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   489
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   490
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   491
     * Returns a pseudorandomly chosen {@code int} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   492
     * zero (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   493
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   494
     * @implNote The default implementation simply calls
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   495
     *     {@code RngSupport.checkBound(bound)} and then
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   496
     *     {@code RngSupport.boundedNextInt(this, bound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   497
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   498
     * @param bound the upper bound (exclusive) for the returned value.  Must be positive.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   499
     * @return a pseudorandomly chosen {@code int} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   500
     *         zero (inclusive) and the bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   501
     * @throws IllegalArgumentException if {@code bound} is not positive
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   502
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   503
    default int nextInt(int bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   504
	RngSupport.checkBound(bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   505
	return RngSupport.boundedNextInt(this, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   506
    }
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   507
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   508
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   509
     * Returns a pseudorandomly chosen {@code int} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   510
     * specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   511
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   512
     * @implNote The default implementation simply calls
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   513
     *     {@code RngSupport.checkRange(origin, bound)} and then
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   514
     *     {@code RngSupport.boundedNextInt(this, origin, bound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   515
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   516
     * @param origin the least value that can be returned
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   517
     * @param bound the upper bound (exclusive) for the returned value
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   518
     * @return a pseudorandomly chosen {@code int} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   519
     *         origin (inclusive) and the bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   520
     * @throws IllegalArgumentException if {@code origin} is greater than
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   521
     *         or equal to {@code bound}
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
    default int nextInt(int origin, int bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   524
	RngSupport.checkRange(origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   525
        return RngSupport.boundedNextInt(this, origin, bound);
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
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
     * Returns a pseudorandomly chosen {@code long} value.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   530
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   531
     * @return a pseudorandomly chosen {@code long} value
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   532
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   533
    long nextLong();
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   536
     * Returns a pseudorandomly chosen {@code long} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   537
     * zero (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   538
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   539
     * @implNote The default implementation simply calls
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   540
     *     {@code RngSupport.checkBound(bound)} and then
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   541
     *     {@code RngSupport.boundedNextLong(this, bound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   542
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   543
     * @param bound the upper bound (exclusive) for the returned value.  Must be positive.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   544
     * @return a pseudorandomly chosen {@code long} value between
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   545
     *         zero (inclusive) and the bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   546
     * @throws IllegalArgumentException if {@code bound} is not positive
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
    default long nextLong(long bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   549
	RngSupport.checkBound(bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   550
	return RngSupport.boundedNextLong(this, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   551
    }
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   554
     * Returns a pseudorandomly chosen {@code long} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   555
     * specified origin (inclusive) and the specified bound (exclusive).
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   556
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   557
     * @implNote The default implementation simply calls
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   558
     *     {@code RngSupport.checkRange(origin, bound)} and then
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   559
     *     {@code RngSupport.boundedNextInt(this, origin, bound)}.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   560
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   561
     * @param origin the least value that can be returned
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   562
     * @param bound the upper bound (exclusive) for the returned value
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   563
     * @return a pseudorandomly chosen {@code long} value between the
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   564
     *         origin (inclusive) and the bound (exclusive)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   565
     * @throws IllegalArgumentException if {@code origin} is greater than
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   566
     *         or equal to {@code bound}
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   567
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   568
    default long nextLong(long origin, long bound) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   569
	RngSupport.checkRange(origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   570
        return RngSupport.boundedNextLong(this, origin, bound);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   571
    }
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   574
     * Returns a {@code double} value pseudorandomly chosen from
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   575
     * a Gaussian (normal) distribution whose mean is 0 and whose
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   576
     * standard deviation is 1.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   577
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   578
     * @return a {@code double} value pseudorandomly chosen from a
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   579
     *         Gaussian distribution
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   580
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   581
    default double nextGaussian() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   582
	return RngSupport.computeNextGaussian(this);
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
    /**
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   586
     * Returns a {@code double} value pseudorandomly chosen from
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   587
     * a Gaussian (normal) distribution with a mean and
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   588
     * standard deviation specified by the arguments.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   589
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   590
     * @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
   591
     * @param stddev the standard deviation (square root of the variance)
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   592
     *        of the Gaussian distribution to be drawn from
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
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   595
     * @throws IllegalArgumentException if {@code stddev} is negative
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   596
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   597
    default double nextGaussian(double mean, double stddev) {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   598
	if (stddev < 0.0) throw new IllegalArgumentException("standard deviation must be non-negative");
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   599
	return mean + stddev * RngSupport.computeNextGaussian(this);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   600
    }
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
     * Returns a nonnegative {@code double} value pseudorandomly chosen
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   604
     * from an exponential distribution whose mean is 1.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   605
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   606
     * @return a nonnegative {@code double} value pseudorandomly chosen from an
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   607
     *         exponential distribution
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   608
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   609
    default double nextExponential() {
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   610
	return RngSupport.computeNextExponential(this);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   611
    }
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
     * Returns the period of this {@code Rng} object.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   615
     *
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   616
     * @return a {@code BigInteger} whose value is the number of
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   617
     *         distinct possible states of this {@code Rng} object,
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   618
     *         or 0 if unknown, or negative if extremely large.
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   619
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   620
    BigInteger period();
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   621
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
     * 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
   624
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   625
    static final BigInteger UNKNOWN_PERIOD = BigInteger.ZERO;
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   626
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
     * 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
   629
     * 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
   630
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   631
    static final BigInteger TRULY_RANDOM = BigInteger.valueOf(-1);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   632
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
     * 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
   635
     * 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
   636
     */
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   637
    static final BigInteger HUGE_PERIOD = BigInteger.valueOf(-2);
b1e6bc96af3d Initial commit of new RNG code from GLS
briangoetz
parents:
diff changeset
   638
}