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