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