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