src/java.base/share/classes/java/util/random/RandomGenerator.java
author jlaskey
Thu, 14 Nov 2019 08:54:56 -0400
branchJDK-8193209-branch
changeset 59080 1b314be4feb2
parent 57940 7e791393cc4d
permissions -rw-r--r--
[mq]: latest
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     1
/*
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     4
 *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    10
 *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    16
 *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    20
 *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    23
 * questions.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    24
 */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    25
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    26
package java.util.random;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    27
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    28
import java.math.BigInteger;
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
    29
import java.util.Objects;
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    30
import java.util.stream.DoubleStream;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    31
import java.util.stream.IntStream;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    32
import java.util.stream.LongStream;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    33
import java.util.stream.Stream;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    34
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    35
/**
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    36
 * The {@link RandomGenerator} interface is designed to provide a common protocol for objects that
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    37
 * generate random or (more typically) pseudorandom sequences of numbers (or Boolean values).
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    38
 * Such a sequence may be obtained by either repeatedly invoking a method that returns a single
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    39
 * (pseudo)randomly chosen value, or by invoking a method that returns a stream of (pseudo)randomly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    40
 * chosen values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    41
 * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    42
 * Ideally, given an implicitly or explicitly specified range of values, each value would be chosen
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    43
 * independently and uniformly from that range. In practice, one may have to settle for some
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    44
 * approximation to independence and uniformity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    45
 * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    46
 * In the case of {@code int}, {@code long}, and {@link Boolean} values, if there is no explicit
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    47
 * specification of range, then the range includes all possible values of the type.  In the case of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    48
 * {@code float} and {@code double} values, a value is always chosen from the set of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    49
 * 2<sup><i>w</i></sup> values between 0.0 (inclusive) and 1.0 (exclusive), where <i>w</i> is 23 for
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    50
 * {@code float} values and 52 for {@code double} values, such that adjacent values differ by
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    51
 * 2<sup>&minus;<i>w</i></sup>; if an explicit range is specified, then the chosen number is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    52
 * computationally scaled and translated so as to appear to have been chosen from that range.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    53
 * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    54
 * Each method that returns a stream produces a stream of values each of which is chosen in the same
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    55
 * manner as for a method that returns a single (pseudo)randomly chosen value.  For example, if
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    56
 * {@code r} implements {@link RandomGenerator}, then the method call {@code r.ints(100)} returns a
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    57
 * stream of 100 {@code int} values.  These are not necessarily the exact same values that would
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    58
 * have been returned if instead {@code r.nextInt()} had been called 100 times; all that is
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    59
 * guaranteed is that each value in the stream is chosen in a similar (pseudo)random manner from the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    60
 * same range.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    61
 * <p>
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    62
 * Every object that implements the {@link RandomNumberGenerator} interface by using a
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    63
 * pseudorandom algorithm is assumed to contain a finite amount of state.  Using such an object to
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    64
 * generate a pseudorandomly chosen value alters its state by computing a new state as a function
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    65
 * of the current state, without reference to any information other than the current state.
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    66
 * The number of distinct possible states of such an object is called its <i>period</i>.
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    67
 * (Some implementations of the {@link RandomNumberGenerator} interface may be truly random
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    68
 * rather than pseudorandom, for example relying on the statistical behavior of a physical
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    69
 * object to derive chosen values.  Such implementations do not have a fixed period.)
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    70
 * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    71
 * As a rule, objects that implement the {@link RandomGenerator} interface need not be thread-safe.
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    72
 * It is recommended that multithreaded applications use either {@link ThreadLocalRandom} or
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    73
 * (preferably) pseudorandom number generators that implement the {@link SplittableGenerator} or
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    74
 * {@link JumpableGenerator} interface.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    75
 * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    76
 * To implement this interface, a class only needs to provide concrete definitions for the methods
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    77
 * {@code nextLong()} and {@code period()}. Default implementations are provided for all other
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    78
 * methods (but it may be desirable to override some of them, especially {@code nextInt()} if the
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    79
 * underlying algorithm is {@code int}-based). Moreover, it may be preferable instead to implement
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    80
 * a more specialized interface such as {@link JumpableGenerator} or {@link LeapableGenerator},
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    81
 * or to extend an abstract implementation-support class such as {@link AbstractSplittableGenerator}
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    82
 * or {@link AbstractArbitrarilyJumpableGenerator}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    83
 * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    84
 * Objects that implement {@link RandomGenerator} are typically not cryptographically secure.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    85
 * Consider instead using {@link java.security.SecureRandom} to get a cryptographically secure
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    86
 * pseudorandom number generator for use by security-sensitive applications.  Note, however, that
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    87
 * {@code java.security.SecureRandom} does implement the {@link RandomGenerator} interface, so that
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    88
 * instances of {@code java.security.SecureRandom} may be used interchangeably with other types of
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
    89
 * pseudorandom generators in applications that do not require a secure generator.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    90
 *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    91
 * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    92
 */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    93
