jdk/src/java.base/share/classes/java/util/SplittableRandom.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 27797 7734f206f415
child 32991 b27c76b82713
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
     1
/*
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
     4
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    10
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    15
 * accompanied this code).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    16
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    20
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    23
 * questions.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    24
 */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    25
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    26
package java.util;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    27
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    28
import java.util.concurrent.atomic.AtomicLong;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    29
import java.util.function.IntConsumer;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    30
import java.util.function.LongConsumer;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    31
import java.util.function.DoubleConsumer;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    32
import java.util.stream.StreamSupport;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    33
import java.util.stream.IntStream;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    34
import java.util.stream.LongStream;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    35
import java.util.stream.DoubleStream;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    36
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    37
/**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    38
 * A generator of uniform pseudorandom values applicable for use in
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    39
 * (among other contexts) isolated parallel computations that may
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    40
 * generate subtasks. Class {@code SplittableRandom} supports methods for
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    41
 * producing pseudorandom numbers of type {@code int}, {@code long},
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    42
 * and {@code double} with similar usages as for class
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    43
 * {@link java.util.Random} but differs in the following ways:
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    44
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    45
 * <ul>
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    46
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    47
 * <li>Series of generated values pass the DieHarder suite testing
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    48
 * independence and uniformity properties of random number generators.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    49
 * (Most recently validated with <a
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    50
 * href="http://www.phy.duke.edu/~rgb/General/dieharder.php"> version
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    51
 * 3.31.1</a>.) These tests validate only the methods for certain
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    52
 * types and ranges, but similar properties are expected to hold, at
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    53
 * least approximately, for others as well. The <em>period</em>
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    54
 * (length of any series of generated values before it repeats) is at
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    55
 * least 2<sup>64</sup>. </li>
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    56
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    57
 * <li> Method {@link #split} constructs and returns a new
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    58
 * SplittableRandom instance that shares no mutable state with the
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    59
 * current instance. However, with very high probability, the
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    60
 * values collectively generated by the two objects have the same
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    61
 * statistical properties as if the same quantity of values were
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    62
 * generated by a single thread using a single {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    63
 * SplittableRandom} object.  </li>
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    64
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    65
 * <li>Instances of SplittableRandom are <em>not</em> thread-safe.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    66
 * They are designed to be split, not shared, across threads. For
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    67
 * example, a {@link java.util.concurrent.ForkJoinTask
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    68
 * fork/join-style} computation using random numbers might include a
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    69
 * construction of the form {@code new
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    70
 * Subtask(aSplittableRandom.split()).fork()}.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    71
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    72
 * <li>This class provides additional methods for generating random
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    73
 * streams, that employ the above techniques when used in {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    74
 * stream.parallel()} mode.</li>
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    75
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    76
 * </ul>
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    77
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    78
 * <p>Instances of {@code SplittableRandom} are not cryptographically
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    79
 * secure.  Consider instead using {@link java.security.SecureRandom}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    80
 * in security-sensitive applications. Additionally,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    81
 * default-constructed instances do not use a cryptographically random
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    82
 * seed unless the {@linkplain System#getProperty system property}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    83
 * {@code java.util.secureRandomSeed} is set to {@code true}.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    84
 *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    85
 * @author  Guy Steele
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    86
 * @author  Doug Lea
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    87
 * @since   1.8
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    88
 */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    89
