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