public interface RandomGenerator {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    94
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    95
    /**
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
    96
     * Supported random number Algorithms.
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
    97
     */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
    98
    public enum Algorithm {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
    99
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   100
         * L32X64MixRandom algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   101
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   102
        L32X64MixRandom("L32X64MixRandom"),
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   103
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   104
         * L64X128MixRandom algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   105
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   106
        L64X128MixRandom("L64X128MixRandom"),
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   107
        /**
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   108
         * L64X128StarStarRandom algorithm
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   109
         */
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   110
        L64X128StarStarRandom("L64X128StarStarRandom"),
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   111
        /**
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   112
         * L64X128PlusPlusRandom algorithm
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   113
         */
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   114
        L64X128PlusPlusRandom("L64X128PlusPlusRandom"),
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   115
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   116
         * L64X256MixRandom algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   117
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   118
        L64X256MixRandom("L64X256MixRandom"),
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   119
        /**
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   120
         * L64X1024MixRandom algorithm
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   121
         */
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   122
        L64X1024MixRandom("L64X1024MixRandom"),
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   123
        /**
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   124
         * L128X128MixRandom algorithm
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   125
         */
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   126
        L128X128MixRandom("L128X128MixRandom"),
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   127
        /**
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   128
         * L128X128StarStarRandom algorithm
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   129
         */
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   130
        L128X128StarStarRandom("L128X128StarStarRandom"),
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   131
        /**
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   132
         * L128X128PlusPlusRandom algorithm
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   133
         */
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   134
        L128X128PlusPlusRandom("L128X128PlusPlusRandom"),
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   135
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   136
         * L128X256MixRandom algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   137
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   138
        L128X256MixRandom("L128X256MixRandom"),
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   139
        /**
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   140
         * L128X1024MixRandom algorithm
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   141
         */
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   142
        L128X1024MixRandom("L128X1024MixRandom"),
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   143
        /**
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   144
         * MRG32k3a algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   145
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   146
        MRG32k3a("MRG32k3a"),
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   147
        /**
57940
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   148
         * Legacy Random algorithm
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   149
         */
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   150
        @Deprecated
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   151
        Random("Random"),
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   152
        /**
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   153
         * Legacy SecureRandom algorithm
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   154
         */
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   155
        @Deprecated
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   156
        SecureRandom("SecureRandom"),
7e791393cc4d imported patch legacy
jlaskey
parents: 57684
diff changeset
   157
        /**
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   158
         * Xoroshiro128PlusPlus algorithm
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   159
         */
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   160
        Xoroshiro128PlusPlus("Xoroshiro128PlusPlus"),
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   161
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   162
         * Xoroshiro128StarStar algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   163
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   164
        Xoroshiro128StarStar("Xoroshiro128StarStar"),
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   165
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   166
         * Xoshiro256StarStar algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   167
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   168
        Xoshiro256StarStar("Xoshiro256StarStar");
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   169
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   170
        private String name;
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   171
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   172
        private Algorithm(String name) {
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   173
            this.name = name;
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   174
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   175
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   176
        public String toString() {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   177
            return name;
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   178
        }
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   179
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   180
        /**
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   181
         * Returns an instance of {@link RandomGenerator} that utilizes this algorithm.
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   182
         *
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   183
         * @return An instance of {@link RandomGenerator}
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   184
         */
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   185
        public RandomGenerator instance() {
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   186
            return RandomGeneratorFactory.of(name, RandomGenerator.class);
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   187
        }
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   188
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   189
        /**
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   190
         * Returns a {@link RandomGeneratorFactory} that can produce instances
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   191
         * of {@link RandomGenerator} that utilizes this algorithm.
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   192
         *
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   193
         * @return {@link RandomGeneratorFactory} of {@link RandomGenerator}
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   194
         */
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   195
        public RandomGeneratorFactory<RandomGenerator> factory() {
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   196
            return RandomGeneratorFactory.factoryOf(name, RandomGenerator.class);
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   197
        }
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   198
    }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   199
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   200
    /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   201
     * Returns an instance of {@link RandomGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   202
     * {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   203
     *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   204
     * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   205
     *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   206
     * @return An instance of {@link RandomGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   207
     */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   208
    public static RandomGenerator of(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   209
        Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   210
        return RandomGeneratorFactory.of(name, RandomGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   211
    }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   212
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   213
    /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   214
     * Returns an instance of {@link RandomGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   215
     * specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   216
     *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   217
     * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   218
     *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   219
     * @return An instance of {@link RandomGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   220
     */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   221
    public static RandomGenerator of(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   222
        Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   223
        return RandomGeneratorFactory.of(algorithm.toString(), RandomGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   224
    }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   225
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   226
    /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   227
     * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   228
     * of {@link RandomGenerator} that utilizes the {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   229
     *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   230
     * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   231
     *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   232
     * @return {@link RandomGeneratorFactory} of {@link RandomGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   233
     */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   234
    public static RandomGeneratorFactory<RandomGenerator> factoryOf(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   235
        Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   236
        return RandomGeneratorFactory.factoryOf(name, RandomGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   237
    }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   238
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   239
    /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   240
     * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   241
     * of {@link RandomGenerator} that utilizes the specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   242
     *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   243
     * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   244
     *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   245
     * @return {@link RandomGeneratorFactory} of {@link RandomGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   246
     */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   247
    public static RandomGeneratorFactory<RandomGenerator> factoryOf(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   248
        Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   249
        return RandomGeneratorFactory.factoryOf(algorithm.toString(), RandomGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   250
    }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   251
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   252
    /**
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   253
     * Returns an effectively unlimited stream of pseudorandomly chosen
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   254
     * {@code double} values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   255
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   256
     * @return a stream of pseudorandomly chosen {@code double} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   257
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   258
     * @implNote It is permitted to implement this method in a manner
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   259
     * equivalent to {@code doubles(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   260
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   261
     * @implNote The default implementation produces a sequential stream
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   262
     *           that repeatedly calls {@code nextDouble()}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   263
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   264
    default DoubleStream doubles() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   265
        return DoubleStream.generate(this::nextDouble).sequential();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   266
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   267
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   268
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   269
     * Returns an effectively unlimited stream of pseudorandomly chosen
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   270
     * {@code double} values, where each value is between the specified
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   271
     * origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   272
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   273
     * @param randomNumberOrigin the least value that can be produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   274
     * @param randomNumberBound the upper bound (exclusive) for each value produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   275
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   276
     * @return a stream of pseudorandomly chosen {@code double} values, each between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   277
     *         the specified origin (inclusive) and the specified bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   278
     *
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   279
     * @throws IllegalArgumentException if {@code randomNumberOrigin} is not finite,
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   280
     *         or {@code randomNumberBound} is not finite, or {@code randomNumberOrigin}
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   281
     *         is greater than or equal to {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   282
     *
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   283
     * @implNote It is permitted to implement this method in a manner equivalent to
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   284
     *           {@code doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   285
     * @implNote The default implementation produces a sequential stream that repeatedly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   286
     *           calls {@code nextDouble(randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   287
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   288
    default DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   289
        RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   290
        return DoubleStream.generate(() -> nextDouble(randomNumberOrigin, randomNumberBound)).sequential();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   291
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   292
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   293
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   294
     * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   295
     * pseudorandomly chosen {@code double} values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   296
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   297
     * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   298
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   299
     * @return a stream of pseudorandomly chosen {@code double} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   300
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   301
     * @throws IllegalArgumentException if {@code streamSize} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   302
     *         less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   303
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   304
     * @implNote The default implementation produces a sequential stream
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   305
     * that repeatedly calls {@code nextDouble()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   306
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   307
    default DoubleStream doubles(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   308
        RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   309
        return doubles().limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   310
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   311
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   312
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   313
     * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   314
     * pseudorandomly chosen {@code double} values, where each value is between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   315
     * the specified origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   316
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   317
     * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   318
     * @param randomNumberOrigin the least value that can be produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   319
     * @param randomNumberBound the upper bound (exclusive) for each value produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   320
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   321
     * @return a stream of pseudorandomly chosen {@code double} values, each between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   322
     *         the specified origin (inclusive) and the specified bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   323
     *
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   324
     * @throws IllegalArgumentException if {@code streamSize} is less than zero,
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   325
     *         or {@code randomNumberOrigin} is not finite,
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   326
     *         or {@code randomNumberBound} is not finite, or {@code randomNumberOrigin}
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   327
     *         is greater than or equal to {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   328
     *
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   329
     * @implNote The default implementation produces a sequential stream that repeatedly
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   330
     *           calls {@code nextDouble(randomNumberOrigin, randomNumberBound)}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   331
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   332
    default DoubleStream doubles(long streamSize, double randomNumberOrigin,
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   333
				 double randomNumberBound) {
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   334
        RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   335
        RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   336
        return doubles(randomNumberOrigin, randomNumberBound).limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   337
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   338
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   339
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   340
     * Returns an effectively unlimited stream of pseudorandomly chosen
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   341
     * {@code int} values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   342
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   343
     * @return a stream of pseudorandomly chosen {@code int} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   344
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   345
     * @implNote It is permitted to implement this method in a manner
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   346
     *           equivalent to {@code ints(Long.MAX_VALUE)}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   347
     * @implNote The default implementation produces a sequential stream
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   348
     *           that repeatedly calls {@code nextInt()}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   349
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   350
    default IntStream ints() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   351
        return IntStream.generate(this::nextInt).sequential();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   352
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   353
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   354
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   355
     * Returns an effectively unlimited stream of pseudorandomly chosen
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   356
     * {@code int} values, where each value is between the specified
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   357
     * origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   358
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   359
     * @param randomNumberOrigin the least value that can be produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   360
     * @param randomNumberBound the upper bound (exclusive) for each value produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   361
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   362
     * @return a stream of pseudorandomly chosen {@code int} values, each between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   363
     *         the specified origin (inclusive) and the specified bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   364
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   365
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   366
     *         is greater than or equal to {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   367
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   368
     * @implNote It is permitted to implement this method in a manner equivalent to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   369
     *           {@code ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   370
     * @implNote The default implementation produces a sequential stream that repeatedly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   371
     *           calls {@code nextInt(randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   372
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   373
    default IntStream ints(int randomNumberOrigin, int randomNumberBound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   374
        RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   375
        return IntStream.generate(() -> nextInt(randomNumberOrigin, randomNumberBound)).sequential();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   376
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   377
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   378
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   379
     * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   380
     * pseudorandomly chosen {@code int} values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   381
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   382
     * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   383
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   384
     * @return a stream of pseudorandomly chosen {@code int} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   385
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   386
     * @throws IllegalArgumentException if {@code streamSize} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   387
     *         less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   388
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   389
     * @implNote The default implementation produces a sequential stream
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   390
     *           that repeatedly calls {@code nextInt()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   391
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   392
    default IntStream ints(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   393
        RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   394
        return ints().limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   395
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   396
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   397
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   398
     * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   399
     * pseudorandomly chosen {@code int} values, where each value is between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   400
     * the specified origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   401
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   402
     * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   403
     * @param randomNumberOrigin the least value that can be produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   404
     * @param randomNumberBound the upper bound (exclusive) for each value produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   405
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   406
     * @return a stream of pseudorandomly chosen {@code int} values, each between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   407
     *         the specified origin (inclusive) and the specified bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   408
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   409
     * @throws IllegalArgumentException if {@code streamSize} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   410
     *         less than zero, or {@code randomNumberOrigin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   411
     *         is greater than or equal to {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   412
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   413
     * @implNote The default implementation produces a sequential stream that repeatedly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   414
     *           calls {@code nextInt(randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   415
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   416
    default IntStream ints(long streamSize, int randomNumberOrigin,
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   417
			   int randomNumberBound) {
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   418
        RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   419
        RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   420
        return ints(randomNumberOrigin, randomNumberBound).limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   421
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   422
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   423
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   424
     * Returns an effectively unlimited stream of pseudorandomly chosen
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   425
     * {@code long} values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   426
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   427
     * @return a stream of pseudorandomly chosen {@code long} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   428
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   429
     * @implNote It is permitted to implement this method in a manner
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   430
     *           equivalent to {@code longs(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   431
     * @implNote The default implementation produces a sequential stream
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   432
     *           that repeatedly calls {@code nextLong()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   433
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   434
    default LongStream longs() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   435
        return LongStream.generate(this::nextLong).sequential();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   436
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   437
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   438
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   439
     * Returns an effectively unlimited stream of pseudorandomly chosen
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   440
     * {@code long} values, where each value is between the specified
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   441
     * origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   442
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   443
     * @param randomNumberOrigin the least value that can be produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   444
     * @param randomNumberBound the upper bound (exclusive) for each value produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   445
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   446
     * @return a stream of pseudorandomly chosen {@code long} values, each between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   447
     *         the specified origin (inclusive) and the specified bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   448
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   449
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   450
     *         is greater than or equal to {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   451
     *
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   452
     * @implNote It is permitted to implement this method in a manner equivalent to
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   453
     *           {@code longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   454
     * @implNote The default implementation produces a sequential stream that repeatedly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   455
     *           calls {@code nextLong(randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   456
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   457
    default LongStream longs(long randomNumberOrigin, long randomNumberBound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   458
        RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   459
        return LongStream.generate(() -> nextLong(randomNumberOrigin, randomNumberBound)).sequential();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   460
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   461
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   462
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   463
     * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   464
     * pseudorandomly chosen {@code long} values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   465
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   466
     * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   467
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   468
     * @return a stream of pseudorandomly chosen {@code long} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   469
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   470
     * @throws IllegalArgumentException if {@code streamSize} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   471
     *         less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   472
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   473
     * @implNote The default implementation produces a sequential stream
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   474
     * that repeatedly calls {@code nextLong()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   475
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   476
    default LongStream longs(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   477
        RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   478
        return longs().limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   479
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   480
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   481
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   482
     * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   483
     * pseudorandomly chosen {@code long} values, where each value is between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   484
     * the specified origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   485
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   486
     * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   487
     * @param randomNumberOrigin the least value that can be produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   488
     * @param randomNumberBound the upper bound (exclusive) for each value produced
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   489
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   490
     * @return a stream of pseudorandomly chosen {@code long} values, each between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   491
     *         the specified origin (inclusive) and the specified bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   492
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   493
     * @throws IllegalArgumentException if {@code streamSize} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   494
     *         less than zero, or {@code randomNumberOrigin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   495
     *         is greater than or equal to {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   496
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   497
     * @implNote The default implementation produces a sequential stream that repeatedly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   498
     *            calls {@code nextLong(randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   499
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   500
    default LongStream longs(long streamSize, long randomNumberOrigin,
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   501
			     long randomNumberBound) {
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   502
        RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   503
        RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   504
        return longs(randomNumberOrigin, randomNumberBound).limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   505
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   506
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   507
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   508
     * Returns a pseudorandomly chosen {@code boolean} value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   509
     * <p>
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   510
     * The default implementation tests the high-order bit (sign bit) of a value produced by
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   511
     * {@code nextInt()}, on the grounds that some algorithms for pseudorandom number generation
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   512
     * produce values whose high-order bits have better statistical quality than the low-order bits.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   513
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   514
     * @return a pseudorandomly chosen {@code boolean} value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   515
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   516
    default boolean nextBoolean() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   517
        return nextInt() < 0;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   518
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   519
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   520
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   521
     * Returns a pseudorandom {@code float} value between zero (inclusive) and one (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   522
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   523
     * The default implementation uses the 24 high-order bits from a call to {@code nextInt()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   524
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   525
     * @return a pseudorandom {@code float} value between zero (inclusive) and one (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   526
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   527
    default float nextFloat() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   528
        return (nextInt() >>> 8) * 0x1.0p-24f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   529
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   530
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   531
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   532
     * Returns a pseudorandomly chosen {@code float} value between zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   533
     * (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   534
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   535
     * @param bound the upper bound (exclusive) for the returned value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   536
     *        Must be positive and finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   537
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   538
     * @return a pseudorandomly chosen {@code float} value between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   539
     *         zero (inclusive) and the bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   540
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   541
     * @throws IllegalArgumentException if {@code bound} is not
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   542
     *         both positive and finite
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   543
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   544
     * @implNote The default implementation simply calls
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   545
     *           {@code RandomSupport.checkBound(bound)} and then
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   546
     *           {@code RandomSupport.boundedNextFloat(this, bound)}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   547
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   548
    default float nextFloat(float bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   549
        RandomSupport.checkBound(bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   550
        return RandomSupport.boundedNextFloat(this, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   551
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   552
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   553
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   554
     * Returns a pseudorandomly chosen {@code float} value between the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   555
     * specified origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   556
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   557
     * @param origin the least value that can be returned
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   558
     * @param bound the upper bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   559
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   560
     * @return a pseudorandomly chosen {@code float} value between the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   561
     *         origin (inclusive) and the bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   562
     *
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   563
     * @throws IllegalArgumentException if {@code origin} is not finite,
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   564
     *         or {@code bound} is not finite, or {@code origin}
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   565
     *         is greater than or equal to {@code bound}
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   566
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   567
     * @implNote The default implementation simply calls
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   568
     *           {@code RandomSupport.checkRange(origin, bound)} and then
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   569
     *           {@code RandomSupport.boundedNextFloat(this, origin, bound)}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   570
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   571
    default float nextFloat(float origin, float bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   572
        RandomSupport.checkRange(origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   573
        return RandomSupport.boundedNextFloat(this, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   574
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   575
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   576
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   577
     * Returns a pseudorandom {@code double} value between zero (inclusive) and one (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   578
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   579
     * The default implementation uses the 53 high-order bits from a call to {@code nextLong()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   580
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   581
     * @return a pseudorandom {@code double} value between zero (inclusive) and one (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   582
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   583
    default double nextDouble() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   584
        return (nextLong() >>> 11) * 0x1.0p-53;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   585
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   586
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   587
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   588
     * Returns a pseudorandomly chosen {@code double} value between zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   589
     * (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   590
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   591
     * @param bound the upper bound (exclusive) for the returned value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   592
     *        Must be positive and finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   593
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   594
     * @return a pseudorandomly chosen {@code double} value between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   595
     *         zero (inclusive) and the bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   596
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   597
     * @throws IllegalArgumentException if {@code bound} is not
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   598
     *         both positive and finite
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   599
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   600
     * @implNote The default implementation simply calls
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   601
     *           {@code RandomSupport.checkBound(bound)} and then
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   602
     *           {@code RandomSupport.boundedNextDouble(this, bound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   603
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   604
    default double nextDouble(double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   605
        RandomSupport.checkBound(bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   606
        return RandomSupport.boundedNextDouble(this, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   607
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   608
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   609
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   610
     * Returns a pseudorandomly chosen {@code double} value between the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   611
     * specified origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   612
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   613
     * @param origin the least value that can be returned
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   614
     * @param bound the upper bound (exclusive) for the returned value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   615
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   616
     * @return a pseudorandomly chosen {@code double} value between the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   617
     *         origin (inclusive) and the bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   618
     *
59080
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   619
     * @throws IllegalArgumentException if {@code origin} is not finite,
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   620
     *         or {@code bound} is not finite, or {@code origin}
1b314be4feb2 [mq]: latest
jlaskey
parents: 57940
diff changeset
   621
     *         is greater than or equal to {@code bound}
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   622
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   623
     * @implNote The default implementation simply calls
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   624
     *           {@code RandomSupport.checkRange(origin, bound)} and then
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   625
     *           {@code RandomSupport.boundedNextDouble(this, origin, bound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   626
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   627
    default double nextDouble(double origin, double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   628
        RandomSupport.checkRange(origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   629
        return RandomSupport.boundedNextDouble(this, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   630
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   631
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   632
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   633
     * Returns a pseudorandomly chosen {@code int} value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   634
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   635
     * The default implementation uses the 32 high-order bits from a call to {@code nextLong()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   636
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   637
     * @return a pseudorandomly chosen {@code int} value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   638
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   639
    default public int nextInt() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   640
        return (int)(nextLong() >>> 32);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   641
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   642
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   643
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   644
     * Returns a pseudorandomly chosen {@code int} value between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   645
     * zero (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   646
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   647
     * @param bound the upper bound (exclusive) for the returned value. Must be positive.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   648
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   649
     * @return a pseudorandomly chosen {@code int} value between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   650
     *         zero (inclusive) and the bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   651
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   652
     * @throws IllegalArgumentException if {@code bound} is not positive
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   653
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   654
     * @implNote The default implementation simply calls
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   655
     *           {@code RandomSupport.checkBound(bound)} and then
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   656
     *           {@code RandomSupport.boundedNextInt(this, bound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   657
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   658
    default int nextInt(int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   659
        RandomSupport.checkBound(bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   660
        return RandomSupport.boundedNextInt(this, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   661
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   662
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   663
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   664
     * Returns a pseudorandomly chosen {@code int} value between the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   665
     * specified origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   666
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   667
     * @param origin the least value that can be returned
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   668
     * @param bound the upper bound (exclusive) for the returned value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   669
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   670
     * @return a pseudorandomly chosen {@code int} value between the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   671
     *         origin (inclusive) and the bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   672
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   673
     * @throws IllegalArgumentException if {@code origin} is greater than
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   674
     *         or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   675
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   676
     * @implNote The default implementation simply calls
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   677
     *           {@code RandomSupport.checkRange(origin, bound)} and then
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   678
     *           {@code RandomSupport.boundedNextInt(this, origin, bound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   679
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   680
    default int nextInt(int origin, int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   681
        RandomSupport.checkRange(origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   682
        return RandomSupport.boundedNextInt(this, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   683
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   684
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   685
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   686
     * Returns a pseudorandomly chosen {@code long} value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   687
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   688
     * @return a pseudorandomly chosen {@code long} value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   689
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   690
    long nextLong();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   691
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   692
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   693
     * Returns a pseudorandomly chosen {@code long} value between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   694
     * zero (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   695
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   696
     * @param bound the upper bound (exclusive) for the returned value.  Must be positive.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   697
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   698
     * @return a pseudorandomly chosen {@code long} value between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   699
     *         zero (inclusive) and the bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   700
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   701
     * @throws IllegalArgumentException if {@code bound} is not positive
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   702
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   703
     * @implNote The default implementation simply calls
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   704
     *           {@code RandomSupport.checkBound(bound)} and then
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   705
     *           {@code RandomSupport.boundedNextLong(this, bound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   706
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   707
    default long nextLong(long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   708
        RandomSupport.checkBound(bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   709
        return RandomSupport.boundedNextLong(this, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   710
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   711
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   712
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   713
     * Returns a pseudorandomly chosen {@code long} value between the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   714
     * specified origin (inclusive) and the specified bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   715
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   716
     * @param origin the least value that can be returned
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   717
     * @param bound the upper bound (exclusive) for the returned value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   718
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   719
     * @return a pseudorandomly chosen {@code long} value between the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   720
     *         origin (inclusive) and the bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   721
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   722
     * @throws IllegalArgumentException if {@code origin} is greater than
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   723
     *         or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   724
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   725
     * @implNote The default implementation simply calls
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   726
     *           {@code RandomSupport.checkRange(origin, bound)} and then
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   727
     *           {@code RandomSupport.boundedNextInt(this, origin, bound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   728
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   729
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   730
    default long nextLong(long origin, long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   731
        RandomSupport.checkRange(origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   732
        return RandomSupport.boundedNextLong(this, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   733
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   734
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   735
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   736
     * Returns a {@code double} value pseudorandomly chosen from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   737
     * a Gaussian (normal) distribution whose mean is 0 and whose
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   738
     * standard deviation is 1.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   739
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   740
     * @return a {@code double} value pseudorandomly chosen from a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   741
     *         Gaussian distribution
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   742
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   743
    default double nextGaussian() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   744
        return RandomSupport.computeNextGaussian(this);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   745
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   746
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   747
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   748
     * Returns a {@code double} value pseudorandomly chosen from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   749
     * a Gaussian (normal) distribution with a mean and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   750
     * standard deviation specified by the arguments.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   751
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   752
     * @param mean the mean of the Gaussian distribution to be drawn from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   753
     * @param stddev the standard deviation (square root of the variance)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   754
     *        of the Gaussian distribution to be drawn from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   755
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   756
     * @return a {@code double} value pseudorandomly chosen from the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   757
     *         specified Gaussian distribution
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   758
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   759
     * @throws IllegalArgumentException if {@code stddev} is negative
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   760
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   761
    default double nextGaussian(double mean, double stddev) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   762
        if (stddev < 0.0) throw new IllegalArgumentException("standard deviation must be non-negative");
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   763
        return mean + stddev * RandomSupport.computeNextGaussian(this);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   764
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   765
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   766
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   767
     * Returns a nonnegative {@code double} value pseudorandomly chosen
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   768
     * from an exponential distribution whose mean is 1.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   769
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   770
     * @return a nonnegative {@code double} value pseudorandomly chosen from an
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   771
     *         exponential distribution
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   772
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   773
    default double nextExponential() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   774
        return RandomSupport.computeNextExponential(this);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   775
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   776
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   777
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   778
     * Returns the period of this {@link RandomGenerator} object.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   779
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   780
     * @return a {@link BigInteger} whose value is the number of distinct possible states of this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   781
     *         {@link RandomGenerator} object, or 0 if unknown, or negative if extremely
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   782
     *         large.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   783
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   784
    BigInteger period();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   785
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   786
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   787
     * The value (0) returned by the {@code period()} method if the period is unknown.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   788
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   789
    static final BigInteger UNKNOWN_PERIOD = BigInteger.ZERO;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   790
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   791
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   792
     * The (negative) value returned by the {@code period()} method if this generator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   793
     * has no period because it is truly random rather than just pseudorandom.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   794
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   795
    static final BigInteger TRULY_RANDOM = BigInteger.valueOf(-1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   796
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   797
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   798
     * The (negative) value that may be returned by the {@code period()} method
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   799
     * if this generator has a huge period (larger than 2**(2**16)).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   800
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   801
    static final BigInteger HUGE_PERIOD = BigInteger.valueOf(-2);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   802
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   803
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   804
     * The {@link StreamableGenerator} interface augments the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   805
     * to provide methods that return streams of {@link RandomGenerator} objects.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   806
     * Ideally, such a stream of objects would have the property that the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   807
     * behavior of each object is statistically independent of all the others.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   808
     * In practice, one may have to settle for some approximation to this property.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   809
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   810
     * A generator that implements interface {@link SplittableGenerator}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   811
     * may choose to use its {@code splits} method to implement the {@code rngs}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   812
     * method required by this interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   813
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   814
     * A generator that implements interface {@link JumpableGenerator}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   815
     * may choose to use its {@code jumps} method to implement the {@code rngs}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   816
     * method required by this interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   817
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   818
     * A generator that implements interface {@link LeapableGenerator}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   819
     * may choose to use its {@code leaps} method to implement the {@code rngs}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   820
     * method required by this interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   821
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   822
     * An implementation of the {@link StreamableGenerator} interface must provide
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   823
     * concrete definitions for the methods {@code nextInt()}, {@code nextLong},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   824
     * {@code period()}, and {@code rngs()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   825
     * Default implementations are provided for all other methods.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   826
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   827
     * Objects that implement {@link StreamableGenerator} are typically
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   828
     * not cryptographically secure.  Consider instead using
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   829
     * {@link java.security.SecureRandom} to get a cryptographically
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   830
     * secure pseudo-random number generator for use by
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   831
     * security-sensitive applications.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   832
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   833
     * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   834
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   835
    public interface StreamableGenerator extends RandomGenerator {
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   836
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   837
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   838
         * Returns an instance of {@link StreamableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   839
         * {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   840
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   841
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   842
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   843
         * @return An instance of {@link StreamableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   844
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   845
        public static StreamableGenerator of(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   846
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   847
            return RandomGeneratorFactory.of(name, StreamableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   848
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   849
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   850
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   851
         * Returns an instance of {@link StreamableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   852
         * specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   853
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   854
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   855
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   856
         * @return An instance of {@link StreamableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   857
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   858
        public static StreamableGenerator of(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   859
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   860
            return RandomGeneratorFactory.of(algorithm.toString(), StreamableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   861
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   862
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   863
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   864
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   865
         * of {@link StreamableGenerator} that utilizes the {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   866
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   867
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   868
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   869
         * @return {@link RandomGeneratorFactory} of {@link StreamableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   870
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   871
        public static RandomGeneratorFactory<StreamableGenerator> factoryOf(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   872
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   873
            return RandomGeneratorFactory.factoryOf(name, StreamableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   874
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   875
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   876
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   877
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   878
         * of {@link StreamableGenerator} that utilizes the specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   879
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   880
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   881
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   882
         * @return {@link RandomGeneratorFactory} of {@link StreamableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   883
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   884
        public static RandomGeneratorFactory<StreamableGenerator> factoryOf(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   885
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   886
            return RandomGeneratorFactory.factoryOf(algorithm.toString(), StreamableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   887
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   888
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   889
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   890
         * Returns an effectively unlimited stream of objects, each of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   891
         * which implements the {@link RandomGenerator} interface.  Ideally the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   892
         * generators in the stream will appear to be statistically
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   893
         * independent.  The new generators should be of the same kind
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   894
         * as this generator.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   895
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   896
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   897
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   898
         * @implNote It is permitted to implement this method in a manner
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   899
         *           equivalent to {@code rngs(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   900
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   901
        Stream<RandomGenerator> rngs();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   902
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   903
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   904
         * Returns an effectively unlimited stream of objects, each of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   905
         * which implements the {@link RandomGenerator} interface.  Ideally the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   906
         * generators in the stream will appear to be statistically
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   907
         * independent.  The new generators should be of the same kind
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   908
         * as this generator.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   909
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   910
         * @param streamSize the number of generators to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   911
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   912
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   913
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   914
         * @throws IllegalArgumentException if {@code streamSize} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   915
         *         less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   916
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   917
         * @implNote The default implementation calls {@code rngs()} and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   918
         *           then limits its length to {@code streamSize}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   919
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   920
        default Stream<RandomGenerator> rngs(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   921
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   922
            return rngs().limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   923
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   924
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   925
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   926
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   927
     * This interface is designed to provide a common protocol for objects
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   928
     * that generate sequences of pseudorandom numbers (or Boolean values)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   929
     * and furthermore can be <i>split</i> into two objects (the original
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   930
     * one and a new one) each of which obey that same protocol (and therefore
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   931
     * can be recursively split indefinitely).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   932
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   933
     * Ideally, all {@link SplittableGenerator} objects produced by recursive
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   934
     * splitting from a single original {@link SplittableGenerator} object are
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   935
     * statistically independent of one another and individually uniform.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   936
     * Therefore we would expect the set of values collectively generated
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   937
     * by a set of such objects to have the same statistical properties as
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   938
     * if the same quantity of values were generated by a single thread
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   939
     * using a single {@link SplittableGenerator} object.  In practice, one must
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   940
     * settle for some approximation to independence and uniformity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   941
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   942
     * Methods are provided to perform a single splitting operation and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   943
     * also to produce a stream of generators split off from the original
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   944
     * (by either iterative or recursive splitting, or a combination).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   945
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   946
     * An implementation of the {@link SplittableGenerator} interface must provide
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   947
     * concrete definitions for the methods {@code nextInt()}, {@code nextLong},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   948
     * {@code period()}, {@code split()}, {@code split(SplittableGenerator)},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   949
     * {@code splits()}, {@code splits(long)}, {@code splits(SplittableGenerator)},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   950
     * and {@code splits(long, SplittableGenerator)}.  Perhaps the most convenient
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   951
     * way to implement this interface is to extend the abstract class
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   952
     * {@link AbstractSplittableGenerator}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   953
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   954
     * Objects that implement {@link SplittableGenerator} are
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   955
     * typically not cryptographically secure.  Consider instead using
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   956
     * {@link java.security.SecureRandom} to get a cryptographically
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   957
     * secure pseudo-random number generator for use by
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   958
     * security-sensitive applications.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   959
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   960
     * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   961
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   962
    public interface SplittableGenerator extends StreamableGenerator {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   963
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   964
        /**
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   965
         * Returns an instance of {@link SplittableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   966
         * {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   967
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   968
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   969
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   970
         * @return An instance of {@link SplittableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   971
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   972
        public static SplittableGenerator of(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   973
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   974
            return RandomGeneratorFactory.of(name, SplittableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   975
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   976
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   977
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   978
         * Returns an instance of {@link SplittableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   979
         * specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   980
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   981
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   982
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   983
         * @return An instance of {@link SplittableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   984
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   985
        public static SplittableGenerator of(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   986
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   987
            return RandomGeneratorFactory.of(algorithm.toString(), SplittableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   988
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   989
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   990
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   991
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   992
         * of {@link SplittableGenerator} that utilizes the {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   993
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   994
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   995
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
   996
         * @return {@link RandomGeneratorFactory} of {@link SplittableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   997
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   998
        public static RandomGeneratorFactory<SplittableGenerator> factoryOf(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
   999
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1000
            return RandomGeneratorFactory.factoryOf(name, SplittableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1001
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1002
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1003
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1004
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1005
         * of {@link SplittableGenerator} that utilizes the specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1006
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1007
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1008
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1009
         * @return {@link RandomGeneratorFactory} of {@link SplittableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1010
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1011
        public static RandomGeneratorFactory<SplittableGenerator> factoryOf(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1012
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1013
            return RandomGeneratorFactory.factoryOf(algorithm.toString(), SplittableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1014
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1015
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1016
        /**
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1017
         * Returns a new pseudorandom number generator, split off from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1018
         * this one, that implements the {@link RandomGenerator} and {@link SplittableGenerator}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1019
         * interfaces.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1020
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1021
         * This pseudorandom number generator may be used as a source of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1022
         * pseudorandom bits used to initialize the state the new one.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1023
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1024
         * @return a new object that implements the {@link RandomGenerator} and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1025
         *         {@link SplittableGenerator} interfaces
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1026
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1027
        SplittableGenerator split();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1028
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1029
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1030
         * Returns a new pseudorandom number generator, split off from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1031
         * this one, that implements the {@link RandomGenerator} and {@link SplittableGenerator}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1032
         * interfaces.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1033
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1034
         * @param source a {@link SplittableGenerator} instance to be used instead
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1035
         *               of this one as a source of pseudorandom bits used to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1036
         *               initialize the state of the new ones.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1037
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1038
         * @return an object that implements the {@link RandomGenerator} and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1039
         *         {@link SplittableGenerator} interfaces
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1040
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1041
        SplittableGenerator split(SplittableGenerator source);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1042
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1043
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1044
         * Returns an effectively unlimited stream of new pseudorandom
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1045
         * number generators, each of which implements the {@link SplittableGenerator}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1046
         * interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1047
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1048
         * This pseudorandom number generator may be used as a source of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1049
         * pseudorandom bits used to initialize the state the new ones.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1050
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1051
         * @implNote It is permitted to implement this method in a manner
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1052
         * equivalent to {@code splits(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1053
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1054
         * @return a stream of {@link SplittableGenerator} objects
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1055
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1056
        default Stream<SplittableGenerator> splits() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1057
            return this.splits(this);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1058
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1059
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1060
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1061
         * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1062
         * new pseudorandom number generators, each of which implements the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1063
         * {@link SplittableGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1064
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1065
         * This pseudorandom number generator may be used as a source of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1066
         * pseudorandom bits used to initialize the state the new ones.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1067
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1068
         * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1069
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1070
         * @return a stream of {@link SplittableGenerator} objects
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1071
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1072
         * @throws IllegalArgumentException if {@code streamSize} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1073
         *         less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1074
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1075
        Stream<SplittableGenerator> splits(long streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1076
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1077
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1078
         * Returns an effectively unlimited stream of new pseudorandom
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1079
         * number generators, each of which implements the {@link SplittableGenerator}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1080
         * interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1081
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1082
         * @param source a {@link SplittableGenerator} instance to be used instead
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1083
         *               of this one as a source of pseudorandom bits used to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1084
         *               initialize the state of the new ones.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1085
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1086
         * @return a stream of {@link SplittableGenerator} objects
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1087
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1088
         * @implNote It is permitted to implement this method in a manner
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1089
         *           equivalent to {@code splits(Long.MAX_VALUE, source)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1090
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1091
        Stream<SplittableGenerator> splits(SplittableGenerator source);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1092
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1093
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1094
         * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1095
         * new pseudorandom number generators, each of which implements the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1096
         * {@link SplittableGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1097
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1098
         * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1099
         * @param source a {@link SplittableGenerator} instance to be used instead
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1100
         *               of this one as a source of pseudorandom bits used to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1101
         *               initialize the state of the new ones.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1102
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1103
         * @return a stream of {@link SplittableGenerator} objects
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1104
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1105
         * @throws IllegalArgumentException if {@code streamSize} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1106
         *         less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1107
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1108
        Stream<SplittableGenerator> splits(long streamSize, SplittableGenerator source);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1109
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1110
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1111
         * Returns an effectively unlimited stream of new pseudorandom
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1112
         * number generators, each of which implements the {@link RandomGenerator}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1113
         * interface.  Ideally the generators in the stream will appear
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1114
         * to be statistically independent.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1115
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1116
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1117
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1118
         * @implNote The default implementation calls {@code splits()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1119
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1120
        default Stream<RandomGenerator> rngs() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1121
            return this.splits().map(x -> (RandomGenerator)x);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1122
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1123
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1124
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1125
         * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1126
         * new pseudorandom number generators, each of which implements the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1127
         * {@link RandomGenerator} interface.  Ideally the generators in the stream will
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1128
         * appear to be statistically independent.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1129
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1130
         * @param streamSize the number of generators to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1131
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1132
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1133
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1134
         * @throws IllegalArgumentException if {@code streamSize} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1135
         *         less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1136
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1137
         * @implNote The default implementation calls {@code splits(streamSize)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1138
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1139
        default Stream<RandomGenerator> rngs(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1140
            return this.splits(streamSize).map(x -> (RandomGenerator)x);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1141
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1142
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1143
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1144
    /**
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1145
     * This interface is designed to provide a common protocol for objects that generate
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1146
     * pseudorandom sequences of numbers (or Boolean values) and furthermore can easily <i>jump</i>
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1147
     * forward (by a fixed amount) to a distant point in the state cycle.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1148
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1149
     * Ideally, all {@link JumpableGenerator} objects produced by iterative jumping from a single
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1150
     * original {@link JumpableGenerator} object are statistically independent of one another and
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1151
     * individually uniform. In practice, one must settle for some approximation to independence and
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1152
     * uniformity.  In particular, a specific implementation may assume that each generator in a
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1153
     * stream produced by the {@code jumps} method is used to produce a number of values no larger
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1154
     * than either 2<sup>64</sup> or the square root of its period.  Implementors are advised to use
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1155
     * algorithms whose period is at least 2<sup>127</sup>.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1156
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1157
     * Methods are provided to perform a single jump operation and also to produce a stream of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1158
     * generators produced from the original by iterative copying and jumping of internal state.  A
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1159
     * typical strategy for a multithreaded application is to create a single {@link
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1160
     * JumpableGenerator} object, calls its {@code jumps} method exactly once, and then parcel out
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1161
     * generators from the resulting stream, one to each thread.  It is generally not a good idea to
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1162
     * call {@code jump} on a generator that was itself produced by the {@code jumps} method,
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1163
     * because the result may be a generator identical to another generator already produce by that
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1164
     * call to the {@code jumps} method. For this reason, the return type of the {@code jumps}
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1165
     * method is {@code Stream<RandomGenerator>} rather than {@code Stream<JumpableGenerator>}, even
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1166
     * though the actual generator objects in that stream likely do also implement the {@link
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1167
     * JumpableGenerator} interface.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1168
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1169
     * An implementation of the {@link JumpableGenerator} interface must provide concrete
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1170
     * definitions for the methods {@code nextInt()}, {@code nextLong}, {@code period()}, {@code
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1171
     * copy()}, {@code jump()}, and {@code defaultJumpDistance()}. Default implementations are
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1172
     * provided for all other methods.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1173
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1174
     * Objects that implement {@link JumpableGenerator} are typically not cryptographically secure.
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1175
     * Consider instead using {@link java.security.SecureRandom} to get a cryptographically secure
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1176
     * pseudo-random number generator for use by security-sensitive applications.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1177
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1178
     * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1179
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1180
    public interface JumpableGenerator extends StreamableGenerator {
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1181
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1182
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1183
         * Returns an instance of {@link JumpableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1184
         * {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1185
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1186
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1187
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1188
         * @return An instance of {@link JumpableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1189
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1190
        public static JumpableGenerator of(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1191
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1192
            return RandomGeneratorFactory.of(name, JumpableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1193
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1194
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1195
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1196
         * Returns an instance of {@link JumpableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1197
         * specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1198
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1199
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1200
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1201
         * @return An instance of {@link JumpableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1202
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1203
        public static JumpableGenerator of(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1204
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1205
            return RandomGeneratorFactory.of(algorithm.toString(), JumpableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1206
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1207
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1208
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1209
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1210
         * of {@link JumpableGenerator} that utilizes the {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1211
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1212
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1213
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1214
         * @return {@link RandomGeneratorFactory} of {@link JumpableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1215
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1216
        public static RandomGeneratorFactory<JumpableGenerator> factoryOf(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1217
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1218
            return RandomGeneratorFactory.factoryOf(name, JumpableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1219
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1220
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1221
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1222
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1223
         * of {@link JumpableGenerator} that utilizes the specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1224
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1225
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1226
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1227
         * @return {@link RandomGeneratorFactory} of {@link JumpableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1228
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1229
        public static RandomGeneratorFactory<JumpableGenerator> factoryOf(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1230
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1231
            return RandomGeneratorFactory.factoryOf(algorithm.toString(), JumpableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1232
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1233
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1234
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1235
         * Returns a new generator whose internal state is an exact copy of this generator (therefore
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1236
         * their future behavior should be identical if subjected to the same series of operations).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1237
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1238
         * @return a new object that is a copy of this generator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1239
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1240
        JumpableGenerator copy();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1241
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1242
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1243
         * Alter the state of this pseudorandom number generator so as to jump forward a large, fixed
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1244
         * distance (typically 2<sup>64</sup> or more) within its state cycle.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1245
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1246
        void jump();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1247
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1248
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1249
         * Returns the distance by which the {@code jump()} method will jump forward within the state
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1250
         * cycle of this generator object.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1251
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1252
         * @return the default jump distance (as a {@code double} value)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1253
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1254
        double defaultJumpDistance();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1255
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1256
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1257
         * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1258
         * implements the {@link RandomGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1259
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1260
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1261
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1262
         * @implNote It is permitted to implement this method in a manner equivalent to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1263
         *         {@code jumps(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1264
         * @implNote The default implementation produces a sequential stream that  repeatedly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1265
         *         calls {@code copy()} and {@code jump()} on this generator, and the copies become the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1266
         *         generators produced by the stream.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1267
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1268
        default Stream<RandomGenerator> jumps() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1269
            return Stream.generate(this::copyAndJump).sequential();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1270
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1271
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1272
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1273
         * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1274
         * generators, each of which implements the {@link RandomGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1275
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1276
         * @param streamSize the number of generators to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1277
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1278
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1279
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1280
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1281
         * @implNote The default implementation produces a sequential stream that  repeatedly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1282
         *         calls {@code copy()} and {@code jump()} on this generator, and the copies become the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1283
         *         generators produced by the stream.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1284
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1285
        default Stream<RandomGenerator> jumps(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1286
            return jumps().limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1287
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1288
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1289
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1290
         * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1291
         * implements the {@link RandomGenerator} interface.  Ideally the generators in the stream
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1292
         * will appear to be statistically independent.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1293
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1294
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1295
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1296
         * @implNote The default implementation calls {@code jumps()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1297
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1298
        default Stream<RandomGenerator> rngs() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1299
            return this.jumps();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1300
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1301
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1302
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1303
         * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1304
         * generators, each of which implements the {@link RandomGenerator} interface.  Ideally
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1305
         * the generators in the stream will appear to be statistically independent.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1306
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1307
         * @param streamSize the number of generators to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1308
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1309
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1310
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1311
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1312
         * @implNote The default implementation calls {@code jumps(streamSize)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1313
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1314
        default Stream<RandomGenerator> rngs(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1315
            return this.jumps(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1316
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1317
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1318
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1319
         * Copy this generator, jump this generator forward, then return the copy.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1320
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1321
         * @return a copy of this generator object before the jump occurred
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1322
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1323
        default RandomGenerator copyAndJump() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1324
            RandomGenerator result = copy();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1325
            jump();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1326
            return result;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1327
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1328
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1329
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1330
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1331
    /**
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1332
     * This interface is designed to provide a common protocol for objects that generate sequences
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1333
     * of pseudorandom numbers (or Boolean values) and furthermore can easily not only jump but
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1334
     * also
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1335
     * <i>leap</i> to a very distant point in the state cycle.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1336
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1337
     * Typically one will construct a series of {@link LeapableGenerator} objects by iterative
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1338
     * leaping from a single original {@link LeapableGenerator} object, and then for each such
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1339
     * object produce a subseries of objects by iterative jumping.  There is little conceptual
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1340
     * difference between leaping and jumping, but typically a leap will be a very long jump in the
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1341
     * state cycle (perhaps distance 2<sup>128</sup> or so).
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1342
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1343
     * Ideally, all {@link LeapableGenerator} objects produced by iterative leaping and jumping from
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1344
     * a single original {@link LeapableGenerator} object are statistically independent of one
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1345
     * another and individually uniform. In practice, one must settle for some approximation to
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1346
     * independence and uniformity.  In particular, a specific implementation may assume that each
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1347
     * generator in a stream produced by the {@code leaps} method is used to produce (by jumping) a
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1348
     * number of objects no larger than 2<sup>64</sup>.  Implementors are advised to use algorithms
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1349
     * whose period is at least 2<sup>191</sup>.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1350
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1351
     * Methods are provided to perform a single leap operation and also to produce a stream of
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1352
     * generators produced from the original by iterative copying and leaping of internal state.
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1353
     * The generators produced must implement the {@link JumpableGenerator} interface but need not
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1354
     * also implement the {@link LeapableGenerator} interface.  A typical strategy for a
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1355
     * multithreaded application is to create a single {@link LeapableGenerator} object, calls its
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1356
     * {@code leaps} method exactly once, and then parcel out generators from the resulting stream,
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1357
     * one to each thread.  Then the {@code jumps} method of each such generator be called to
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1358
     * produce a substream of generator objects.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1359
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1360
     * An implementation of the {@link LeapableGenerator} interface must provide concrete
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1361
     * definitions for the methods {@code nextInt()}, {@code nextLong}, {@code period()},
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1362
     * {@code copy()}, {@code jump()}, {@code defaultJumpDistance()}, {@code leap()},
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1363
     * and {@code defaultLeapDistance()}. Default implementations are provided for all other
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1364
     * methods.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1365
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1366
     * Objects that implement {@link LeapableGenerator} are typically not cryptographically secure.
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1367
     * Consider instead using {@link java.security.SecureRandom} to get a cryptographically secure
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1368
     * pseudo-random number generator for use by security-sensitive applications.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1369
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1370
     * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1371
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1372
    public interface LeapableGenerator extends JumpableGenerator {
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1373
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1374
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1375
         * Returns an instance of {@link LeapableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1376
         * {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1377
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1378
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1379
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1380
         * @return An instance of {@link LeapableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1381
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1382
        public static LeapableGenerator of(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1383
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1384
            return RandomGeneratorFactory.of(name, LeapableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1385
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1386
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1387
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1388
         * Returns an instance of {@link LeapableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1389
         * specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1390
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1391
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1392
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1393
         * @return An instance of {@link LeapableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1394
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1395
        public static LeapableGenerator of(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1396
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1397
            return RandomGeneratorFactory.of(algorithm.toString(), LeapableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1398
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1399
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1400
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1401
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1402
         * of {@link LeapableGenerator} that utilizes the {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1403
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1404
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1405
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1406
         * @return {@link RandomGeneratorFactory} of {@link LeapableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1407
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1408
        public static RandomGeneratorFactory<LeapableGenerator> factoryOf(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1409
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1410
            return RandomGeneratorFactory.factoryOf(name, LeapableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1411
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1412
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1413
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1414
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1415
         * of {@link LeapableGenerator} that utilizes the specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1416
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1417
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1418
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1419
         * @return {@link RandomGeneratorFactory} of {@link LeapableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1420
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1421
        public static RandomGeneratorFactory<LeapableGenerator> factoryOf(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1422
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1423
            return RandomGeneratorFactory.factoryOf(algorithm.toString(), LeapableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1424
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1425
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1426
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1427
         * Returns a new generator whose internal state is an exact copy of this generator (therefore
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1428
         * their future behavior should be identical if subjected to the same series of operations).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1429
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1430
         * @return a new object that is a copy of this generator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1431
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1432
        LeapableGenerator copy();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1433
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1434
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1435
         * Alter the state of this pseudorandom number generator so as to leap forward a large, fixed
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1436
         * distance (typically 2<sup>96</sup> or more) within its state cycle.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1437
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1438
        void leap();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1439
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1440
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1441
         * Returns the distance by which the {@code leap()} method will leap forward within the state
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1442
         * cycle of this generator object.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1443
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1444
         * @return the default leap distance (as a {@code double} value)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1445
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1446
        double defaultLeapDistance();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1447
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1448
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1449
         * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1450
         * implements the {@link JumpableGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1451
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1452
         * @return a stream of objects that implement the {@link JumpableGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1453
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1454
         * @implNote It is permitted to implement this method in a manner equivalent to {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1455
         *         leaps(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1456
         * @implNote The default implementation produces a sequential stream that  repeatedly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1457
         *         calls {@code copy()} and {@code leap()} on this generator, and the copies become the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1458
         *         generators produced by the stream.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1459
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1460
        default Stream<JumpableGenerator> leaps() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1461
            return Stream.generate(this::copyAndLeap).sequential();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1462
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1463
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1464
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1465
         * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1466
         * generators, each of which implements the {@link JumpableGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1467
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1468
         * @param streamSize the number of generators to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1469
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1470
         * @return a stream of objects that implement the {@link JumpableGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1471
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1472
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1473
         * @implNote The default implementation produces a sequential stream that  repeatedly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1474
         *         calls {@code copy()} and {@code leap()} on this generator, and the copies become the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1475
         *         generators produced by the stream.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1476
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1477
        default Stream<JumpableGenerator> leaps(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1478
            return leaps().limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1479
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1480
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1481
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1482
         * Copy this generator, leap this generator forward, then return the copy.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1483
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1484
         * @return a copy of this generator object before the leap occurred
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1485
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1486
        default JumpableGenerator copyAndLeap() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1487
            JumpableGenerator result = copy();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1488
            leap();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1489
            return result;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1490
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1491
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1492
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1493
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1494
    /**
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1495
     * This interface is designed to provide a common protocol for objects that generate sequences
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1496
     * of pseudorandom numbers (or Boolean values) and furthermore can easily <i>jump</i> to an
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1497
     * arbitrarily specified distant point in the state cycle.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1498
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1499
     * Ideally, all {@link ArbitrarilyJumpableGenerator} objects produced by iterative jumping from
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1500
     * a single original {@link ArbitrarilyJumpableGenerator} object are statistically independent
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1501
     * of one another and individually uniform, provided that they do not traverse overlapping
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1502
     * portions of the state cycle. In practice, one must settle for some approximation to
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1503
     * independence and uniformity.  In particular, a specific implementation may assume that each
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1504
     * generator in a stream produced by the {@code jumps} method is used to produce a number of
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1505
     * values no larger than the jump distance specified.  Implementors are advised to use
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1506
     * algorithms whose period is at least 2<sup>127</sup>.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1507
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1508
     * For many applications, it suffices to jump forward by a power of two or some small multiple
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1509
     * of a power of two, but this power of two may not be representable as a {@code long} value.
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1510
     * To avoid the use of {@link java.math.BigInteger} values as jump distances, {@code double}
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1511
     * values are used instead.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1512
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1513
     * Methods are provided to perform a single jump operation and also to produce a stream of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1514
     * generators produced from the original by iterative copying and jumping of internal state.  A
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1515
     * typical strategy for a multithreaded application is to create a single
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1516
     * {@link ArbitrarilyJumpableGenerator} object, call its {@code jumps} method exactly once, and
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1517
     * then parcel out generators from the resulting stream, one to each thread.  However, each
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1518
     * generator produced also has type {@link ArbitrarilyJumpableGenerator}; with care, different
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1519
     * jump distances can be used to traverse the entire state cycle in various ways.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1520
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1521
     * An implementation of the {@link ArbitrarilyJumpableGenerator} interface must provide concrete
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1522
     * definitions for the methods {@code nextInt()}, {@code nextLong}, {@code period()},
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1523
     * {@code copy()}, {@code jump(double)}, {@code defaultJumpDistance()}, and
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1524
     * {@code defaultLeapDistance()}. Default implementations are provided for all other methods.
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1525
     * Perhaps the most convenient way to implement this interface is to extend the abstract class
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1526
     * {@link ArbitrarilyJumpableGenerator}, which provides spliterator-based implementations of the
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1527
     * methods {@code ints}, {@code longs}, {@code doubles}, {@code rngs}, {@code jumps}, and
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1528
     * {@code leaps}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1529
     * <p>
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1530
     * Objects that implement {@link ArbitrarilyJumpableGenerator} are typically not
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1531
     * cryptographically secure. Consider instead using {@link java.security.SecureRandom} to get a
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1532
     * cryptographically secure pseudo-random number generator for use by security-sensitive
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1533
     * applications.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1534
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1535
     * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1536
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1537
    public interface ArbitrarilyJumpableGenerator extends LeapableGenerator {
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1538
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1539
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1540
         * Returns an instance of {@link ArbitrarilyJumpableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1541
         * {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1542
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1543
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1544
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1545
         * @return An instance of {@link ArbitrarilyJumpableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1546
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1547
        public static ArbitrarilyJumpableGenerator of(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1548
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1549
            return RandomGeneratorFactory.of(name, ArbitrarilyJumpableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1550
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1551
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1552
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1553
         * Returns an instance of {@link ArbitrarilyJumpableGenerator} that utilizes the
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1554
         * specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1555
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1556
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1557
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1558
         * @return An instance of {@link ArbitrarilyJumpableGenerator}
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1559
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1560
        public static ArbitrarilyJumpableGenerator of(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1561
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1562
            return RandomGeneratorFactory.of(algorithm.toString(), ArbitrarilyJumpableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1563
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1564
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1565
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1566
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1567
         * of {@link ArbitrarilyJumpableGenerator} that utilizes the {@code name} algorithm.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1568
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1569
         * @param name  Name of random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1570
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1571
         * @return {@link RandomGeneratorFactory} of {@link ArbitrarilyJumpableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1572
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1573
        public static RandomGeneratorFactory<ArbitrarilyJumpableGenerator> factoryOf(String name) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1574
            Objects.requireNonNull(name);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1575
            return RandomGeneratorFactory.factoryOf(name, ArbitrarilyJumpableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1576
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1577
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1578
        /**
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1579
         * Returns a {@link RandomGeneratorFactory} that can produce instances
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1580
         * of {@link ArbitrarilyJumpableGenerator} that utilizes the specified {@code algorithm}.
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1581
         *
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1582
         * @param algorithm  Random number generator algorithm
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1583
         *
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1584
         * @return {@link RandomGeneratorFactory} of {@link ArbitrarilyJumpableGenerator}
57671
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1585
         */
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1586
        public static RandomGeneratorFactory<ArbitrarilyJumpableGenerator> factoryOf(Algorithm algorithm) {
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1587
            Objects.requireNonNull(algorithm);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1588
            return RandomGeneratorFactory.factoryOf(algorithm.toString(), ArbitrarilyJumpableGenerator.class);
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1589
        }
6a4be8bf8990 imported patch factory
jlaskey
parents: 57547
diff changeset
  1590
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1591
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1592
         * Returns a new generator whose internal state is an exact copy of this generator (therefore
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1593
         * their future behavior should be identical if subjected to the same series of operations).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1594
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1595
         * @return a new object that is a copy of this generator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1596
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1597
        ArbitrarilyJumpableGenerator copy();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1598
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1599
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1600
         * Alter the state of this pseudorandom number generator so as to jump forward a distance equal
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1601
         * to 2<sup>{@code logDistance}</sup> within its state cycle.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1602
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1603
         * @param logDistance the base-2 logarithm of the distance to jump forward within the state
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1604
         *                    cycle
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1605
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1606
         * @throws IllegalArgumentException if {@code logDistance} is NaN or negative, or if
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1607
         *                                  2<sup>{@code logDistance}</sup> is greater than the period
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1608
         *                                  of this generator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1609
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1610
        void jumpPowerOfTwo(int logDistance);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1611
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1612
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1613
         * Alter the state of this pseudorandom number generator so as to jump forward a specified
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1614
         * distance within its state cycle.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1615
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1616
         * @param distance the distance to jump forward within the state cycle
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1617
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1618
         * @throws IllegalArgumentException if {@code distance} is Nan, negative, or greater than the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1619
         *                                  period of this generator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1620
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1621
        void jump(double distance);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1622
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1623
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1624
         * Alter the state of this pseudorandom number generator so as to jump forward a large, fixed
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1625
         * distance (typically 2<sup>64</sup> or more) within its state cycle.  The distance used is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1626
         * that returned by method {@code defaultJumpDistance()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1627
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1628
        default void jump() { jump(defaultJumpDistance()); }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1629
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1630
        /**
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1631
         * Returns an effectively unlimited stream of new pseudorandom number generators, each of
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1632
         * which implements the {@link ArbitrarilyJumpableGenerator} interface, produced by jumping
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1633
         * copies of this generator by different integer multiples of the specified jump distance.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1634
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1635
         * @param distance a distance to jump forward within the state cycle
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1636
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1637
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1638
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1639
         * @implNote This method is implemented to be equivalent to {@code jumps(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1640
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1641
        default Stream<ArbitrarilyJumpableGenerator> jumps(double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1642
            return Stream.generate(() -> copyAndJump(distance)).sequential();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1643
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1644
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1645
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1646
         * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1647
         * generators, each of which implements the {@link ArbitrarilyJumpableGenerator} interface,
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1648
         * produced by jumping copies of this generator by different integer multiples of the
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1649
         * specified jump distance.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1650
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1651
         * @param streamSize the number of generators to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1652
         * @param distance   a distance to jump forward within the state cycle
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1653
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1654
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1655
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1656
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1657
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1658
        default Stream<ArbitrarilyJumpableGenerator> jumps(long streamSize, double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1659
            return jumps(distance).limit(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1660
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1661
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1662
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1663
         * Alter the state of this pseudorandom number generator so as to jump forward a very large,
57684
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1664
         * fixed distance (typically 2<sup>128</sup> or more) within its state cycle.  The distance
7cb325557832 imported patch comments
jlaskey
parents: 57671
diff changeset
  1665
         * used is that returned by method {@code defaultJLeapDistance()}.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1666
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1667
        default void leap() { jump(defaultLeapDistance()); }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1668
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1669
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1670
         * Copy this generator, jump this generator forward, then return the copy.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1671
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1672
         * @param distance a distance to jump forward within the state cycle
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1673
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1674
         * @return a copy of this generator object before the jump occurred
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1675
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1676
        default ArbitrarilyJumpableGenerator copyAndJump(double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1677
            ArbitrarilyJumpableGenerator result = copy();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1678
            jump(distance);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1679
            return result;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1680
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1681
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1682
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1683
}