public final class SplittableRandom {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    90
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    91
    /*
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    92
     * Implementation Overview.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    93
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    94
     * This algorithm was inspired by the "DotMix" algorithm by
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    95
     * Leiserson, Schardl, and Sukha "Deterministic Parallel
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    96
     * Random-Number Generation for Dynamic-Multithreading Platforms",
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    97
     * PPoPP 2012, as well as those in "Parallel random numbers: as
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    98
     * easy as 1, 2, 3" by Salmon, Morae, Dror, and Shaw, SC 2011.  It
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
    99
     * differs mainly in simplifying and cheapening operations.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   100
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   101
     * The primary update step (method nextSeed()) is to add a
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   102
     * constant ("gamma") to the current (64 bit) seed, forming a
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   103
     * simple sequence.  The seed and the gamma values for any two
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   104
     * SplittableRandom instances are highly likely to be different.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   105
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   106
     * Methods nextLong, nextInt, and derivatives do not return the
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   107
     * sequence (seed) values, but instead a hash-like bit-mix of
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   108
     * their bits, producing more independently distributed sequences.
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   109
     * For nextLong, the mix64 function is based on David Stafford's
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   110
     * (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   111
     * "Mix13" variant of the "64-bit finalizer" function in Austin
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   112
     * Appleby's MurmurHash3 algorithm (see
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   113
     * http://code.google.com/p/smhasher/wiki/MurmurHash3). The mix32
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   114
     * function is based on Stafford's Mix04 mix function, but returns
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   115
     * the upper 32 bits cast as int.
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   116
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   117
     * The split operation uses the current generator to form the seed
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   118
     * and gamma for another SplittableRandom.  To conservatively
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   119
     * avoid potential correlations between seed and value generation,
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   120
     * gamma selection (method mixGamma) uses different
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   121
     * (Murmurhash3's) mix constants.  To avoid potential weaknesses
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   122
     * in bit-mixing transformations, we restrict gammas to odd values
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   123
     * with at least 24 0-1 or 1-0 bit transitions.  Rather than
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   124
     * rejecting candidates with too few or too many bits set, method
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   125
     * mixGamma flips some bits (which has the effect of mapping at
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   126
     * most 4 to any given gamma value).  This reduces the effective
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   127
     * set of 64bit odd gamma values by about 2%, and serves as an
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   128
     * automated screening for sequence constant selection that is
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   129
     * left as an empirical decision in some other hashing and crypto
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   130
     * algorithms.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   131
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   132
     * The resulting generator thus transforms a sequence in which
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   133
     * (typically) many bits change on each step, with an inexpensive
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   134
     * mixer with good (but less than cryptographically secure)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   135
     * avalanching.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   136
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   137
     * The default (no-argument) constructor, in essence, invokes
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   138
     * split() for a common "defaultGen" SplittableRandom.  Unlike
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   139
     * other cases, this split must be performed in a thread-safe
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   140
     * manner, so we use an AtomicLong to represent the seed rather
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   141
     * than use an explicit SplittableRandom. To bootstrap the
27797
7734f206f415 8066397: Remove network-related seed initialization code in ThreadLocal/SplittableRandom
psandoz
parents: 25859
diff changeset
   142
     * defaultGen, we start off using a seed based on current time
7734f206f415 8066397: Remove network-related seed initialization code in ThreadLocal/SplittableRandom
psandoz
parents: 25859
diff changeset
   143
     * unless the java.util.secureRandomSeed property is set. This
7734f206f415 8066397: Remove network-related seed initialization code in ThreadLocal/SplittableRandom
psandoz
parents: 25859
diff changeset
   144
     * serves as a slimmed-down (and insecure) variant of SecureRandom
7734f206f415 8066397: Remove network-related seed initialization code in ThreadLocal/SplittableRandom
psandoz
parents: 25859
diff changeset
   145
     * that also avoids stalls that may occur when using /dev/random.
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   146
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   147
     * It is a relatively simple matter to apply the basic design here
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   148
     * to use 128 bit seeds. However, emulating 128bit arithmetic and
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   149
     * carrying around twice the state add more overhead than appears
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   150
     * warranted for current usages.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   151
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   152
     * File organization: First the non-public methods that constitute
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   153
     * the main algorithm, then the main public methods, followed by
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   154
     * some custom spliterator classes needed for stream methods.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   155
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   156
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   157
    /**
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   158
     * The golden ratio scaled to 64bits, used as the initial gamma
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   159
     * value for (unsplit) SplittableRandoms.
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   160
     */
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   161
    private static final long GOLDEN_GAMMA = 0x9e3779b97f4a7c15L;
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   162
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   163
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   164
     * The least non-zero value returned by nextDouble(). This value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   165
     * is scaled by a random value of 53 bits to produce a result.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   166
     */
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   167
    private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53);
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   168
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   169
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   170
     * The seed. Updated only via method nextSeed.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   171
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   172
    private long seed;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   173
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   174
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   175
     * The step value.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   176
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   177
    private final long gamma;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   178
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   179
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   180
     * Internal constructor used by all others except default constructor.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   181
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   182
    private SplittableRandom(long seed, long gamma) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   183
        this.seed = seed;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   184
        this.gamma = gamma;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   185
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   186
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   187
    /**
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   188
     * Computes Stafford variant 13 of 64bit mix function.
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   189
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   190
    private static long mix64(long z) {
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   191
        z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   192
        z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL;
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   193
        return z ^ (z >>> 31);
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   194
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   195
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   196
    /**
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   197
     * Returns the 32 high bits of Stafford variant 4 mix64 function as int.
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   198
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   199
    private static int mix32(long z) {
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   200
        z = (z ^ (z >>> 33)) * 0x62a9d9ed799705f5L;
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   201
        return (int)(((z ^ (z >>> 28)) * 0xcb24d0a5c88c35b3L) >>> 32);
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   202
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   203
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   204
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   205
     * Returns the gamma value to use for a new split instance.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   206
     */
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   207
    private static long mixGamma(long z) {
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   208
        z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   209
        z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   210
        z = (z ^ (z >>> 33)) | 1L;                  // force to be odd
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   211
        int n = Long.bitCount(z ^ (z >>> 1));       // ensure enough transitions
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   212
        return (n < 24) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   213
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   214
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   215
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   216
     * Adds gamma to seed.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   217
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   218
    private long nextSeed() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   219
        return seed += gamma;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   220
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   221
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   222
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   223
     * The seed generator for default constructors.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   224
     */
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   225
    private static final AtomicLong defaultGen = new AtomicLong(initialSeed());
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   226
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   227
    private static long initialSeed() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   228
        String pp = java.security.AccessController.doPrivileged(
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   229
                new sun.security.action.GetPropertyAction(
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   230
                        "java.util.secureRandomSeed"));
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   231
        if (pp != null && pp.equalsIgnoreCase("true")) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   232
            byte[] seedBytes = java.security.SecureRandom.getSeed(8);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   233
            long s = (long)(seedBytes[0]) & 0xffL;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   234
            for (int i = 1; i < 8; ++i)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   235
                s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   236
            return s;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   237
        }
