src/java.base/share/classes/java/util/random/RandomSupport.java
author jlaskey
Thu, 08 Aug 2019 11:06:54 -0300
branchJDK-8193209-branch
changeset 57684 7cb325557832
parent 57547 56cbdc3ea079
child 59080 1b314be4feb2
permissions -rw-r--r--
imported patch comments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     1
/*
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
     2
 * Copyright (c) 2013, 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.util.function.Consumer;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    29
import java.util.function.DoubleConsumer;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    30
import java.util.function.IntConsumer;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    31
import java.util.function.LongConsumer;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    32
import java.util.random.RandomGenerator.SplittableGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    33
import java.util.Spliterator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    34
import java.util.stream.DoubleStream;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    35
import java.util.stream.IntStream;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    36
import java.util.stream.LongStream;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    37
import java.util.stream.Stream;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    38
import java.util.stream.StreamSupport;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    39
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    40
/**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    41
 * Low-level utility methods helpful for implementing pseudorandom number generators.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    42
 *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    43
 * This class is mostly for library writers creating specific implementations of the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    44
 * interface {@link RandomGenerator}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    45
 *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    46
 * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    47
 */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    48
public class RandomSupport {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    49
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    50
    /*
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    51
     * Implementation Overview.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    52
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    53
     * This class provides utility methods and constants frequently
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    54
     * useful in the implentation of pseudorandom number generators
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    55
     * that satisfy the interface {@link RandomGenerator}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    56
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    57
     * File organization: First some message strings, then the main
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    58
     * public methods, followed by a non-public base spliterator class.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    59
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    60
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    61
    // IllegalArgumentException messages
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    62
    static final String BAD_SIZE = "size must be non-negative";
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    63
    static final String BAD_DISTANCE = "jump distance must be finite, positive, and an exact integer";
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    64
    static final String BAD_BOUND = "bound must be positive";
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    65
    static final String BAD_FLOATING_BOUND = "bound must be finite and positive";
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    66
    static final String BAD_RANGE = "bound must be greater than origin";
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    67
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    68
    /* ---------------- public methods ---------------- */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    69
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    70
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    71
     * Check a {@code long} proposed stream size for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    72
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    73
     * @param streamSize the proposed stream size
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    74
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    75
     * @throws IllegalArgumentException if {@code streamSize} is negative
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    76
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    77
    public static void checkStreamSize(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    78
        if (streamSize < 0L)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    79
            throw new IllegalArgumentException(BAD_SIZE);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    80
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    81
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    82
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    83
     * Check a {@code double} proposed jump distance for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    84
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    85
     * @param distance the proposed jump distance
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    86
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    87
     * @throws IllegalArgumentException if {@code size} not positive, finite, and an exact integer
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    88
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    89
    public static void checkJumpDistance(double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    90
        if (!(distance > 0.0 && distance < Float.POSITIVE_INFINITY
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    91
                             && distance == Math.floor(distance))) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    92
            throw new IllegalArgumentException(BAD_DISTANCE);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    93
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    94
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    95
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    96
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    97
     * Checks a {@code float} upper bound value for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    98
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
    99
     * @param bound the upper bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   100
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   101
     * @throws IllegalArgumentException if {@code bound} is not positive and finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   102
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   103
    public static void checkBound(float bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   104
        if (!(bound > 0.0 && bound < Float.POSITIVE_INFINITY)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   105
            throw new IllegalArgumentException(BAD_FLOATING_BOUND);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   106
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   107
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   108
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   109
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   110
     * Checks a {@code double} upper bound value for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   111
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   112
     * @param bound the upper bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   113
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   114
     * @throws IllegalArgumentException if {@code bound} is not positive and finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   115
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   116
    public static void checkBound(double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   117
        if (!(bound > 0.0 && bound < Double.POSITIVE_INFINITY)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   118
            throw new IllegalArgumentException(BAD_FLOATING_BOUND);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   119
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   120
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   121
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   122
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   123
     * Checks an {@code int} upper bound value for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   124
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   125
     * @param bound the upper bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   126
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   127
     * @throws IllegalArgumentException if {@code bound} is not positive
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   128
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   129
    public static void checkBound(int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   130
        if (bound <= 0) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   131
            throw new IllegalArgumentException(BAD_BOUND);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   132
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   133
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   134
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   135
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   136
     * Checks a {@code long} upper bound value for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   137
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   138
     * @param bound the upper bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   139
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   140
     * @throws IllegalArgumentException if {@code bound} is not positive
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   141
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   142
    public static void checkBound(long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   143
        if (bound <= 0) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   144
            throw new IllegalArgumentException(BAD_BOUND);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   145
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   146
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   147
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   148
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   149
     * Checks a {@code float} range for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   150
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   151
     * @param origin the least value (inclusive) in the range
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   152
     * @param bound  the upper bound (exclusive) of the range
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   153
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   154
     * @throws IllegalArgumentException unless {@code origin} is finite, {@code bound} is finite,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   155
     *                                  and {@code bound - origin} is finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   156
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   157
    public static void checkRange(float origin, float bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   158
        if (!(origin < bound && (bound - origin) < Float.POSITIVE_INFINITY)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   159
            throw new IllegalArgumentException(BAD_RANGE);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   160
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   161
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   162
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   163
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   164
     * Checks a {@code double} range for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   165
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   166
     * @param origin the least value (inclusive) in the range
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   167
     * @param bound  the upper bound (exclusive) of the range
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   168
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   169
     * @throws IllegalArgumentException unless {@code origin} is finite, {@code bound} is finite,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   170
     *                                  and {@code bound - origin} is finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   171
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   172
    public static void checkRange(double origin, double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   173
        if (!(origin < bound && (bound - origin) < Double.POSITIVE_INFINITY)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   174
            throw new IllegalArgumentException(BAD_RANGE);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   175
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   176
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   177
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   178
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   179
     * Checks an {@code int} range for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   180
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   181
     * @param origin the least value that can be returned
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   182
     * @param bound  the upper bound (exclusive) for the returned value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   183
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   184
     * @throws IllegalArgumentException if {@code origin} is greater than or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   185
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   186
    public static void checkRange(int origin, int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   187
        if (origin >= bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   188
            throw new IllegalArgumentException(BAD_RANGE);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   189
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   190
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   191
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   192
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   193
     * Checks a {@code long} range for validity.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   194
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   195
     * @param origin the least value that can be returned
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   196
     * @param bound  the upper bound (exclusive) for the returned value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   197
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   198
     * @throws IllegalArgumentException if {@code origin} is greater than or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   199
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   200
    public static void checkRange(long origin, long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   201
        if (origin >= bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   202
            throw new IllegalArgumentException(BAD_RANGE);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   203
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   204
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   205
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   206
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   207
     * Given an array of seed bytes of any length, construct an array
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   208
     * of {@code long} seed values of length {@code n}, such that the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   209
     * last {@code z} values are not all zero.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   210
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   211
     * @param seed an array of {@code byte} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   212
     * @param n the length of the result array (should be nonnegative)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   213
     * @param z the number of trailing result elements that are required
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   214
     *        to be not all zero (should be nonnegative but not larger
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   215
     *        than {@code n})
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   216
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   217
     * @return an array of length {@code n} containing {@code long} seed values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   218
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   219
    public static long[] convertSeedBytesToLongs(byte[] seed, int n, int z) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   220
        final long[] result = new long[n];
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   221
        final int m = Math.min(seed.length, n << 3);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   222
        // Distribute seed bytes into the words to be formed.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   223
        for (int j = 0; j < m; j++) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   224
            result[j>>3] = (result[j>>3] << 8) | seed[j];
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   225
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   226
        // If there aren't enough seed bytes for all the words we need,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   227
        // use a SplitMix-style PRNG to fill in the rest.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   228
        long v = result[0];
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   229
        for (int j = (m + 7) >> 3; j < n; j++) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   230
            result[j] = mixMurmur64(v += SILVER_RATIO_64);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   231
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   232
        // Finally, we need to make sure the last z words are not all zero.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   233
        search: {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   234
            for (int j = n - z; j < n; j++) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   235
                if (result[j] != 0) break search;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   236
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   237
            // If they are, fill in using a SplitMix-style PRNG.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   238
            // Using "& ~1L" in the next line defends against the case z==1
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   239
            // by guaranteeing that the first generated value will be nonzero.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   240
            long w = result[0] & ~1L;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   241
            for (int j = n - z; j < n; j++) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   242
                result[j] = mixMurmur64(w += SILVER_RATIO_64);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   243
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   244
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   245
        return result;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   246
    }
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
     * Given an array of seed bytes of any length, construct an array
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   250
     * of {@code int} seed values of length {@code n}, such that the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   251
     * last {@code z} values are not all zero.
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 seed an array of {@code byte} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   254
     * @param n the length of the result array (should be nonnegative)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   255
     * @param z the number of trailing result elements that are required
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   256
     *        to be not all zero (should be nonnegative but not larger
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   257
     *        than {@code n})
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   258
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   259
     * @return an array of length {@code n} containing {@code int} seed values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   260
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   261
    public static int[] convertSeedBytesToInts(byte[] seed, int n, int z) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   262
        final int[] result = new int[n];
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   263
        final int m = Math.min(seed.length, n << 2);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   264
        // Distribute seed bytes into the words to be formed.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   265
        for (int j = 0; j < m; j++) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   266
            result[j>>2] = (result[j>>2] << 8) | seed[j];
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   267
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   268
        // If there aren't enough seed bytes for all the words we need,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   269
        // use a SplitMix-style PRNG to fill in the rest.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   270
        int v = result[0];
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   271
        for (int j = (m + 3) >> 2; j < n; j++) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   272
            result[j] = mixMurmur32(v += SILVER_RATIO_32);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   273
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   274
        // Finally, we need to make sure the last z words are not all zero.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   275
        search: {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   276
            for (int j = n - z; j < n; j++) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   277
                if (result[j] != 0) break search;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   278
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   279
            // If they are, fill in using a SplitMix-style PRNG.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   280
            // Using "& ~1" in the next line defends against the case z==1
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   281
            // by guaranteeing that the first generated value will be nonzero.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   282
            int w = result[0] & ~1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   283
            for (int j = n - z; j < n; j++) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   284
                result[j] = mixMurmur32(w += SILVER_RATIO_32);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   285
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   286
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   287
        return result;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   288
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   289
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   290
    /*
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   291
     * Bounded versions of nextX methods used by streams, as well as
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   292
     * the public nextX(origin, bound) methods.  These exist mainly to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   293
     * avoid the need for multiple versions of stream spliterators
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   294
     * across the different exported forms of streams.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   295
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   296
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   297
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   298
     * This is the form of {@code nextLong} used by a {@link LongStream}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   299
     * {@link Spliterator} and by the public method
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   300
     * {@code nextLong(origin, bound)}.  If {@code origin} is greater
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   301
     * than {@code bound}, then this method simply calls the unbounded
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   302
     * version of {@code nextLong()}, choosing pseudorandomly from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   303
     * among all 2<sup>64</sup> possible {@code long} values}, and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   304
     * otherwise uses one or more calls to {@code nextLong()} to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   305
     * choose a value pseudorandomly from the possible values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   306
     * between {@code origin} (inclusive) and {@code bound} (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   307
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   308
     * @implNote This method first calls {@code nextLong()} to obtain
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   309
     * a {@code long} value that is assumed to be pseudorandomly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   310
     * chosen uniformly and independently from the 2<sup>64</sup>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   311
     * possible {@code long} values (that is, each of the 2<sup>64</sup>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   312
     * possible long values is equally likely to be chosen).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   313
     * Under some circumstances (when the specified range is not
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   314
     * a power of 2), {@code nextLong()} may be called additional times
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   315
     * to ensure that that the values in the specified range are
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   316
     * equally likely to be chosen (provided the assumption holds).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   317
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   318
     * The implementation considers four cases:
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   319
     * <ol>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   320
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   321
     * <li> If the {@code} bound} is less than or equal to the {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   322
     *      (indicated an unbounded form), the 64-bit {@code long} value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   323
     *      obtained from {@code nextLong()} is returned directly.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   324
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   325
     * <li> Otherwise, if the length <i>n</i> of the specified range is an
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   326
     *      exact power of two 2<sup><i>m</i></sup> for some integer
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   327
     *      <i>m</i>, then return the sum of {@code origin} and the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   328
     *      <i>m</i> lowest-order bits of the value from {@code nextLong()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   329
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   330
     * <li> Otherwise, if the length <i>n</i> of the specified range
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   331
     *      is less than 2<sup>63</sup>, then the basic idea is to use the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   332
     *      remainder modulo <i>n</i> of the value from {@code nextLong()},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   333
     *      but with this approach some values will be over-represented.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   334
     *      Therefore a loop is used to avoid potential bias by rejecting
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   335
     *      candidates that are too large.  Assuming that the results from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   336
     *      {@code nextLong()} are truly chosen uniformly and independently,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   337
     *      the expected number of iterations will be somewhere between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   338
     *      1 and 2, depending on the precise value of <i>n</i>.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   339
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   340
     * <li> Otherwise, the length <i>n</i> of the specified range
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   341
     *      cannot be represented as a positive {@code long} value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   342
     *      A loop repeatedly calls {@code nextlong()} until obtaining
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   343
     *      a suitable candidate,  Again, the expected number of iterations
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   344
     *      is less than 2.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   345
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   346
     * </ol>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   347
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   348
     * @param rng a random number generator to be used as a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   349
     *        source of pseudorandom {@code long} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   350
     * @param origin the least value that can be produced,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   351
     *        unless greater than or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   352
     * @param bound the upper bound (exclusive), unless {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   353
     *        is greater than or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   354
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   355
     * @return a pseudorandomly chosen {@code long} value,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   356
     *         which will be between {@code origin} (inclusive) and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   357
     *         {@code bound} exclusive unless {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   358
     *         is greater than or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   359
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   360
    public static long boundedNextLong(RandomGenerator rng, long origin, long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   361
        long r = rng.nextLong();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   362
        if (origin < bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   363
            // It's not case (1).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   364
            final long n = bound - origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   365
            final long m = n - 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   366
            if ((n & m) == 0L) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   367
                // It is case (2): length of range is a power of 2.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   368
                r = (r & m) + origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   369
            } else if (n > 0L) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   370
                // It is case (3): need to reject over-represented candidates.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   371
                /* This loop takes an unlovable form (but it works):
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   372
                   because the first candidate is already available,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   373
                   we need a break-in-the-middle construction,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   374
                   which is concisely but cryptically performed
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   375
                   within the while-condition of a body-less for loop. */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   376
                for (long u = r >>> 1;            // ensure nonnegative
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   377
                     u + m - (r = u % n) < 0L;    // rejection check
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   378
                     u = rng.nextLong() >>> 1) // retry
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   379
                    ;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   380
                r += origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   381
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   382
            else {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   383
                // It is case (4): length of range not representable as long.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   384
                while (r < origin || r >= bound)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   385
                    r = rng.nextLong();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   386
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   387
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   388
        return r;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   389
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   390
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   391
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   392
     * This is the form of {@code nextLong} used by the public method
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   393
     * {@code nextLong(bound)}.  This is essentially a version of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   394
     * {@code boundedNextLong(origin, bound)} that has been
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   395
     * specialized for the case where the {@code origin} is zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   396
     * and the {@code bound} is greater than zero.  The value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   397
     * returned is chosen pseudorandomly from nonnegative integer
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   398
     * values less than {@code bound}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   399
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   400
     * @implNote This method first calls {@code nextLong()} to obtain
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   401
     * a {@code long} value that is assumed to be pseudorandomly
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   402
     * chosen uniformly and independently from the 2<sup>64</sup>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   403
     * possible {@code long} values (that is, each of the 2<sup>64</sup>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   404
     * possible long values is equally likely to be chosen).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   405
     * Under some circumstances (when the specified range is not
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   406
     * a power of 2), {@code nextLong()} may be called additional times
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   407
     * to ensure that that the values in the specified range are
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   408
     * equally likely to be chosen (provided the assumption holds).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   409
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   410
     * The implementation considers two cases:
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   411
     * <ol>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   412
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   413
     * <li> If {@code bound} is an exact power of two 2<sup><i>m</i></sup>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   414
     *      for some integer <i>m</i>, then return the sum of {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   415
     *      and the <i>m</i> lowest-order bits of the value from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   416
     *      {@code nextLong()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   417
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   418
     * <li> Otherwise, the basic idea is to use the remainder modulo
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   419
     *      <i>bound</i> of the value from {@code nextLong()},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   420
     *      but with this approach some values will be over-represented.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   421
     *      Therefore a loop is used to avoid potential bias by rejecting
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   422
     *      candidates that vare too large.  Assuming that the results from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   423
     *      {@code nextLong()} are truly chosen uniformly and independently,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   424
     *      the expected number of iterations will be somewhere between
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   425
     *      1 and 2, depending on the precise value of <i>bound</i>.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   426
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   427
     * </ol>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   428
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   429
     * @param rng a random number generator to be used as a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   430
     *        source of pseudorandom {@code long} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   431
     * @param bound the upper bound (exclusive); must be greater than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   432
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   433
     * @return a pseudorandomly chosen {@code long} value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   434
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   435
    public static long boundedNextLong(RandomGenerator rng, long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   436
        // Specialize boundedNextLong for origin == 0, bound > 0
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   437
        final long m = bound - 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   438
        long r = rng.nextLong();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   439
        if ((bound & m) == 0L) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   440
            // The bound is a power of 2.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   441
            r &= m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   442
        } else {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   443
            // Must reject over-represented candidates
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   444
            /* This loop takes an unlovable form (but it works):
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   445
               because the first candidate is already available,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   446
               we need a break-in-the-middle construction,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   447
               which is concisely but cryptically performed
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   448
               within the while-condition of a body-less for loop. */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   449
            for (long u = r >>> 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   450
                 u + m - (r = u % bound) < 0L;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   451
                 u = rng.nextLong() >>> 1)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   452
                ;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   453
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   454
        return r;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   455
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   456
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   457
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   458
     * This is the form of {@code nextInt} used by an {@link IntStream}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   459
     * {@link Spliterator} and by the public method
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   460
     * {@code nextInt(origin, bound)}.  If {@code origin} is greater
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   461
     * than {@code bound}, then this method simply calls the unbounded
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   462
     * version of {@code nextInt()}, choosing pseudorandomly from
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   463
     * among all 2<sup>64</sup> possible {@code int} values}, and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   464
     * otherwise uses one or more calls to {@code nextInt()} to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   465
     * choose a value pseudorandomly from the possible values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   466
     * between {@code origin} (inclusive) and {@code bound} (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   467
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   468
     * @param rng a random number generator to be used as a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   469
     *        source of pseudorandom {@code int} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   470
     * @param origin the least value that can be produced,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   471
     *        unless greater than or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   472
     * @param bound the upper bound (exclusive), unless {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   473
     *        is greater than or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   474
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   475
     * @return a pseudorandomly chosen {@code int} value,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   476
     *         which will be between {@code origin} (inclusive) and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   477
     *         {@code bound} exclusive unless {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   478
     *         is greater than or equal to {@code bound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   479
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   480
     * @implNote The implementation of this method is identical to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   481
     *           the implementation of {@code nextLong(origin, bound)}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   482
     *           except that {@code int} values and the {@code nextInt()}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   483
     *           method are used rather than {@code long} values and the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   484
     *           {@code nextLong()} method.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   485
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   486
    public static int boundedNextInt(RandomGenerator rng, int origin, int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   487
        int r = rng.nextInt();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   488
        if (origin < bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   489
            // It's not case (1).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   490
            final int n = bound - origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   491
            final int m = n - 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   492
            if ((n & m) == 0) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   493
                // It is case (2): length of range is a power of 2.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   494
                r = (r & m) + origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   495
            } else if (n > 0) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   496
                // It is case (3): need to reject over-represented candidates.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   497
                for (int u = r >>> 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   498
                     u + m - (r = u % n) < 0;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   499
                     u = rng.nextInt() >>> 1)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   500
                    ;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   501
                r += origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   502
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   503
            else {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   504
                // It is case (4): length of range not representable as long.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   505
                while (r < origin || r >= bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   506
                    r = rng.nextInt();
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
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   510
        return r;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   511
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   512
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   513
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   514
     * This is the form of {@code nextInt} used by the public method
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   515
     * {@code nextInt(bound)}.  This is essentially a version of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   516
     * {@code boundedNextInt(origin, bound)} that has been
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   517
     * specialized for the case where the {@code origin} is zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   518
     * and the {@code bound} is greater than zero.  The value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   519
     * returned is chosen pseudorandomly from nonnegative integer
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   520
     * values less than {@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
     * @param rng a random number generator to be used as a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   523
     *        source of pseudorandom {@code long} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   524
     * @param bound the upper bound (exclusive); must be greater than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   525
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   526
     * @return a pseudorandomly chosen {@code long} value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   527
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   528
     * @implNote The implementation of this method is identical to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   529
     *           the implementation of {@code nextLong(bound)}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   530
     *           except that {@code int} values and the {@code nextInt()}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   531
     *           method are used rather than {@code long} values and the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   532
     *           {@code nextLong()} method.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   533
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   534
    public static int boundedNextInt(RandomGenerator rng, int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   535
        // Specialize boundedNextInt for origin == 0, bound > 0
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   536
        final int m = bound - 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   537
        int r = rng.nextInt();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   538
        if ((bound & m) == 0) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   539
            // The bound is a power of 2.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   540
            r &= m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   541
        } else {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   542
            // Must reject over-represented candidates
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   543
            for (int u = r >>> 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   544
                 u + m - (r = u % bound) < 0;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   545
                 u = rng.nextInt() >>> 1)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   546
                ;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   547
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   548
        return r;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   549
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   550
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   551
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   552
     * This is the form of {@code nextDouble} used by a {@link DoubleStream}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   553
     * {@link Spliterator} and by the public method
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   554
     * {@code nextDouble(origin, bound)}.  If {@code origin} is greater
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   555
     * than {@code bound}, then this method simply calls the unbounded
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   556
     * version of {@code nextDouble()}, and otherwise scales and translates
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   557
     * the result of a call to {@code nextDouble()} so that it lies
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   558
     * between {@code origin} (inclusive) and {@code bound} (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   559
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   560
     * @implNote The implementation considers two cases:
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   561
     * <ol>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   562
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   563
     * <li> If the {@code bound} is less than or equal to the {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   564
     *      (indicated an unbounded form), the 64-bit {@code double} value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   565
     *      obtained from {@code nextDouble()} is returned directly.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   566
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   567
     * <li> Otherwise, the result of a call to {@code nextDouble} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   568
     *      multiplied by {@code (bound - origin)}, then {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   569
     *      is added, and then if this this result is not less than
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   570
     *      {@code bound} (which can sometimes occur because of rounding),
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   571
     *      it is replaced with the largest {@code double} value that
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   572
     *      is less than {@code bound}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   573
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   574
     * </ol>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   575
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   576
     * @param rng a random number generator to be used as a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   577
     *        source of pseudorandom {@code double} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   578
     * @param origin the least value that can be produced,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   579
     *        unless greater than or equal to {@code bound}; must be finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   580
     * @param bound the upper bound (exclusive), unless {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   581
     *        is greater than or equal to {@code bound}; must be finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   582
     * @return a pseudorandomly chosen {@code double} value,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   583
     *         which will be between {@code origin} (inclusive) and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   584
     *         {@code bound} exclusive unless {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   585
     *         is greater than or equal to {@code bound},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   586
     *         in which case it will be between 0.0 (inclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   587
     *         and 1.0 (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   588
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   589
    public static double boundedNextDouble(RandomGenerator rng, double origin, double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   590
        double r = rng.nextDouble();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   591
        if (origin < bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   592
            r = r * (bound - origin) + origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   593
            if (r >= bound)  // may need to correct a rounding problem
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   594
                r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   595
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   596
        return r;
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
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   600
     * This is the form of {@code nextDouble} used by the public method
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   601
     * {@code nextDouble(bound)}.  This is essentially a version of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   602
     * {@code boundedNextDouble(origin, bound)} that has been
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   603
     * specialized for the case where the {@code origin} is zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   604
     * and the {@code bound} is greater than zero.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   605
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   606
     * @implNote The result of a call to {@code nextDouble} is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   607
     *      multiplied by {@code bound}, and then if this result is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   608
     *      not less than {@code bound} (which can sometimes occur
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   609
     *      because of rounding), it is replaced with the largest
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   610
     *      {@code double} value that is less than {@code bound}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   611
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   612
     * @param rng a random number generator to be used as a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   613
     *        source of pseudorandom {@code double} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   614
     * @param bound the upper bound (exclusive); must be finite and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   615
     *        greater than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   616
     * @return a pseudorandomly chosen {@code double} value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   617
     *         between zero (inclusive) and {@code bound} (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   618
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   619
    public static double boundedNextDouble(RandomGenerator rng, double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   620
        // Specialize boundedNextDouble for origin == 0, bound > 0
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   621
        double r = rng.nextDouble();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   622
        r = r * bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   623
        if (r >= bound)  // may need to correct a rounding problem
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   624
            r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   625
        return r;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   626
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   627
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   628
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   629
     * This is the form of {@code nextFloat} used by a {@code Stream<Float>}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   630
     * {@link Spliterator} (if there were any) and by the public method
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   631
     * {@code nextFloat(origin, bound)}.  If {@code origin} is greater
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   632
     * than {@code bound}, then this method simply calls the unbounded
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   633
     * version of {@code nextFloat()}, and otherwise scales and translates
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   634
     * the result of a call to {@code nextFloat()} so that it lies
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   635
     * between {@code origin} (inclusive) and {@code bound} (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   636
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   637
     * @implNote The implementation of this method is identical to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   638
     *     the implementation of {@code nextDouble(origin, bound)}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   639
     *     except that {@code float} values and the {@code nextFloat()}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   640
     *     method are used rather than {@code double} values and the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   641
     *     {@code nextDouble()} method.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   642
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   643
     * @param rng a random number generator to be used as a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   644
     *        source of pseudorandom {@code float} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   645
     * @param origin the least value that can be produced,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   646
     *        unless greater than or equal to {@code bound}; must be finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   647
     * @param bound the upper bound (exclusive), unless {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   648
     *        is greater than or equal to {@code bound}; must be finite
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   649
     * @return a pseudorandomly chosen {@code float} value,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   650
     *         which will be between {@code origin} (inclusive) and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   651
     *         {@code bound} exclusive unless {@code origin}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   652
     *         is greater than or equal to {@code bound},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   653
     *         in which case it will be between 0.0 (inclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   654
     *         and 1.0 (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
    public static float boundedNextFloat(RandomGenerator rng, float origin, float bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   657
        float r = rng.nextFloat();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   658
        if (origin < bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   659
            r = r * (bound - origin) + origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   660
            if (r >= bound) // may need to correct a rounding problem
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   661
                r = Float.intBitsToFloat(Float.floatToIntBits(bound) - 1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   662
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   663
        return r;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   664
    }
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
     * This is the form of {@code nextFloat} used by the public method
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   668
     * {@code nextFloat(bound)}.  This is essentially a version of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   669
     * {@code boundedNextFloat(origin, bound)} that has been
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   670
     * specialized for the case where the {@code origin} is zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   671
     * and the {@code bound} is greater than zero.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   672
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   673
     * @implNote The implementation of this method is identical to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   674
     *     the implementation of {@code nextDouble(bound)}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   675
     *     except that {@code float} values and the {@code nextFloat()}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   676
     *     method are used rather than {@code double} values and the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   677
     *     {@code nextDouble()} method.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   678
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   679
     * @param rng a random number generator to be used as a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   680
     *        source of pseudorandom {@code float} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   681
     * @param bound the upper bound (exclusive); must be finite and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   682
     *        greater than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   683
     * @return a pseudorandomly chosen {@code float} value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   684
     *         between zero (inclusive) and {@code bound} (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   685
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   686
    public static float boundedNextFloat(RandomGenerator rng, float bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   687
        // Specialize boundedNextFloat for origin == 0, bound > 0
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   688
        float r = rng.nextFloat();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   689
        r = r * bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   690
        if (r >= bound) // may need to correct a rounding problem
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   691
            r = Float.intBitsToFloat(Float.floatToIntBits(bound) - 1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   692
        return r;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   693
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   694
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   695
    // The following decides which of two strategies initialSeed() will use.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   696
    private static boolean secureRandomSeedRequested() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   697
        String pp = java.security.AccessController.doPrivileged(
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   698
                new sun.security.action.GetPropertyAction(
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   699
                        "java.util.secureRandomSeed"));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   700
        return (pp != null && pp.equalsIgnoreCase("true"));
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
    private static final boolean useSecureRandomSeed = secureRandomSeedRequested();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   704
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   705
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   706
     * Returns a {@code long} value (chosen from some
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   707
     * machine-dependent entropy source) that may be useful for
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   708
     * initializing a source of seed values for instances of {@link RandomGenerator}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   709
     * created by zero-argument constructors.  (This method should
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   710
     * <i>not</i> be called repeatedly, once per constructed
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   711
     * object; at most it should be called once per class.)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   712
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   713
     * @return a {@code long} value, randomly chosen using
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   714
     *         appropriate environmental entropy
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   715
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   716
    public static long initialSeed() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   717
        if (useSecureRandomSeed) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   718
            byte[] seedBytes = java.security.SecureRandom.getSeed(8);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   719
            long s = (long)(seedBytes[0]) & 0xffL;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   720
            for (int i = 1; i < 8; ++i)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   721
                s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   722
            return s;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   723
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   724
        return (mixStafford13(System.currentTimeMillis()) ^
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   725
                mixStafford13(System.nanoTime()));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   726
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   727
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   728
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   729
     * The first 32 bits of the golden ratio (1+sqrt(5))/2, forced to be odd.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   730
     * Useful for producing good Weyl sequences or as an arbitrary nonzero odd value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   731
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   732
    public static final int  GOLDEN_RATIO_32 = 0x9e3779b9;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   733
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   734
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   735
     * The first 64 bits of the golden ratio (1+sqrt(5))/2, forced to be odd.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   736
     * Useful for producing good Weyl sequences or as an arbitrary nonzero odd value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   737
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   738
    public static final long GOLDEN_RATIO_64 = 0x9e3779b97f4a7c15L;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   739
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   740
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   741
     * The first 32 bits of the silver ratio 1+sqrt(2), forced to be odd.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   742
     * Useful for producing good Weyl sequences or as an arbitrary nonzero odd value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   743
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   744
    public static final int  SILVER_RATIO_32 = 0x6A09E667;
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 first 64 bits of the silver ratio 1+sqrt(2), forced to be odd.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   748
     * Useful for producing good Weyl sequences or as an arbitrary nonzero odd value.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   749
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   750
    public static final long SILVER_RATIO_64 = 0x6A09E667F3BCC909L;
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
     * Computes the 64-bit mixing function for MurmurHash3.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   754
     * This is a 64-bit hashing function with excellent avalanche statistics.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   755
     * https://github.com/aappleby/smhasher/wiki/MurmurHash3
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   756
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   757
     * Note that if the argument {@code z} is 0, the result is 0.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   758
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   759
     * @param z any long value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   760
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   761
     * @return the result of hashing z
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   762
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   763
    public static long mixMurmur64(long z) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   764
        z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   765
        z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   766
        return z ^ (z >>> 33);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   767
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   768
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   769
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   770
     * Computes Stafford variant 13 of the 64-bit mixing function for MurmurHash3.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   771
     * This is a 64-bit hashing function with excellent avalanche statistics.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   772
     * http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   773
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   774
     * Note that if the argument {@code z} is 0, the result is 0.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   775
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   776
     * @param z any long value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   777
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   778
     * @return the result of hashing z
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   779
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   780
    public static long mixStafford13(long z) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   781
        z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   782
        z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   783
        return z ^ (z >>> 31);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   784
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   785
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   786
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   787
     * Computes Doug Lea's 64-bit mixing function.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   788
     * This is a 64-bit hashing function with excellent avalanche statistics.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   789
     * It has the advantages of using the same multiplicative constant twice
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   790
     * and of using only 32-bit shifts.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   791
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   792
     * Note that if the argument {@code z} is 0, the result is 0.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   793
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   794
     * @param z any long value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   795
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   796
     * @return the result of hashing z
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   797
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   798
    public static long mixLea64(long z) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   799
        z = (z ^ (z >>> 32)) * 0xdaba0b6eb09322e3L;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   800
        z = (z ^ (z >>> 32)) * 0xdaba0b6eb09322e3L;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   801
        return z ^ (z >>> 32);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   802
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   803
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   804
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   805
     * Computes the 32-bit mixing function for MurmurHash3.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   806
     * This is a 32-bit hashing function with excellent avalanche statistics.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   807
     * https://github.com/aappleby/smhasher/wiki/MurmurHash3
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   808
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   809
     * Note that if the argument {@code z} is 0, the result is 0.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   810
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   811
     * @param z any long value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   812
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   813
     * @return the result of hashing z
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   814
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   815
    public static int mixMurmur32(int z) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   816
        z = (z ^ (z >>> 16)) * 0x85ebca6b;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   817
        z = (z ^ (z >>> 13)) * 0xc2b2ae35;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   818
        return z ^ (z >>> 16);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   819
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   820
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   821
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   822
     * Computes Doug Lea's 32-bit mixing function.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   823
     * This is a 32-bit hashing function with excellent avalanche statistics.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   824
     * It has the advantages of using the same multiplicative constant twice
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   825
     * and of using only 16-bit shifts.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   826
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   827
     * Note that if the argument {@code z} is 0, the result is 0.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   828
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   829
     * @param z any long value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   830
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   831
     * @return the result of hashing z
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   832
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   833
    public static int mixLea32(int z) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   834
        z = (z ^ (z >>> 16)) * 0xd36d884b;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   835
        z = (z ^ (z >>> 16)) * 0xd36d884b;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   836
        return z ^ (z >>> 16);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   837
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   838
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   839
    // Non-public (package only) support for spliterators needed by AbstractSplittableGenerator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   840
    // and AbstractArbitrarilyJumpableGenerator and AbstractSharedGenerator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   841
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   842
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   843
     * Base class for making Spliterator classes for streams of randomly chosen values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   844
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   845
    public abstract static class RandomSpliterator {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   846
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   847
        /** low range value */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   848
        public long index;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   849
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   850
        /** high range value */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   851
        public final long fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   852
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   853
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   854
         * Constructor
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   855
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   856
         * @param index  low range value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   857
         * @param fence  high range value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   858
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   859
        public RandomSpliterator(long index, long fence) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   860
            this.index = index; this.fence = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   861
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   862
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   863
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   864
         * Returns estimated size.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   865
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   866
         * @return estimated size
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   867
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   868
        public long estimateSize() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   869
            return fence - index;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   870
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   871
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   872
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   873
         * Returns characteristics.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   874
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   875
         * @return characteristics
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   876
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   877
        public int characteristics() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   878
            return (Spliterator.SIZED | Spliterator.SUBSIZED |
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   879
                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
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
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   883
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   884
    /*
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   885
     * Implementation support for nextExponential() and nextGaussian() methods of RandomGenerator.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   886
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   887
     * Each is implemented using McFarland's fast modified ziggurat algorithm (largely
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   888
     * table-driven, with rare cases handled by computation and rejection sampling).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   889
     * Walker's alias method for sampling a discrete distribution also plays a role.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   890
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   891
     * The tables themselves, as well as a number of associated parameters, are defined
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   892
     * in class java.util.DoubleZigguratTables, which is automatically generated by the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   893
     * program create_ziggurat_tables.c (which takes only a few seconds to run).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   894
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   895
     * For more information about the algorithms, see these articles:
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   896
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   897
     * Christopher D. McFarland.  2016 (published online 24 Jun 2015).  A modified ziggurat
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   898
     * algorithm for generating exponentially and normally distributed pseudorandom numbers.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   899
     * Journal of Statistical Computation and Simulation 86 (7), pages 1281-1294.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   900
     * https://www.tandfonline.com/doi/abs/10.1080/00949655.2015.1060234
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   901
     * Also at https://arxiv.org/abs/1403.6870 (26 March 2014).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   902
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   903
     * Alastair J. Walker.  1977.  An efficient method for generating discrete random
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   904
     * variables with general distributions. ACM Trans. Math. Software 3, 3
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   905
     * (September 1977), 253-256. DOI: https://doi.org/10.1145/355744.355749
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   906
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   907
     * Certain details of these algorithms depend critically on the quality of the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   908
     * low-order bits delivered by NextLong().  These algorithms should not be used
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   909
     * with RNG algorithms (such as a simple Linear Congruential Generator) whose
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   910
     * low-order output bits do not have good statistical quality.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   911
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   912
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   913
    // Implementation support for nextExponential()
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   914
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   915
    static double computeNextExponential(RandomGenerator rng) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   916
        long U1 = rng.nextLong();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   917
        // Experimentation on a variety of machines indicates that it is overall much faster
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   918
        // to do the following & and < operations on longs rather than first cast U1 to int
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   919
        // (but then we need to cast to int before doing the array indexing operation).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   920
        long i = U1 & DoubleZigguratTables.exponentialLayerMask;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   921
        if (i < DoubleZigguratTables.exponentialNumberOfLayers) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   922
            // This is the fast path (occurring more than 98% of the time).  Make an early exit.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   923
            return DoubleZigguratTables.exponentialX[(int)i] * (U1 >>> 1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   924
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   925
        // We didn't use the upper part of U1 after all.  We'll be able to use it later.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   926
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   927
        for (double extra = 0.0; ; ) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   928
            // Use Walker's alias method to sample an (unsigned) integer j from a discrete
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   929
            // probability distribution that includes the tail and all the ziggurat overhangs;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   930
            // j will be less than DoubleZigguratTables.exponentialNumberOfLayers + 1.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   931
            long UA = rng.nextLong();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   932
            int j = (int)UA & DoubleZigguratTables.exponentialAliasMask;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   933
            if (UA >= DoubleZigguratTables.exponentialAliasThreshold[j]) {
57684
7cb325557832 imported patch comments
jlaskey
parents: 57547
diff changeset
   934
                j = DoubleZigguratTables.exponentialAliasMap[j] &
7cb325557832 imported patch comments
jlaskey
parents: 57547
diff changeset
   935
                    DoubleZigguratTables.exponentialSignCorrectionMask;
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   936
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   937
            if (j > 0) {   // Sample overhang j
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   938
                // For the exponential distribution, every overhang is convex.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   939
                final double[] X = DoubleZigguratTables.exponentialX;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   940
                final double[] Y = DoubleZigguratTables.exponentialY;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   941
                for (;; U1 = (rng.nextLong() >>> 1)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   942
                    long U2 = (rng.nextLong() >>> 1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   943
                    // Compute the actual x-coordinate of the randomly chosen point.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   944
                    double x = (X[j] * 0x1.0p63) + ((X[j-1] - X[j]) * (double)U1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   945
                    // Does the point lie below the curve?
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   946
                    long Udiff = U2 - U1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   947
                    if (Udiff < 0) {
57684
7cb325557832 imported patch comments
jlaskey
parents: 57547
diff changeset
   948
                        // We picked a point in the upper-right triangle.  None of those can be
7cb325557832 imported patch comments
jlaskey
parents: 57547
diff changeset
   949
                        // accepted.  So remap the point into the lower-left triangle and try that.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   950
                        // In effect, we swap U1 and U2, and invert the sign of Udiff.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   951
                        Udiff = -Udiff;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   952
                        U2 = U1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   953
                        U1 -= Udiff;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   954
                    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   955
                    if (Udiff >= DoubleZigguratTables.exponentialConvexMargin) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   956
                        return x + extra;   // The chosen point is way below the curve; accept it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   957
                    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   958
                    // Compute the actual y-coordinate of the randomly chosen point.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   959
                    double y = (Y[j] * 0x1.0p63) + ((Y[j] - Y[j-1]) * (double)U2);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   960
                    // Now see how that y-coordinate compares to the curve
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   961
                    if (y <= Math.exp(-x)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   962
                        return x + extra;   // The chosen point is below the curve; accept it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   963
                    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   964
                    // Otherwise, we reject this sample and have to try again.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   965
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   966
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   967
            // We are now committed to sampling from the tail.  We could do a recursive call
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   968
            // and then add X[0] but we save some time and stack space by using an iterative loop.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   969
            extra += DoubleZigguratTables.exponentialX0;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   970
            // This is like the first five lines of this method, but if it returns, it first adds "extra".
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   971
            U1 = rng.nextLong();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   972
            i = U1 & DoubleZigguratTables.exponentialLayerMask;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   973
            if (i < DoubleZigguratTables.exponentialNumberOfLayers) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   974
                return DoubleZigguratTables.exponentialX[(int)i] * (U1 >>> 1) + extra;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   975
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   976
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   977
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   978
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   979
    // Implementation support for nextGaussian()
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   980
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   981
    static double computeNextGaussian(RandomGenerator rng) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   982
        long U1 = rng.nextLong();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   983
        // Experimentation on a variety of machines indicates that it is overall much faster
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   984
        // to do the following & and < operations on longs rather than first cast U1 to int
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   985
        // (but then we need to cast to int before doing the array indexing operation).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   986
        long i = U1 & DoubleZigguratTables.normalLayerMask;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   987
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   988
        if (i < DoubleZigguratTables.normalNumberOfLayers) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   989
            // This is the fast path (occurring more than 98% of the time).  Make an early exit.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   990
            return DoubleZigguratTables.normalX[(int)i] * U1;   // Note that the sign bit of U1 is used here.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   991
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   992
        // We didn't use the upper part of U1 after all.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   993
        // Pull U1 apart into a sign bit and a 63-bit value for later use.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   994
        double signBit = (U1 >= 0) ? 1.0 : -1.0;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   995
        U1 = (U1 << 1) >>> 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   996
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   997
        // Use Walker's alias method to sample an (unsigned) integer j from a discrete
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   998
        // probability distribution that includes the tail and all the ziggurat overhangs;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
   999
        // j will be less than DoubleZigguratTables.normalNumberOfLayers + 1.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1000
        long UA = rng.nextLong();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1001
        int j = (int)UA & DoubleZigguratTables.normalAliasMask;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1002
        if (UA >= DoubleZigguratTables.normalAliasThreshold[j]) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1003
            j = DoubleZigguratTables.normalAliasMap[j] & DoubleZigguratTables.normalSignCorrectionMask;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1004
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1005
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1006
        double x;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1007
        // Now the goal is to choose the result, which will be multiplied by signBit just before return.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1008
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1009
        // There are four kinds of overhangs:
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1010
        //
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1011
        //  j == 0                          :  Sample from tail
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1012
        //  0 < j < normalInflectionIndex   :  Overhang is convex; can reject upper-right triangle
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1013
        //  j == normalInflectionIndex      :  Overhang includes the inflection point
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1014
        //  j > normalInflectionIndex       :  Overhang is concave; can accept point in lower-left triangle
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1015
        //
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1016
        // Choose one of four loops to compute x, each specialized for a specific kind of overhang.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1017
        // Conditional statements are arranged such that the more likely outcomes are first.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1018
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1019
        // In the three cases other than the tail case:
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1020
        // U1 represents a fraction (scaled by 2**63) of the width of rectangle measured from the left.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1021
        // U2 represents a fraction (scaled by 2**63) of the height of rectangle measured from the top.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1022
        // Together they indicate a randomly chosen point within the rectangle.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1023
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1024
        final double[] X = DoubleZigguratTables.normalX;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1025
        final double[] Y = DoubleZigguratTables.normalY;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1026
        if (j > DoubleZigguratTables.normalInflectionIndex) {   // Concave overhang
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1027
            for (;; U1 = (rng.nextLong() >>> 1)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1028
                long U2 = (rng.nextLong() >>> 1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1029
                // Compute the actual x-coordinate of the randomly chosen point.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1030
                x = (X[j] * 0x1.0p63) + ((X[j-1] - X[j]) * (double)U1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1031
                // Does the point lie below the curve?
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1032
                long Udiff = U2 - U1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1033
                if (Udiff >= 0) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1034
                    break;   // The chosen point is in the lower-left triangle; accept it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1035
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1036
                if (Udiff <= -DoubleZigguratTables.normalConcaveMargin) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1037
                    continue;   // The chosen point is way above the curve; reject it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1038
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1039
                // Compute the actual y-coordinate of the randomly chosen point.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1040
                double y = (Y[j] * 0x1.0p63) + ((Y[j] - Y[j-1]) * (double)U2);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1041
                // Now see how that y-coordinate compares to the curve
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1042
                if (y <= Math.exp(-0.5*x*x)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1043
                    break;   // The chosen point is below the curve; accept it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1044
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1045
                // Otherwise, we reject this sample and have to try again.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1046
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1047
        } else if (j == 0) {   // Tail
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1048
            // Tail-sampling method of Marsaglia and Tsang.  See any one of:
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1049
            // Marsaglia and Tsang. 1984. A fast, easily implemented method for sampling from decreasing
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1050
            //    or symmetric unimodal density functions. SIAM J. Sci. Stat. Comput. 5, 349-359.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1051
            // Marsaglia and Tsang. 1998. The Monty Python method for generating random variables.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1052
            //    ACM Trans. Math. Softw. 24, 3 (September 1998), 341-350.  See page 342, step (4).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1053
            //    http://doi.org/10.1145/292395.292453
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1054
            // Thomas, Luk, Leong, and Villasenor. 2007. Gaussian random number generators.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1055
            //    ACM Comput. Surv. 39, 4, Article 11 (November 2007).  See Algorithm 16.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1056
            //    http://doi.org/10.1145/1287620.1287622
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1057
            // Compute two separate random exponential samples and then compare them in certain way.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1058
            do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1059
                x = (1.0 / DoubleZigguratTables.normalX0) * computeNextExponential(rng);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1060
            } while (computeNextExponential(rng) < 0.5*x*x);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1061
            x += DoubleZigguratTables.normalX0;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1062
        } else if (j < DoubleZigguratTables.normalInflectionIndex) {   // Convex overhang
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1063
            for (;; U1 = (rng.nextLong() >>> 1)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1064
                long U2 = (rng.nextLong() >>> 1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1065
                // Compute the actual x-coordinate of the randomly chosen point.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1066
                x = (X[j] * 0x1.0p63) + ((X[j-1] - X[j]) * (double)U1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1067
                // Does the point lie below the curve?
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1068
                long Udiff = U2 - U1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1069
                if (Udiff < 0) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1070
                    // We picked a point in the upper-right triangle.  None of those can be accepted.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1071
                    // So remap the point into the lower-left triangle and try that.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1072
                    // In effect, we swap U1 and U2, and invert the sign of Udiff.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1073
                    Udiff = -Udiff;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1074
                    U2 = U1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1075
                    U1 -= Udiff;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1076
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1077
                if (Udiff >= DoubleZigguratTables.normalConvexMargin) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1078
                    break;   // The chosen point is way below the curve; accept it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1079
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1080
                // Compute the actual y-coordinate of the randomly chosen point.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1081
                double y = (Y[j] * 0x1.0p63) + ((Y[j] - Y[j-1]) * (double)U2);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1082
                // Now see how that y-coordinate compares to the curve
57684
7cb325557832 imported patch comments
jlaskey
parents: 57547
diff changeset
  1083
                if (y <= Math.exp(-0.5*x*x)) break; // The chosen point is below the curve; accept it.
57547
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1084
                // Otherwise, we reject this sample and have to try again.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1085
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1086
        } else {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1087
            // The overhang includes the inflection point, so the curve is both convex and concave
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1088
            for (;; U1 = (rng.nextLong() >>> 1)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1089
                long U2 = (rng.nextLong() >>> 1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1090
                // Compute the actual x-coordinate of the randomly chosen point.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1091
                x = (X[j] * 0x1.0p63) + ((X[j-1] - X[j]) * (double)U1);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1092
                // Does the point lie below the curve?
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1093
                long Udiff = U2 - U1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1094
                if (Udiff >= DoubleZigguratTables.normalConvexMargin) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1095
                    break;   // The chosen point is way below the curve; accept it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1096
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1097
                if (Udiff <= -DoubleZigguratTables.normalConcaveMargin) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1098
                    continue;   // The chosen point is way above the curve; reject it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1099
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1100
                // Compute the actual y-coordinate of the randomly chosen point.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1101
                double y = (Y[j] * 0x1.0p63) + ((Y[j] - Y[j-1]) * (double)U2);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1102
                // Now see how that y-coordinate compares to the curve
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1103
                if (y <= Math.exp(-0.5*x*x)) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1104
                    break;   // The chosen point is below the curve; accept it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1105
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1106
                // Otherwise, we reject this sample and have to try again.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1107
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1108
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1109
        return signBit*x;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1110
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1111
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1112
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1113
     * This class overrides the stream-producing methods (such as {@code ints()})
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1114
     * in class {@link AbstractGenerator} to provide {@link Spliterator}-based
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1115
     * implmentations that support potentially parallel execution.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1116
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1117
     * To implement a pseudorandom number generator, the programmer needs
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1118
     * only to extend this class and provide implementations for the methods
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1119
     * {@code nextInt()}, {@code nextLong()}, {@code makeIntsSpliterator},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1120
     * {@code makeLongsSpliterator}, and {@code makeDoublesSpliterator}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1121
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1122
     * This class is not public; it provides shared code to the public
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1123
     * classes {@link AbstractSplittableGenerator}, {@link AbstractSharedGenerator},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1124
     * and {@link AbstractArbitrarilyJumpableGenerator}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1125
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1126
     * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1127
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1128
    public abstract static class AbstractSpliteratorGenerator implements RandomGenerator {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1129
        /*
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1130
         * Implementation Overview.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1131
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1132
         * This class provides most of the "user API" methods needed to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1133
         * satisfy the interface RandomGenerator.  An implementation of this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1134
         * interface need only extend this class and provide implementations
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1135
         * of six methods: nextInt, nextLong, and nextDouble (the versions
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1136
         * that take no arguments) and makeIntsSpliterator,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1137
         * makeLongsSpliterator, and makeDoublesSpliterator.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1138
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1139
         * File organization: First the non-public abstract methods needed
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1140
         * to create spliterators, then the main public methods.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1141
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1142
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1143
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1144
         * Needs comment (was made public to be overridden out of package.)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1145
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1146
         * @param index low
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1147
         * @param fence high
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1148
         * @param origin low
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1149
         * @param bound high
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1150
         * @return result
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1151
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1152
        public abstract Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1153
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1154
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1155
         * Needs comment (was made public to be overridden out of package.)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1156
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1157
         * @param index low
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1158
         * @param fence high
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1159
         * @param origin low
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1160
         * @param bound high
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1161
         * @return result
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1162
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1163
        public abstract Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1164
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1165
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1166
         * Needs comment (was made public to be overridden out of package.)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1167
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1168
         * @param index low
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1169
         * @param fence high
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1170
         * @param origin low
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1171
         * @param bound high
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1172
         * @return result
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1173
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1174
        public abstract Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1175
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1176
        /* ---------------- public methods ---------------- */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1177
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1178
        // stream methods, coded in a way intended to better isolate for
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1179
        // maintenance purposes the small differences across forms.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1180
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1181
        private static IntStream intStream(Spliterator.OfInt srng) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1182
            return StreamSupport.intStream(srng, false);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1183
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1184
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1185
        private static LongStream longStream(Spliterator.OfLong srng) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1186
            return StreamSupport.longStream(srng, false);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1187
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1188
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1189
        private static DoubleStream doubleStream(Spliterator.OfDouble srng) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1190
            return StreamSupport.doubleStream(srng, false);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1191
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1192
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1193
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1194
         * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code int}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1195
         * values from this generator and/or one split from it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1196
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1197
         * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1198
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1199
         * @return a stream of pseudorandom {@code int} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1200
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1201
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1202
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1203
        public IntStream ints(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1204
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1205
            return intStream(makeIntsSpliterator(0L, streamSize, Integer.MAX_VALUE, 0));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1206
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1207
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1208
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1209
         * Returns an effectively unlimited stream of pseudorandomly chosen
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1210
         * {@code int} values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1211
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1212
         * @implNote The implementation of this method is effectively
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1213
         * equivalent to {@code ints(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1214
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1215
         * @return a stream of pseudorandomly chosen {@code int} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1216
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1217
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1218
        public IntStream ints() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1219
            return intStream(makeIntsSpliterator(0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1220
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1221
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1222
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1223
         * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code int}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1224
         * values from this generator and/or one split from it; each value conforms to the given origin
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1225
         * (inclusive) and bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1226
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1227
         * @param streamSize         the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1228
         * @param randomNumberOrigin the origin (inclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1229
         * @param randomNumberBound  the bound (exclusive) of each random value
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 pseudorandom {@code int} values, each with the given origin (inclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1232
         *         and bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1233
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1234
         * @throws IllegalArgumentException if {@code streamSize} is less than zero, or {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1235
         *                                  randomNumberOrigin} is greater than or equal to {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1236
         *                                  randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1237
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1238
        public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1239
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1240
            RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1241
            return intStream(makeIntsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1242
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1243
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1244
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1245
         * Returns an effectively unlimited stream of pseudorandom {@code int} values from this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1246
         * generator and/or one split from it; each value conforms to the given origin (inclusive) and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1247
         * bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1248
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1249
         * @param randomNumberOrigin the origin (inclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1250
         * @param randomNumberBound  the bound (exclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1251
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1252
         * @return a stream of pseudorandom {@code int} values, each with the given origin (inclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1253
         *         and bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1254
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1255
         * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1256
         *                                  {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1257
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1258
         * @implNote This method is implemented to be equivalent to {@code ints(Long.MAX_VALUE,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1259
         *         randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1260
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1261
        public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1262
            RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1263
            return intStream(makeIntsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1264
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1265
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1266
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1267
         * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code long}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1268
         * values from this generator and/or one split from it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1269
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1270
         * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1271
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1272
         * @return a stream of pseudorandom {@code long} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1273
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1274
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1275
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1276
        public LongStream longs(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1277
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1278
            return longStream(makeLongsSpliterator(0L, streamSize, Long.MAX_VALUE, 0L));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1279
        }
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
         * Returns an effectively unlimited stream of pseudorandom {@code long} values from this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1283
         * generator and/or one split from it.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1284
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1285
         * @return a stream of pseudorandom {@code long} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1286
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1287
         * @implNote This method is implemented to be equivalent to {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1288
         *         longs(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1289
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1290
        public LongStream longs() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1291
            return longStream(makeLongsSpliterator(0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1292
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1293
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1294
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1295
         * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code long}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1296
         * values from this generator and/or one split from it; each value conforms to the given origin
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1297
         * (inclusive) and bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1298
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1299
         * @param streamSize         the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1300
         * @param randomNumberOrigin the origin (inclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1301
         * @param randomNumberBound  the bound (exclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1302
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1303
         * @return a stream of pseudorandom {@code long} values, each with the given origin (inclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1304
         *         and bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1305
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1306
         * @throws IllegalArgumentException if {@code streamSize} is less than zero, or {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1307
         *                                  randomNumberOrigin} is greater than or equal to {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1308
         *                                  randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1309
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1310
        public LongStream longs(long streamSize, long randomNumberOrigin,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1311
                                 long randomNumberBound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1312
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1313
            RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1314
            return longStream(makeLongsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1315
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1316
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1317
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1318
         * Returns an effectively unlimited stream of pseudorandom {@code long} values from this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1319
         * generator and/or one split from it; each value conforms to the given origin (inclusive) and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1320
         * bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1321
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1322
         * @param randomNumberOrigin the origin (inclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1323
         * @param randomNumberBound  the bound (exclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1324
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1325
         * @return a stream of pseudorandom {@code long} values, each with the given origin (inclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1326
         *         and bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1327
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1328
         * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1329
         *                                  {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1330
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1331
         * @implNote This method is implemented to be equivalent to {@code longs(Long.MAX_VALUE,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1332
         *         randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1333
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1334
        public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1335
            RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1336
            return StreamSupport.longStream
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1337
                (makeLongsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1338
                 false);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1339
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1340
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1341
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1342
         * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code double}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1343
         * values from this generator and/or one split from it; each value is between zero (inclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1344
         * and one (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1345
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1346
         * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1347
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1348
         * @return a stream of {@code double} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1349
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1350
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1351
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1352
        public DoubleStream doubles(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1353
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1354
            return doubleStream(makeDoublesSpliterator(0L, streamSize, Double.MAX_VALUE, 0.0));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1355
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1356
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1357
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1358
         * Returns an effectively unlimited stream of pseudorandom {@code double} values from this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1359
         * generator and/or one split from it; each value is between zero (inclusive) and one
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1360
         * (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1361
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1362
         * @return a stream of pseudorandom {@code double} values
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1363
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1364
         * @implNote This method is implemented to be equivalent to {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1365
         *         doubles(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1366
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1367
        public DoubleStream doubles() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1368
            return doubleStream(makeDoublesSpliterator(0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1369
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1370
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1371
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1372
         * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code double}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1373
         * values from this generator and/or one split from it; each value conforms to the given origin
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1374
         * (inclusive) and bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1375
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1376
         * @param streamSize         the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1377
         * @param randomNumberOrigin the origin (inclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1378
         * @param randomNumberBound  the bound (exclusive) of each random value
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 stream of pseudorandom {@code double} values, each with the given origin
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1381
         *         (inclusive) and bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1382
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1383
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1384
         * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1385
         *                                  {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1386
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1387
        public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1388
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1389
            RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1390
            return doubleStream(makeDoublesSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1391
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1392
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1393
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1394
         * Returns an effectively unlimited stream of pseudorandom {@code double} values from this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1395
         * generator and/or one split from it; each value conforms to the given origin (inclusive) and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1396
         * bound (exclusive).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1397
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1398
         * @param randomNumberOrigin the origin (inclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1399
         * @param randomNumberBound  the bound (exclusive) of each random value
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1400
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1401
         * @return a stream of pseudorandom {@code double} values, each with the given origin
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1402
         *         (inclusive) and bound (exclusive)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1403
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1404
         * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1405
         *                                  {@code randomNumberBound}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1406
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1407
         * @implNote This method is implemented to be equivalent to {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1408
         *         doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1409
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1410
        public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1411
            RandomSupport.checkRange(randomNumberOrigin, randomNumberBound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1412
            return doubleStream(makeDoublesSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound));
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
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1416
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1417
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1418
     * This class provides much of the implementation of the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1419
     * {@link ArbitrarilyJumpableGenerator} interface, to minimize the effort
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1420
     * required to implement that 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
     * To implement a pseudorandom number generator, the programmer needs
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1423
     * only to extend this class and provide implementations for the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1424
     * methods {@link #nextInt()}, {@link #nextLong()}, {@link #copy()},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1425
     * {@link #jump(double)}, {@link #jumpPowerOfTwo(int)},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1426
     * {@link #defaultJumpDistance()}, and {@link #defaultLeapDistance()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1427
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1428
     * (If the pseudorandom number generator also has the ability to split,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1429
     * then the programmer may wish to consider instead extending
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1430
     * {@link AbstractSplittableGenerator}.)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1431
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1432
     * The programmer should generally provide at least three constructors:
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1433
     * one that takes no arguments, one that accepts a {@code long}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1434
     * seed value, and one that accepts an array of seed {@code byte} values.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1435
     * This class provides a public {@code initialSeed()} method that may
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1436
     * be useful in initializing some static state from which to derive
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1437
     * defaults seeds for use by the no-argument constructor.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1438
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1439
     * For the stream methods (such as {@code ints()} and {@code splits()}),
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1440
     * this class provides {@link Spliterator}-based implementations that
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1441
     * allow parallel execution when appropriate.  In this respect
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1442
     * {@link ArbitrarilyJumpableGenerator} differs from {@link JumpableGenerator},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1443
     * which provides very simple implementations that produce
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1444
     * sequential streams only.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1445
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1446
     * <p>An implementation of the {@link AbstractArbitrarilyJumpableGenerator} class
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1447
     * must provide concrete definitions for the methods {@code nextInt()},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1448
     * {@code nextLong}, {@code period()}, {@code copy()}, {@code jump(double)},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1449
     * {@code defaultJumpDistance()}, and {@code defaultLeapDistance()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1450
     * Default implementations are provided for all other methods.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1451
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1452
     * The documentation for each non-abstract method in this class
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1453
     * describes its implementation in detail. Each of these methods may
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1454
     * be overridden if the pseudorandom number generator being
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1455
     * implemented admits a more efficient implementation.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1456
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1457
     * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1458
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1459
    public abstract static class AbstractArbitrarilyJumpableGenerator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1460
        extends AbstractSpliteratorGenerator implements RandomGenerator.ArbitrarilyJumpableGenerator {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1461
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1462
        /*
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1463
         * Implementation Overview.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1464
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1465
         * This class provides most of the "user API" methods needed to satisfy
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1466
         * the interface ArbitrarilyJumpableGenerator.  Most of these methods
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1467
         * are in turn inherited from AbstractGenerator and the non-public class
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1468
         * AbstractSpliteratorGenerator; this file implements four versions of the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1469
         * jumps method and defines the spliterators necessary to support them.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1470
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1471
         * File organization: First the non-public methods needed by the class
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1472
         * AbstractSpliteratorGenerator, then the main public methods, followed by some
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1473
         * custom spliterator classes needed for stream methods.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1474
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1475
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1476
        // IllegalArgumentException messages
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1477
        static final String BadLogDistance  = "logDistance must be non-negative";
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1478
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1479
        // Methods required by class AbstractSpliteratorGenerator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1480
        public Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1481
            return new RandomIntsSpliterator(this, index, fence, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1482
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1483
        public Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1484
            return new RandomLongsSpliterator(this, index, fence, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1485
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1486
        public Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1487
            return new RandomDoublesSpliterator(this, index, fence, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1488
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1489
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1490
        // Similar methods used by this class
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1491
        Spliterator<RandomGenerator> makeJumpsSpliterator(long index, long fence, double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1492
            return new RandomJumpsSpliterator(this, index, fence, distance);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1493
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1494
        Spliterator<JumpableGenerator> makeLeapsSpliterator(long index, long fence, double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1495
            return new RandomLeapsSpliterator(this, index, fence, distance);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1496
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1497
        Spliterator<ArbitrarilyJumpableGenerator> makeArbitraryJumpsSpliterator(long index, long fence, double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1498
            return new RandomArbitraryJumpsSpliterator(this, index, fence, distance);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1499
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1500
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1501
        /* ---------------- public methods ---------------- */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1502
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1503
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1504
         * Returns a new generator whose internal state is an exact copy
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1505
         * of this generator (therefore their future behavior should be
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1506
         * identical if subjected to the same series of operations).
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1507
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1508
         * @return a new object that is a copy of this generator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1509
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1510
        public abstract AbstractArbitrarilyJumpableGenerator copy();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1511
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1512
        // Stream methods for jumping
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1513
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1514
        private static <T> Stream<T> stream(Spliterator<T> srng) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1515
            return StreamSupport.stream(srng, false);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1516
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1517
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1518
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1519
         * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1520
         * implements the {@link RandomGenerator} interface, produced by jumping copies of this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1521
         * generator by different integer multiples of the default jump distance.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1522
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1523
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1524
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1525
         * @implNote This method is implemented to be equivalent to {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1526
         *         jumps(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1527
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1528
        public Stream<RandomGenerator> jumps() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1529
            return stream(makeJumpsSpliterator(0L, Long.MAX_VALUE, defaultJumpDistance()));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1530
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1531
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1532
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1533
         * Returns a stream producing the given {@code streamSize} number of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1534
         * new pseudorandom number generators, each of which implements the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1535
         * {@link RandomGenerator} interface, produced by jumping copies of this generator
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1536
         * by different integer multiples of the default jump distance.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1537
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1538
         * @param streamSize the number of generators to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1539
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1540
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1541
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1542
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1543
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1544
        public Stream<RandomGenerator> jumps(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1545
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1546
            return stream(makeJumpsSpliterator(0L, streamSize, defaultJumpDistance()));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1547
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1548
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1549
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1550
         * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1551
         * implements the {@link RandomGenerator} interface, produced by jumping copies of this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1552
         * generator by different integer multiples of the specified jump distance.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1553
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1554
         * @param distance a distance to jump forward within the state cycle
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1555
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1556
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1557
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1558
         * @implNote This method is implemented to be equivalent to {@code
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1559
         *         jumps(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1560
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1561
        public Stream<ArbitrarilyJumpableGenerator> jumps(double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1562
            return stream(makeArbitraryJumpsSpliterator(0L, Long.MAX_VALUE, distance));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1563
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1564
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1565
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1566
         * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1567
         * generators, each of which implements the {@link RandomGenerator} interface, produced by
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1568
         * jumping copies of this generator by different integer multiples of the specified jump
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1569
         * distance.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1570
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1571
         * @param streamSize the number of generators to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1572
         * @param distance   a distance to jump forward within the state cycle
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1573
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1574
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1575
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1576
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1577
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1578
        public Stream<ArbitrarilyJumpableGenerator> jumps(long streamSize, double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1579
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1580
            return stream(makeArbitraryJumpsSpliterator(0L, streamSize, 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
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1583
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1584
         * Alter the state of this pseudorandom number generator so as to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1585
         * jump forward a very large, fixed distance (typically 2<sup>128</sup>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1586
         * or more) within its state cycle.  The distance used is that
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1587
         * returned by method {@code defaultLeapDistance()}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1588
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1589
        public void leap() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1590
            jump(defaultLeapDistance());
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
        // Stream methods for leaping
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1594
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1595
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1596
         * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1597
         * implements the {@link RandomGenerator} interface, produced by jumping copies of this
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1598
         * generator by different integer multiples of the default leap distance.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1599
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1600
         * @implNote This method is implemented to be equivalent to {@code leaps(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1601
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1602
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1603
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1604
        public Stream<JumpableGenerator> leaps() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1605
            return stream(makeLeapsSpliterator(0L, Long.MAX_VALUE, defaultLeapDistance()));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1606
        }
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
         * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1610
         * generators, each of which implements the {@link RandomGenerator} interface, produced by
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1611
         * jumping copies of this generator by different integer multiples of the default leap
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1612
         * distance.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1613
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1614
         * @param streamSize the number of generators to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1615
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1616
         * @return a stream of objects that implement the {@link RandomGenerator} interface
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1617
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1618
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1619
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1620
        public Stream<JumpableGenerator> leaps(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1621
            return stream(makeLeapsSpliterator(0L, streamSize, defaultLeapDistance()));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1622
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1623
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1624
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1625
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1626
         * Spliterator for int streams.  We multiplex the four int versions into one class by treating a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1627
         * bound less than origin as unbounded, and also by treating "infinite" as equivalent to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1628
         * {@code Long.MAX_VALUE}. For splits, we choose to override the method {@code trySplit()} to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1629
         * try to optimize execution speed: instead of dividing a range in half, it breaks off the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1630
         * largest possible chunk whose size is a power of two such that the remaining chunk is not
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1631
         * empty. In this way, the necessary jump distances will tend to be powers of two.  The long
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1632
         * and double versions of this class are identical except for types.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1633
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1634
        static class RandomIntsSpliterator extends RandomSupport.RandomSpliterator implements Spliterator.OfInt {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1635
            final ArbitrarilyJumpableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1636
            final int origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1637
            final int bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1638
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1639
            RandomIntsSpliterator(ArbitrarilyJumpableGenerator generatingGenerator, long index, long fence, int origin, int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1640
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1641
                this.origin = origin; this.bound = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1642
                this.generatingGenerator = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1643
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1644
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1645
            public Spliterator.OfInt trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1646
                long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1647
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1648
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1649
                ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1650
                return new RandomIntsSpliterator(r.copyAndJump((double)delta), i, m, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1651
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1652
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1653
            public boolean tryAdvance(IntConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1654
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1655
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1656
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1657
                    consumer.accept(RandomSupport.boundedNextInt(generatingGenerator, origin, bound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1658
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1659
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1660
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1661
                else return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1662
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1663
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1664
            public void forEachRemaining(IntConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1665
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1666
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1667
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1668
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1669
                    ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1670
                    int o = origin, b = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1671
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1672
                        consumer.accept(RandomSupport.boundedNextInt(r, o, b));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1673
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1674
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1675
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1676
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1677
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1678
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1679
         * Spliterator for long streams.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1680
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1681
        static class RandomLongsSpliterator extends RandomSupport.RandomSpliterator implements Spliterator.OfLong {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1682
            final ArbitrarilyJumpableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1683
            final long origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1684
            final long bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1685
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1686
            RandomLongsSpliterator(ArbitrarilyJumpableGenerator generatingGenerator, long index, long fence, long origin, long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1687
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1688
                this.generatingGenerator = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1689
                this.origin = origin; this.bound = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1690
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1691
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1692
            public Spliterator.OfLong trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1693
                long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1694
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1695
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1696
                ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1697
                return new RandomLongsSpliterator(r.copyAndJump((double)delta), i, m, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1698
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1699
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1700
            public boolean tryAdvance(LongConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1701
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1702
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1703
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1704
                    consumer.accept(RandomSupport.boundedNextLong(generatingGenerator, origin, bound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1705
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1706
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1707
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1708
                else return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1709
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1710
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1711
            public void forEachRemaining(LongConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1712
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1713
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1714
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1715
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1716
                    ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1717
                    long o = origin, b = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1718
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1719
                        consumer.accept(RandomSupport.boundedNextLong(r, o, b));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1720
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1721
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1722
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1723
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1724
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1725
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1726
         * Spliterator for double streams.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1727
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1728
        static class RandomDoublesSpliterator extends RandomSupport.RandomSpliterator implements Spliterator.OfDouble {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1729
            final ArbitrarilyJumpableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1730
            final double origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1731
            final double bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1732
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1733
            RandomDoublesSpliterator(ArbitrarilyJumpableGenerator generatingGenerator, long index, long fence, double origin, double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1734
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1735
                this.generatingGenerator = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1736
                this.origin = origin; this.bound = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1737
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1738
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1739
            public Spliterator.OfDouble trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1740
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1741
                long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1742
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1743
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1744
                ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1745
                return new RandomDoublesSpliterator(r.copyAndJump((double)delta), i, m, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1746
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1747
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1748
            public boolean tryAdvance(DoubleConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1749
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1750
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1751
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1752
                    consumer.accept(RandomSupport.boundedNextDouble(generatingGenerator, origin, bound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1753
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1754
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1755
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1756
                else return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1757
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1758
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1759
            public void forEachRemaining(DoubleConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1760
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1761
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1762
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1763
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1764
                    ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1765
                    double o = origin, b = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1766
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1767
                        consumer.accept(RandomSupport.boundedNextDouble(r, o, b));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1768
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1769
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1770
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1771
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1772
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1773
        // Spliterators for producing new generators by jumping or leaping.  The
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1774
        // complete implementation of each of these spliterators is right here.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1775
        // In the same manner as for the preceding spliterators, the method trySplit() is
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1776
        // coded to optimize execution speed: instead of dividing a range
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1777
        // in half, it breaks off the largest possible chunk whose
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1778
        // size is a power of two such that the remaining chunk is not
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1779
        // empty.  In this way, the necessary jump distances will tend to be
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1780
        // powers of two.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1781
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1782
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1783
         * Spliterator for stream of generators of type RandomGenerator produced by jumps.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1784
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1785
        static class RandomJumpsSpliterator extends RandomSupport.RandomSpliterator implements Spliterator<RandomGenerator> {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1786
            ArbitrarilyJumpableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1787
            final double distance;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1788
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1789
            RandomJumpsSpliterator(ArbitrarilyJumpableGenerator generatingGenerator, long index, long fence, double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1790
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1791
                this.generatingGenerator = generatingGenerator; this.distance = distance;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1792
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1793
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1794
            public Spliterator<RandomGenerator> trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1795
                long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1796
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1797
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1798
                ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1799
                // Because delta is a power of two, (distance * (double)delta) can always be computed exactly.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1800
                return new RandomJumpsSpliterator(r.copyAndJump(distance * (double)delta), i, m, distance);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1801
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1802
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1803
            public boolean tryAdvance(Consumer<? super RandomGenerator> consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1804
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1805
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1806
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1807
                    consumer.accept(generatingGenerator.copyAndJump(distance));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1808
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1809
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1810
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1811
                return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1812
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1813
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1814
            public void forEachRemaining(Consumer<? super RandomGenerator> consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1815
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1816
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1817
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1818
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1819
                    ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1820
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1821
                        consumer.accept(r.copyAndJump(distance));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1822
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1823
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1824
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1825
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1826
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1827
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1828
         * Spliterator for stream of generators of type RandomGenerator produced by leaps.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1829
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1830
        static class RandomLeapsSpliterator extends RandomSupport.RandomSpliterator implements Spliterator<JumpableGenerator> {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1831
            ArbitrarilyJumpableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1832
            final double distance;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1833
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1834
            RandomLeapsSpliterator(ArbitrarilyJumpableGenerator generatingGenerator, long index, long fence, double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1835
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1836
                this.generatingGenerator = generatingGenerator; this.distance = distance;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1837
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1838
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1839
            public Spliterator<JumpableGenerator> trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1840
                long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1841
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1842
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1843
                // Because delta is a power of two, (distance * (double)delta) can always be computed exactly.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1844
                return new RandomLeapsSpliterator(generatingGenerator.copyAndJump(distance * (double)delta), i, m, distance);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1845
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1846
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1847
            public boolean tryAdvance(Consumer<? super JumpableGenerator> consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1848
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1849
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1850
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1851
                    consumer.accept(generatingGenerator.copyAndJump(distance));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1852
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1853
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1854
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1855
                return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1856
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1857
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1858
            public void forEachRemaining(Consumer<? super JumpableGenerator> consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1859
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1860
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1861
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1862
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1863
                    ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1864
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1865
                        consumer.accept(r.copyAndJump(distance));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1866
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1867
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1868
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1869
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1870
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1871
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1872
         * Spliterator for stream of generators of type RandomGenerator produced by arbitrary jumps.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1873
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1874
        static class RandomArbitraryJumpsSpliterator extends RandomSupport.RandomSpliterator implements Spliterator<ArbitrarilyJumpableGenerator> {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1875
            ArbitrarilyJumpableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1876
            final double distance;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1877
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1878
            RandomArbitraryJumpsSpliterator(ArbitrarilyJumpableGenerator generatingGenerator, long index, long fence, double distance) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1879
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1880
                this.generatingGenerator = generatingGenerator; this.distance = distance;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1881
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1882
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1883
            public Spliterator<ArbitrarilyJumpableGenerator> trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1884
                long i = index, delta = Long.highestOneBit((fence - i) - 1), m = i + delta;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1885
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1886
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1887
                // Because delta is a power of two, (distance * (double)delta) can always be computed exactly.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1888
                return new RandomArbitraryJumpsSpliterator(generatingGenerator.copyAndJump(distance * (double)delta), i, m, distance);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1889
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1890
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1891
            public boolean tryAdvance(Consumer<? super ArbitrarilyJumpableGenerator> consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1892
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1893
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1894
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1895
                    consumer.accept(generatingGenerator.copyAndJump(distance));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1896
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1897
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1898
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1899
                return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1900
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1901
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1902
            public void forEachRemaining(Consumer<? super ArbitrarilyJumpableGenerator> consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1903
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1904
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1905
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1906
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1907
                    ArbitrarilyJumpableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1908
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1909
                        consumer.accept(r.copyAndJump(distance));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1910
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1911
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1912
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1913
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1914
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1915
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1916
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1917
    /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1918
     * This class provides much of the implementation of the {@link SplittableGenerator} interface, to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1919
     * minimize the effort required to implement this interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1920
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1921
     * To implement a pseudorandom number generator, the programmer needs only to extend this class and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1922
     * provide implementations for the methods {@code nextInt()}, {@code nextLong()}, {@code period()},
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1923
     * and {@code split(SplittableGenerator)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1924
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1925
     * (If the pseudorandom number generator also has the ability to jump, then the programmer may wish
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1926
     * to consider instead extending the class {@link ArbitrarilyJumpableGenerator}.  But if the pseudorandom
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1927
     * number generator furthermore has the ability to jump an arbitrary specified distance, then the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1928
     * programmer may wish to consider instead extending the class {@link
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1929
     * AbstractArbitrarilyJumpableGenerator}.)
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1930
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1931
     * The programmer should generally provide at least three constructors: one that takes no arguments,
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1932
     * one that accepts a {@code long} seed value, and one that accepts an array of seed {@code byte}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1933
     * values. This class provides a public {@code initialSeed()} method that may be useful in
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1934
     * initializing some static state from which to derive defaults seeds for use by the no-argument
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1935
     * constructor.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1936
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1937
     * For the stream methods (such as {@code ints()} and {@code splits()}), this class provides {@link
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1938
     * Spliterator} based implementations that allow parallel execution when appropriate.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1939
     * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1940
     * The documentation for each non-abstract method in this class describes its implementation in
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1941
     * detail. Each of these methods may be overridden if the pseudorandom number generator being
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1942
     * implemented admits a more efficient implementation.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1943
     *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1944
     * @since 14
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1945
     */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1946
    public abstract static class AbstractSplittableGenerator extends AbstractSpliteratorGenerator implements SplittableGenerator {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1947
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1948
        /*
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1949
         * Implementation Overview.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1950
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1951
         * This class provides most of the "user API" methods needed to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1952
         * satisfy the interface JumpableGenerator.  Most of these methods
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1953
         * are in turn inherited from AbstractGenerator and the non-public class
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1954
         * AbstractSpliteratorGenerator; this file implements two versions of the
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1955
         * splits method and defines the spliterators necessary to support
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1956
         * them.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1957
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1958
         * The abstract split() method from interface SplittableGenerator is redeclared
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1959
         * here so as to narrow the return type to AbstractSplittableGenerator.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1960
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1961
         * File organization: First the non-public methods needed by the class
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1962
         * AbstractSpliteratorGenerator, then the main public methods, followed by some
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1963
         * custom spliterator classes.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1964
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1965
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1966
        public Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1967
            return new RandomIntsSpliterator(this, index, fence, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1968
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1969
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1970
        public Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1971
            return new RandomLongsSpliterator(this, index, fence, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1972
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1973
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1974
        public Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1975
            return new RandomDoublesSpliterator(this, index, fence, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1976
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1977
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1978
        Spliterator<SplittableGenerator> makeSplitsSpliterator(long index, long fence, SplittableGenerator source) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1979
            return new RandomSplitsSpliterator(source, index, fence, this);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1980
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1981
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1982
        /* ---------------- public methods ---------------- */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1983
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1984
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1985
         * Implements the @code{split()} method as {@code this.split(this) }.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1986
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1987
         * @return the new {@link AbstractSplittableGenerator} instance
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1988
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1989
        public SplittableGenerator split() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1990
            return this.split(this);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1991
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1992
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1993
        // Stream methods for splittings
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1994
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1995
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1996
         * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1997
         * implements the {@link SplittableGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1998
         * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  1999
         * This pseudorandom number generator provides the entropy used to seed the new ones.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2000
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2001
         * @return a stream of {@link SplittableGenerator} objects
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2002
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2003
         * @implNote This method is implemented to be equivalent to {@code splits(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2004
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2005
        public Stream<SplittableGenerator> splits() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2006
            return this.splits(Long.MAX_VALUE, this);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2007
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2008
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2009
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2010
         * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2011
         * generators, each of which implements the {@link SplittableGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2012
         * <p>
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2013
         * This pseudorandom number generator provides the entropy used to seed the new ones.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2014
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2015
         * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2016
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2017
         * @return a stream of {@link SplittableGenerator} objects
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2018
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2019
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2020
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2021
        public Stream<SplittableGenerator> splits(long streamSize) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2022
            return this.splits(streamSize, this);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2023
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2024
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2025
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2026
         * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2027
         * implements the {@link SplittableGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2028
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2029
         * @param source a {@link SplittableGenerator} instance to be used instead of this one as a source of
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2030
         *               pseudorandom bits used to initialize the state of the new ones.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2031
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2032
         * @return a stream of {@link SplittableGenerator} objects
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2033
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2034
         * @implNote This method is implemented to be equivalent to {@code splits(Long.MAX_VALUE)}.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2035
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2036
        public Stream<SplittableGenerator> splits(SplittableGenerator source) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2037
            return this.splits(Long.MAX_VALUE, source);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2038
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2039
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2040
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2041
         * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2042
         * generators, each of which implements the {@link SplittableGenerator} interface.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2043
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2044
         * @param streamSize the number of values to generate
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2045
         * @param source     a {@link SplittableGenerator} instance to be used instead of this one as a source
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2046
         *                   of pseudorandom bits used to initialize the state of the new ones.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2047
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2048
         * @return a stream of {@link SplittableGenerator} objects
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2049
         *
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2050
         * @throws IllegalArgumentException if {@code streamSize} is less than zero
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2051
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2052
        public Stream<SplittableGenerator> splits(long streamSize, SplittableGenerator source) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2053
            RandomSupport.checkStreamSize(streamSize);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2054
            return StreamSupport.stream(makeSplitsSpliterator(0L, streamSize, source), false);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2055
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2056
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2057
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2058
         * Spliterator for int streams.  We multiplex the four int versions into one class by treating a
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2059
         * bound less than origin as unbounded, and also by treating "infinite" as equivalent to
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2060
         * {@code Long.MAX_VALUE}. For splits, it uses the standard divide-by-two approach. The long and
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2061
         * double versions of this class are identical except for types.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2062
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2063
        static class RandomIntsSpliterator extends RandomSupport.RandomSpliterator implements Spliterator.OfInt {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2064
            final SplittableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2065
            final int origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2066
            final int bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2067
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2068
            RandomIntsSpliterator(SplittableGenerator generatingGenerator, long index, long fence, int origin, int bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2069
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2070
                this.generatingGenerator = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2071
                this.origin = origin; this.bound = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2072
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2073
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2074
            public Spliterator.OfInt trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2075
                long i = index, m = (i + fence) >>> 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2076
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2077
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2078
                return new RandomIntsSpliterator(generatingGenerator.split(), i, m, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2079
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2080
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2081
            public boolean tryAdvance(IntConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2082
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2083
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2084
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2085
                    consumer.accept(RandomSupport.boundedNextInt(generatingGenerator, origin, bound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2086
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2087
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2088
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2089
                else return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2090
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2091
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2092
            public void forEachRemaining(IntConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2093
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2094
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2095
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2096
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2097
                    RandomGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2098
                    int o = origin, b = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2099
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2100
                        consumer.accept(RandomSupport.boundedNextInt(r, o, b));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2101
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2102
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2103
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2104
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2105
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2106
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2107
         * Spliterator for long streams.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2108
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2109
        static class RandomLongsSpliterator extends RandomSupport.RandomSpliterator implements Spliterator.OfLong {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2110
            final SplittableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2111
            final long origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2112
            final long bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2113
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2114
            RandomLongsSpliterator(SplittableGenerator generatingGenerator, long index, long fence, long origin, long bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2115
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2116
                this.generatingGenerator = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2117
                this.origin = origin; this.bound = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2118
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2119
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2120
            public Spliterator.OfLong trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2121
                long i = index, m = (i + fence) >>> 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2122
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2123
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2124
                return new RandomLongsSpliterator(generatingGenerator.split(), i, m, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2125
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2126
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2127
            public boolean tryAdvance(LongConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2128
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2129
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2130
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2131
                    consumer.accept(RandomSupport.boundedNextLong(generatingGenerator, origin, bound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2132
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2133
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2134
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2135
                else return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2136
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2137
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2138
            public void forEachRemaining(LongConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2139
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2140
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2141
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2142
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2143
                    RandomGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2144
                    long o = origin, b = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2145
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2146
                        consumer.accept(RandomSupport.boundedNextLong(r, o, b));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2147
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2148
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2149
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2150
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2151
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2152
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2153
         * Spliterator for double streams.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2154
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2155
        static class RandomDoublesSpliterator extends RandomSupport.RandomSpliterator implements Spliterator.OfDouble {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2156
            final SplittableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2157
            final double origin;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2158
            final double bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2159
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2160
            RandomDoublesSpliterator(SplittableGenerator generatingGenerator, long index, long fence, double origin, double bound) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2161
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2162
                this.generatingGenerator = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2163
                this.origin = origin; this.bound = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2164
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2165
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2166
            public Spliterator.OfDouble trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2167
                long i = index, m = (i + fence) >>> 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2168
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2169
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2170
                return new RandomDoublesSpliterator(generatingGenerator.split(), i, m, origin, bound);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2171
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2172
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2173
            public boolean tryAdvance(DoubleConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2174
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2175
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2176
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2177
                    consumer.accept(RandomSupport.boundedNextDouble(generatingGenerator, origin, bound));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2178
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2179
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2180
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2181
                else return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2182
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2183
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2184
            public void forEachRemaining(DoubleConsumer consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2185
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2186
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2187
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2188
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2189
                    RandomGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2190
                    double o = origin, b = bound;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2191
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2192
                        consumer.accept(RandomSupport.boundedNextDouble(r, o, b));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2193
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2194
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2195
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2196
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2197
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2198
        /**
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2199
         * Spliterator for stream of generators of type SplittableGenerator.  We multiplex the two
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2200
         * versions into one class by treating "infinite" as equivalent to Long.MAX_VALUE.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2201
         * For splits, it uses the standard divide-by-two approach.
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2202
         */
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2203
        static class RandomSplitsSpliterator extends RandomSupport.RandomSpliterator implements Spliterator<SplittableGenerator> {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2204
            final SplittableGenerator generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2205
            final SplittableGenerator constructingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2206
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2207
            RandomSplitsSpliterator(SplittableGenerator generatingGenerator, long index, long fence, SplittableGenerator constructingGenerator) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2208
                super(index, fence);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2209
                this.generatingGenerator = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2210
                this.constructingGenerator = constructingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2211
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2212
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2213
            public Spliterator<SplittableGenerator> trySplit() {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2214
                long i = index, m = (i + fence) >>> 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2215
                if (m <= i) return null;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2216
                index = m;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2217
                return new RandomSplitsSpliterator(generatingGenerator.split(), i, m, constructingGenerator);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2218
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2219
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2220
            public boolean tryAdvance(Consumer<? super SplittableGenerator> consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2221
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2222
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2223
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2224
                    consumer.accept(constructingGenerator.split(generatingGenerator));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2225
                    index = i + 1;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2226
                    return true;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2227
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2228
                else return false;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2229
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2230
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2231
            public void forEachRemaining(Consumer<? super SplittableGenerator> consumer) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2232
                if (consumer == null) throw new NullPointerException();
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2233
                long i = index, f = fence;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2234
                if (i < f) {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2235
                    index = f;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2236
                    SplittableGenerator c = constructingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2237
                    SplittableGenerator r = generatingGenerator;
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2238
                    do {
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2239
                        consumer.accept(c.split(r));
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2240
                    } while (++i < f);
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2241
                }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2242
            }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2243
        }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2244
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2245
    }
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2246
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2247
}
56cbdc3ea079 Reorganize the abstract and interface classes.
jlaskey
parents:
diff changeset
  2248