27797
7734f206f415 8066397: Remove network-related seed initialization code in ThreadLocal/SplittableRandom
psandoz
parents: 25859
diff changeset
   238
        return (mix64(System.currentTimeMillis()) ^
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   239
                mix64(System.nanoTime()));
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   240
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   241
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   242
    // IllegalArgumentException messages
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   243
    static final String BadBound = "bound must be positive";
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   244
    static final String BadRange = "bound must be greater than origin";
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   245
    static final String BadSize  = "size must be non-negative";
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   246
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   247
    /*
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   248
     * Internal versions of nextX methods used by streams, as well as
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   249
     * the public nextX(origin, bound) methods.  These exist mainly to
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   250
     * avoid the need for multiple versions of stream spliterators
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   251
     * across the different exported forms of streams.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   252
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   253
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   254
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   255
     * The form of nextLong used by LongStream Spliterators.  If
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   256
     * origin is greater than bound, acts as unbounded form of
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   257
     * nextLong, else as bounded form.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   258
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   259
     * @param origin the least value, unless greater than bound
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   260
     * @param bound the upper bound (exclusive), must not equal origin
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   261
     * @return a pseudorandom value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   262
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   263
    final long internalNextLong(long origin, long bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   264
        /*
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   265
         * Four Cases:
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   266
         *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   267
         * 1. If the arguments indicate unbounded form, act as
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   268
         * nextLong().
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   269
         *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   270
         * 2. If the range is an exact power of two, apply the
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   271
         * associated bit mask.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   272
         *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   273
         * 3. If the range is positive, loop to avoid potential bias
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   274
         * when the implicit nextLong() bound (2<sup>64</sup>) is not
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   275
         * evenly divisible by the range. The loop rejects candidates
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   276
         * computed from otherwise over-represented values.  The
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   277
         * expected number of iterations under an ideal generator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   278
         * varies from 1 to 2, depending on the bound. The loop itself
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   279
         * takes an unlovable form. Because the first candidate is
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   280
         * already available, we need a break-in-the-middle
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   281
         * construction, which is concisely but cryptically performed
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   282
         * within the while-condition of a body-less for loop.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   283
         *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   284
         * 4. Otherwise, the range cannot be represented as a positive
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   285
         * long.  The loop repeatedly generates unbounded longs until
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   286
         * obtaining a candidate meeting constraints (with an expected
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   287
         * number of iterations of less than two).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   288
         */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   289
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   290
        long r = mix64(nextSeed());
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   291
        if (origin < bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   292
            long n = bound - origin, m = n - 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   293
            if ((n & m) == 0L)  // power of two
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   294
                r = (r & m) + origin;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   295
            else if (n > 0L) {  // reject over-represented candidates
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   296
                for (long u = r >>> 1;            // ensure nonnegative
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   297
                     u + m - (r = u % n) < 0L;    // rejection check
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   298
                     u = mix64(nextSeed()) >>> 1) // retry
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   299
                    ;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   300
                r += origin;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   301
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   302
            else {              // range not representable as long
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   303
                while (r < origin || r >= bound)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   304
                    r = mix64(nextSeed());
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   305
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   306
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   307
        return r;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   308
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   309
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   310
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   311
     * The form of nextInt used by IntStream Spliterators.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   312
     * Exactly the same as long version, except for types.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   313
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   314
     * @param origin the least value, unless greater than bound
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   315
     * @param bound the upper bound (exclusive), must not equal origin
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   316
     * @return a pseudorandom value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   317
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   318
    final int internalNextInt(int origin, int bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   319
        int r = mix32(nextSeed());
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   320
        if (origin < bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   321
            int n = bound - origin, m = n - 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   322
            if ((n & m) == 0)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   323
                r = (r & m) + origin;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   324
            else if (n > 0) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   325
                for (int u = r >>> 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   326
                     u + m - (r = u % n) < 0;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   327
                     u = mix32(nextSeed()) >>> 1)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   328
                    ;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   329
                r += origin;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   330
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   331
            else {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   332
                while (r < origin || r >= bound)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   333
                    r = mix32(nextSeed());
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   334
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   335
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   336
        return r;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   337
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   338
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   339
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   340
     * The form of nextDouble used by DoubleStream Spliterators.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   341
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   342
     * @param origin the least value, unless greater than bound
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   343
     * @param bound the upper bound (exclusive), must not equal origin
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   344
     * @return a pseudorandom value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   345
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   346
    final double internalNextDouble(double origin, double bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   347
        double r = (nextLong() >>> 11) * DOUBLE_UNIT;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   348
        if (origin < bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   349
            r = r * (bound - origin) + origin;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   350
            if (r >= bound) // correct for rounding
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   351
                r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   352
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   353
        return r;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   354
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   355
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   356
    /* ---------------- public methods ---------------- */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   357
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   358
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   359
     * Creates a new SplittableRandom instance using the specified
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   360
     * initial seed. SplittableRandom instances created with the same
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   361
     * seed in the same program generate identical sequences of values.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   362
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   363
     * @param seed the initial seed
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   364
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   365
    public SplittableRandom(long seed) {
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   366
        this(seed, GOLDEN_GAMMA);
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   367
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   368
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   369
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   370
     * Creates a new SplittableRandom instance that is likely to
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   371
     * generate sequences of values that are statistically independent
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   372
     * of those of any other instances in the current program; and
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   373
     * may, and typically does, vary across program invocations.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   374
     */
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   375
    public SplittableRandom() { // emulate defaultGen.split()
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   376
        long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA);
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   377
        this.seed = mix64(s);
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   378
        this.gamma = mixGamma(s + GOLDEN_GAMMA);
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   379
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   380
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   381
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   382
     * Constructs and returns a new SplittableRandom instance that
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   383
     * shares no mutable state with this instance. However, with very
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   384
     * high probability, the set of values collectively generated by
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   385
     * the two objects has the same statistical properties as if the
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   386
     * same quantity of values were generated by a single thread using
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   387
     * a single SplittableRandom object.  Either or both of the two
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   388
     * objects may be further split using the {@code split()} method,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   389
     * and the same expected statistical properties apply to the
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   390
     * entire set of generators constructed by such recursive
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   391
     * splitting.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   392
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   393
     * @return the new SplittableRandom instance
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   394
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   395
    public SplittableRandom split() {
20540
1376a380b9ba 8025136: SplittableRandom enchancements
psandoz
parents: 20188
diff changeset
   396
        return new SplittableRandom(nextLong(), mixGamma(nextSeed()));
19592
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   397
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   398
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   399
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   400
     * Returns a pseudorandom {@code int} value.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   401
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   402
     * @return a pseudorandom {@code int} value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   403
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   404
    public int nextInt() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   405
        return mix32(nextSeed());
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   406
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   407
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   408
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   409
     * Returns a pseudorandom {@code int} value between zero (inclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   410
     * and the specified bound (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   411
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   412
     * @param bound the upper bound (exclusive).  Must be positive.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   413
     * @return a pseudorandom {@code int} value between zero
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   414
     *         (inclusive) and the bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   415
     * @throws IllegalArgumentException if {@code bound} is not positive
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   416
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   417
    public int nextInt(int bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   418
        if (bound <= 0)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   419
            throw new IllegalArgumentException(BadBound);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   420
        // Specialize internalNextInt for origin 0
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   421
        int r = mix32(nextSeed());
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   422
        int m = bound - 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   423
        if ((bound & m) == 0) // power of two
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   424
            r &= m;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   425
        else { // reject over-represented candidates
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   426
            for (int u = r >>> 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   427
                 u + m - (r = u % bound) < 0;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   428
                 u = mix32(nextSeed()) >>> 1)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   429
                ;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   430
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   431
        return r;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   432
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   433
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   434
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   435
     * Returns a pseudorandom {@code int} value between the specified
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   436
     * origin (inclusive) and the specified bound (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   437
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   438
     * @param origin the least value returned
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   439
     * @param bound the upper bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   440
     * @return a pseudorandom {@code int} value between the origin
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   441
     *         (inclusive) and the bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   442
     * @throws IllegalArgumentException if {@code origin} is greater than
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   443
     *         or equal to {@code bound}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   444
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   445
    public int nextInt(int origin, int bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   446
        if (origin >= bound)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   447
            throw new IllegalArgumentException(BadRange);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   448
        return internalNextInt(origin, bound);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   449
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   450
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   451
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   452
     * Returns a pseudorandom {@code long} value.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   453
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   454
     * @return a pseudorandom {@code long} value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   455
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   456
    public long nextLong() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   457
        return mix64(nextSeed());
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   458
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   459
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   460
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   461
     * Returns a pseudorandom {@code long} value between zero (inclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   462
     * and the specified bound (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   463
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   464
     * @param bound the upper bound (exclusive).  Must be positive.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   465
     * @return a pseudorandom {@code long} value between zero
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   466
     *         (inclusive) and the bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   467
     * @throws IllegalArgumentException if {@code bound} is not positive
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   468
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   469
    public long nextLong(long bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   470
        if (bound <= 0)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   471
            throw new IllegalArgumentException(BadBound);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   472
        // Specialize internalNextLong for origin 0
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   473
        long r = mix64(nextSeed());
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   474
        long m = bound - 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   475
        if ((bound & m) == 0L) // power of two
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   476
            r &= m;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   477
        else { // reject over-represented candidates
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   478
            for (long u = r >>> 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   479
                 u + m - (r = u % bound) < 0L;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   480
                 u = mix64(nextSeed()) >>> 1)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   481
                ;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   482
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   483
        return r;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   484
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   485
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   486
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   487
     * Returns a pseudorandom {@code long} value between the specified
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   488
     * origin (inclusive) and the specified bound (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   489
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   490
     * @param origin the least value returned
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   491
     * @param bound the upper bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   492
     * @return a pseudorandom {@code long} value between the origin
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   493
     *         (inclusive) and the bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   494
     * @throws IllegalArgumentException if {@code origin} is greater than
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   495
     *         or equal to {@code bound}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   496
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   497
    public long nextLong(long origin, long bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   498
        if (origin >= bound)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   499
            throw new IllegalArgumentException(BadRange);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   500
        return internalNextLong(origin, bound);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   501
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   502
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   503
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   504
     * Returns a pseudorandom {@code double} value between zero
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   505
     * (inclusive) and one (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   506
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   507
     * @return a pseudorandom {@code double} value between zero
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   508
     *         (inclusive) and one (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   509
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   510
    public double nextDouble() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   511
        return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   512
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   513
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   514
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   515
     * Returns a pseudorandom {@code double} value between 0.0
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   516
     * (inclusive) and the specified bound (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   517
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   518
     * @param bound the upper bound (exclusive).  Must be positive.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   519
     * @return a pseudorandom {@code double} value between zero
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   520
     *         (inclusive) and the bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   521
     * @throws IllegalArgumentException if {@code bound} is not positive
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   522
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   523
    public double nextDouble(double bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   524
        if (!(bound > 0.0))
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   525
            throw new IllegalArgumentException(BadBound);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   526
        double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   527
        return (result < bound) ?  result : // correct for rounding
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   528
            Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   529
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   530
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   531
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   532
     * Returns a pseudorandom {@code double} value between the specified
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   533
     * origin (inclusive) and bound (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   534
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   535
     * @param origin the least value returned
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   536
     * @param bound the upper bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   537
     * @return a pseudorandom {@code double} value between the origin
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   538
     *         (inclusive) and the bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   539
     * @throws IllegalArgumentException if {@code origin} is greater than
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   540
     *         or equal to {@code bound}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   541
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   542
    public double nextDouble(double origin, double bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   543
        if (!(origin < bound))
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   544
            throw new IllegalArgumentException(BadRange);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   545
        return internalNextDouble(origin, bound);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   546
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   547
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   548
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   549
     * Returns a pseudorandom {@code boolean} value.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   550
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   551
     * @return a pseudorandom {@code boolean} value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   552
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   553
    public boolean nextBoolean() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   554
        return mix32(nextSeed()) < 0;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   555
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   556
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   557
    // stream methods, coded in a way intended to better isolate for
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   558
    // maintenance purposes the small differences across forms.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   559
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   560
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   561
     * Returns a stream producing the given {@code streamSize} number
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   562
     * of pseudorandom {@code int} values from this generator and/or
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   563
     * one split from it.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   564
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   565
     * @param streamSize the number of values to generate
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   566
     * @return a stream of pseudorandom {@code int} values
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   567
     * @throws IllegalArgumentException if {@code streamSize} is
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   568
     *         less than zero
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   569
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   570
    public IntStream ints(long streamSize) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   571
        if (streamSize < 0L)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   572
            throw new IllegalArgumentException(BadSize);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   573
        return StreamSupport.intStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   574
            (new RandomIntsSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   575
             (this, 0L, streamSize, Integer.MAX_VALUE, 0),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   576
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   577
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   578
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   579
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   580
     * Returns an effectively unlimited stream of pseudorandom {@code int}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   581
     * values from this generator and/or one split from it.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   582
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   583
     * @implNote This method is implemented to be equivalent to {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   584
     * ints(Long.MAX_VALUE)}.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   585
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   586
     * @return a stream of pseudorandom {@code int} values
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   587
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   588
    public IntStream ints() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   589
        return StreamSupport.intStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   590
            (new RandomIntsSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   591
             (this, 0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   592
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   593
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   594
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   595
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   596
     * Returns a stream producing the given {@code streamSize} number
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   597
     * of pseudorandom {@code int} values from this generator and/or one split
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   598
     * from it; each value conforms to the given origin (inclusive) and bound
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   599
     * (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   600
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   601
     * @param streamSize the number of values to generate
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   602
     * @param randomNumberOrigin the origin (inclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   603
     * @param randomNumberBound the bound (exclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   604
     * @return a stream of pseudorandom {@code int} values,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   605
     *         each with the given origin (inclusive) and bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   606
     * @throws IllegalArgumentException if {@code streamSize} is
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   607
     *         less than zero, or {@code randomNumberOrigin}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   608
     *         is greater than or equal to {@code randomNumberBound}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   609
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   610
    public IntStream ints(long streamSize, int randomNumberOrigin,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   611
                          int randomNumberBound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   612
        if (streamSize < 0L)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   613
            throw new IllegalArgumentException(BadSize);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   614
        if (randomNumberOrigin >= randomNumberBound)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   615
            throw new IllegalArgumentException(BadRange);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   616
        return StreamSupport.intStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   617
            (new RandomIntsSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   618
             (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   619
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   620
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   621
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   622
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   623
     * Returns an effectively unlimited stream of pseudorandom {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   624
     * int} values from this generator and/or one split from it; each value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   625
     * conforms to the given origin (inclusive) and bound (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   626
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   627
     * @implNote This method is implemented to be equivalent to {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   628
     * ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   629
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   630
     * @param randomNumberOrigin the origin (inclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   631
     * @param randomNumberBound the bound (exclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   632
     * @return a stream of pseudorandom {@code int} values,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   633
     *         each with the given origin (inclusive) and bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   634
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   635
     *         is greater than or equal to {@code randomNumberBound}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   636
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   637
    public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   638
        if (randomNumberOrigin >= randomNumberBound)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   639
            throw new IllegalArgumentException(BadRange);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   640
        return StreamSupport.intStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   641
            (new RandomIntsSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   642
             (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   643
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   644
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   645
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   646
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   647
     * Returns a stream producing the given {@code streamSize} number
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   648
     * of pseudorandom {@code long} values from this generator and/or
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   649
     * one split from it.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   650
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   651
     * @param streamSize the number of values to generate
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   652
     * @return a stream of pseudorandom {@code long} values
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   653
     * @throws IllegalArgumentException if {@code streamSize} is
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   654
     *         less than zero
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   655
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   656
    public LongStream longs(long streamSize) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   657
        if (streamSize < 0L)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   658
            throw new IllegalArgumentException(BadSize);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   659
        return StreamSupport.longStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   660
            (new RandomLongsSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   661
             (this, 0L, streamSize, Long.MAX_VALUE, 0L),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   662
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   663
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   664
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   665
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   666
     * Returns an effectively unlimited stream of pseudorandom {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   667
     * long} values from this generator and/or one split from it.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   668
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   669
     * @implNote This method is implemented to be equivalent to {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   670
     * longs(Long.MAX_VALUE)}.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   671
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   672
     * @return a stream of pseudorandom {@code long} values
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   673
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   674
    public LongStream longs() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   675
        return StreamSupport.longStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   676
            (new RandomLongsSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   677
             (this, 0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   678
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   679
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   680
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   681
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   682
     * Returns a stream producing the given {@code streamSize} number of
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   683
     * pseudorandom {@code long} values from this generator and/or one split
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   684
     * from it; each value conforms to the given origin (inclusive) and bound
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   685
     * (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   686
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   687
     * @param streamSize the number of values to generate
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   688
     * @param randomNumberOrigin the origin (inclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   689
     * @param randomNumberBound the bound (exclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   690
     * @return a stream of pseudorandom {@code long} values,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   691
     *         each with the given origin (inclusive) and bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   692
     * @throws IllegalArgumentException if {@code streamSize} is
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   693
     *         less than zero, or {@code randomNumberOrigin}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   694
     *         is greater than or equal to {@code randomNumberBound}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   695
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   696
    public LongStream longs(long streamSize, long randomNumberOrigin,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   697
                            long randomNumberBound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   698
        if (streamSize < 0L)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   699
            throw new IllegalArgumentException(BadSize);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   700
        if (randomNumberOrigin >= randomNumberBound)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   701
            throw new IllegalArgumentException(BadRange);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   702
        return StreamSupport.longStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   703
            (new RandomLongsSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   704
             (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   705
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   706
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   707
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   708
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   709
     * Returns an effectively unlimited stream of pseudorandom {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   710
     * long} values from this generator and/or one split from it; each value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   711
     * conforms to the given origin (inclusive) and bound (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   712
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   713
     * @implNote This method is implemented to be equivalent to {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   714
     * longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   715
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   716
     * @param randomNumberOrigin the origin (inclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   717
     * @param randomNumberBound the bound (exclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   718
     * @return a stream of pseudorandom {@code long} values,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   719
     *         each with the given origin (inclusive) and bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   720
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   721
     *         is greater than or equal to {@code randomNumberBound}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   722
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   723
    public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   724
        if (randomNumberOrigin >= randomNumberBound)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   725
            throw new IllegalArgumentException(BadRange);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   726
        return StreamSupport.longStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   727
            (new RandomLongsSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   728
             (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   729
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   730
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   731
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   732
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   733
     * Returns a stream producing the given {@code streamSize} number of
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   734
     * pseudorandom {@code double} values from this generator and/or one split
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   735
     * from it; each value is between zero (inclusive) and one (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   736
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   737
     * @param streamSize the number of values to generate
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   738
     * @return a stream of {@code double} values
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   739
     * @throws IllegalArgumentException if {@code streamSize} is
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   740
     *         less than zero
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   741
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   742
    public DoubleStream doubles(long streamSize) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   743
        if (streamSize < 0L)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   744
            throw new IllegalArgumentException(BadSize);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   745
        return StreamSupport.doubleStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   746
            (new RandomDoublesSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   747
             (this, 0L, streamSize, Double.MAX_VALUE, 0.0),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   748
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   749
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   750
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   751
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   752
     * Returns an effectively unlimited stream of pseudorandom {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   753
     * double} values from this generator and/or one split from it; each value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   754
     * is between zero (inclusive) and one (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   755
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   756
     * @implNote This method is implemented to be equivalent to {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   757
     * doubles(Long.MAX_VALUE)}.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   758
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   759
     * @return a stream of pseudorandom {@code double} values
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   760
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   761
    public DoubleStream doubles() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   762
        return StreamSupport.doubleStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   763
            (new RandomDoublesSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   764
             (this, 0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   765
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   766
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   767
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   768
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   769
     * Returns a stream producing the given {@code streamSize} number of
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   770
     * pseudorandom {@code double} values from this generator and/or one split
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   771
     * from it; each value conforms to the given origin (inclusive) and bound
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   772
     * (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   773
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   774
     * @param streamSize the number of values to generate
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   775
     * @param randomNumberOrigin the origin (inclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   776
     * @param randomNumberBound the bound (exclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   777
     * @return a stream of pseudorandom {@code double} values,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   778
     *         each with the given origin (inclusive) and bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   779
     * @throws IllegalArgumentException if {@code streamSize} is
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   780
     *         less than zero
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   781
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   782
     *         is greater than or equal to {@code randomNumberBound}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   783
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   784
    public DoubleStream doubles(long streamSize, double randomNumberOrigin,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   785
                                double randomNumberBound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   786
        if (streamSize < 0L)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   787
            throw new IllegalArgumentException(BadSize);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   788
        if (!(randomNumberOrigin < randomNumberBound))
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   789
            throw new IllegalArgumentException(BadRange);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   790
        return StreamSupport.doubleStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   791
            (new RandomDoublesSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   792
             (this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   793
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   794
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   795
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   796
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   797
     * Returns an effectively unlimited stream of pseudorandom {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   798
     * double} values from this generator and/or one split from it; each value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   799
     * conforms to the given origin (inclusive) and bound (exclusive).
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   800
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   801
     * @implNote This method is implemented to be equivalent to {@code
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   802
     * doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   803
     *
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   804
     * @param randomNumberOrigin the origin (inclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   805
     * @param randomNumberBound the bound (exclusive) of each random value
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   806
     * @return a stream of pseudorandom {@code double} values,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   807
     *         each with the given origin (inclusive) and bound (exclusive)
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   808
     * @throws IllegalArgumentException if {@code randomNumberOrigin}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   809
     *         is greater than or equal to {@code randomNumberBound}
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   810
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   811
    public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   812
        if (!(randomNumberOrigin < randomNumberBound))
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   813
            throw new IllegalArgumentException(BadRange);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   814
        return StreamSupport.doubleStream
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   815
            (new RandomDoublesSpliterator
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   816
             (this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   817
             false);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   818
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   819
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   820
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   821
     * Spliterator for int streams.  We multiplex the four int
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   822
     * versions into one class by treating a bound less than origin as
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   823
     * unbounded, and also by treating "infinite" as equivalent to
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   824
     * Long.MAX_VALUE. For splits, it uses the standard divide-by-two
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   825
     * approach. The long and double versions of this class are
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   826
     * identical except for types.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   827
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   828
    static final class RandomIntsSpliterator implements Spliterator.OfInt {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   829
        final SplittableRandom rng;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   830
        long index;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   831
        final long fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   832
        final int origin;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   833
        final int bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   834
        RandomIntsSpliterator(SplittableRandom rng, long index, long fence,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   835
                              int origin, int bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   836
            this.rng = rng; this.index = index; this.fence = fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   837
            this.origin = origin; this.bound = bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   838
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   839
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   840
        public RandomIntsSpliterator trySplit() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   841
            long i = index, m = (i + fence) >>> 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   842
            return (m <= i) ? null :
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   843
                new RandomIntsSpliterator(rng.split(), i, index = m, origin, bound);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   844
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   845
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   846
        public long estimateSize() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   847
            return fence - index;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   848
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   849
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   850
        public int characteristics() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   851
            return (Spliterator.SIZED | Spliterator.SUBSIZED |
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   852
                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   853
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   854
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   855
        public boolean tryAdvance(IntConsumer consumer) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   856
            if (consumer == null) throw new NullPointerException();
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   857
            long i = index, f = fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   858
            if (i < f) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   859
                consumer.accept(rng.internalNextInt(origin, bound));
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   860
                index = i + 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   861
                return true;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   862
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   863
            return false;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   864
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   865
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   866
        public void forEachRemaining(IntConsumer consumer) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   867
            if (consumer == null) throw new NullPointerException();
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   868
            long i = index, f = fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   869
            if (i < f) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   870
                index = f;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   871
                SplittableRandom r = rng;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   872
                int o = origin, b = bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   873
                do {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   874
                    consumer.accept(r.internalNextInt(o, b));
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   875
                } while (++i < f);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   876
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   877
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   878
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   879
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   880
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   881
     * Spliterator for long streams.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   882
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   883
    static final class RandomLongsSpliterator implements Spliterator.OfLong {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   884
        final SplittableRandom rng;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   885
        long index;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   886
        final long fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   887
        final long origin;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   888
        final long bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   889
        RandomLongsSpliterator(SplittableRandom rng, long index, long fence,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   890
                               long origin, long bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   891
            this.rng = rng; this.index = index; this.fence = fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   892
            this.origin = origin; this.bound = bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   893
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   894
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   895
        public RandomLongsSpliterator trySplit() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   896
            long i = index, m = (i + fence) >>> 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   897
            return (m <= i) ? null :
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   898
                new RandomLongsSpliterator(rng.split(), i, index = m, origin, bound);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   899
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   900
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   901
        public long estimateSize() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   902
            return fence - index;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   903
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   904
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   905
        public int characteristics() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   906
            return (Spliterator.SIZED | Spliterator.SUBSIZED |
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   907
                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   908
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   909
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   910
        public boolean tryAdvance(LongConsumer consumer) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   911
            if (consumer == null) throw new NullPointerException();
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   912
            long i = index, f = fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   913
            if (i < f) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   914
                consumer.accept(rng.internalNextLong(origin, bound));
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   915
                index = i + 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   916
                return true;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   917
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   918
            return false;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   919
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   920
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   921
        public void forEachRemaining(LongConsumer consumer) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   922
            if (consumer == null) throw new NullPointerException();
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   923
            long i = index, f = fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   924
            if (i < f) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   925
                index = f;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   926
                SplittableRandom r = rng;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   927
                long o = origin, b = bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   928
                do {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   929
                    consumer.accept(r.internalNextLong(o, b));
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   930
                } while (++i < f);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   931
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   932
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   933
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   934
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   935
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   936
    /**
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   937
     * Spliterator for double streams.
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   938
     */
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   939
    static final class RandomDoublesSpliterator implements Spliterator.OfDouble {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   940
        final SplittableRandom rng;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   941
        long index;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   942
        final long fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   943
        final double origin;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   944
        final double bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   945
        RandomDoublesSpliterator(SplittableRandom rng, long index, long fence,
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   946
                                 double origin, double bound) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   947
            this.rng = rng; this.index = index; this.fence = fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   948
            this.origin = origin; this.bound = bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   949
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   950
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   951
        public RandomDoublesSpliterator trySplit() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   952
            long i = index, m = (i + fence) >>> 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   953
            return (m <= i) ? null :
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   954
                new RandomDoublesSpliterator(rng.split(), i, index = m, origin, bound);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   955
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   956
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   957
        public long estimateSize() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   958
            return fence - index;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   959
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   960
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   961
        public int characteristics() {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   962
            return (Spliterator.SIZED | Spliterator.SUBSIZED |
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   963
                    Spliterator.NONNULL | Spliterator.IMMUTABLE);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   964
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   965
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   966
        public boolean tryAdvance(DoubleConsumer consumer) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   967
            if (consumer == null) throw new NullPointerException();
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   968
            long i = index, f = fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   969
            if (i < f) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   970
                consumer.accept(rng.internalNextDouble(origin, bound));
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   971
                index = i + 1;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   972
                return true;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   973
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   974
            return false;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   975
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   976
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   977
        public void forEachRemaining(DoubleConsumer consumer) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   978
            if (consumer == null) throw new NullPointerException();
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   979
            long i = index, f = fence;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   980
            if (i < f) {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   981
                index = f;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   982
                SplittableRandom r = rng;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   983
                double o = origin, b = bound;
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   984
                do {
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   985
                    consumer.accept(r.internalNextDouble(o, b));
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   986
                } while (++i < f);
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   987
            }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   988
        }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   989
    }
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   990
bcf345afddae 8020292: j.u.SplittableRandom
psandoz
parents:
diff changeset
   991
}