mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8248862: Implement Enhanced Pseudo-Random Number Generators
Reviewed-by: darcy
This commit is contained in:
parent
39719da9d1
commit
a0ec2cb289
27 changed files with 11384 additions and 1772 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -22,24 +22,24 @@
|
|||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.DoubleConsumer;
|
||||
import java.util.function.IntConsumer;
|
||||
import java.util.function.LongConsumer;
|
||||
import java.util.stream.DoubleStream;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.StreamSupport;
|
||||
import java.util.stream.Stream;
|
||||
import jdk.internal.util.random.RandomSupport;
|
||||
import jdk.internal.util.random.RandomSupport.AbstractSplittableGenerator;
|
||||
import jdk.internal.util.random.RandomSupport.RandomGeneratorProperties;
|
||||
|
||||
/**
|
||||
* A generator of uniform pseudorandom values applicable for use in
|
||||
* (among other contexts) isolated parallel computations that may
|
||||
* generate subtasks. Class {@code SplittableRandom} supports methods for
|
||||
* producing pseudorandom numbers of type {@code int}, {@code long},
|
||||
* and {@code double} with similar usages as for class
|
||||
* A generator of uniform pseudorandom values (with period 2<sup>64</sup>)
|
||||
* applicable for use in (among other contexts) isolated parallel
|
||||
* computations that may generate subtasks. Class {@code SplittableRandom}
|
||||
* supports methods for producing pseudorandom numbers of type {@code int},
|
||||
* {@code long}, and {@code double} with similar usages as for class
|
||||
* {@link java.util.Random} but differs in the following ways:
|
||||
*
|
||||
* <ul>
|
||||
|
@ -51,16 +51,16 @@ import java.util.stream.StreamSupport;
|
|||
* 3.31.1</a>.) These tests validate only the methods for certain
|
||||
* types and ranges, but similar properties are expected to hold, at
|
||||
* least approximately, for others as well. The <em>period</em>
|
||||
* (length of any series of generated values before it repeats) is at
|
||||
* least 2<sup>64</sup>.
|
||||
* (length of any series of generated values before it repeats) is
|
||||
* 2<sup>64</sup>. </li>
|
||||
*
|
||||
* <li>Method {@link #split} constructs and returns a new
|
||||
* <li> Method {@link #split} constructs and returns a new
|
||||
* SplittableRandom instance that shares no mutable state with the
|
||||
* current instance. However, with very high probability, the
|
||||
* values collectively generated by the two objects have the same
|
||||
* statistical properties as if the same quantity of values were
|
||||
* generated by a single thread using a single {@code
|
||||
* SplittableRandom} object.
|
||||
* SplittableRandom} object. </li>
|
||||
*
|
||||
* <li>Instances of SplittableRandom are <em>not</em> thread-safe.
|
||||
* They are designed to be split, not shared, across threads. For
|
||||
|
@ -71,7 +71,7 @@ import java.util.stream.StreamSupport;
|
|||
*
|
||||
* <li>This class provides additional methods for generating random
|
||||
* streams, that employ the above techniques when used in {@code
|
||||
* stream.parallel()} mode.
|
||||
* stream.parallel()} mode.</li>
|
||||
*
|
||||
* </ul>
|
||||
*
|
||||
|
@ -80,13 +80,19 @@ import java.util.stream.StreamSupport;
|
|||
* in security-sensitive applications. Additionally,
|
||||
* default-constructed instances do not use a cryptographically random
|
||||
* seed unless the {@linkplain System#getProperty system property}
|
||||
* {@systemProperty java.util.secureRandomSeed} is set to {@code true}.
|
||||
* {@code java.util.secureRandomSeed} is set to {@code true}.
|
||||
*
|
||||
* @author Guy Steele
|
||||
* @author Doug Lea
|
||||
* @since 1.8
|
||||
*/
|
||||
public final class SplittableRandom {
|
||||
@SuppressWarnings("exports")
|
||||
@RandomGeneratorProperties(
|
||||
name = "SplittableRandom",
|
||||
i = 64, j = 0, k = 0,
|
||||
equidistribution = 1
|
||||
)
|
||||
public final class SplittableRandom extends AbstractSplittableGenerator {
|
||||
|
||||
/*
|
||||
* Implementation Overview.
|
||||
|
@ -160,12 +166,6 @@ public final class SplittableRandom {
|
|||
*/
|
||||
private static final long GOLDEN_GAMMA = 0x9e3779b97f4a7c15L;
|
||||
|
||||
/**
|
||||
* The least non-zero value returned by nextDouble(). This value
|
||||
* is scaled by a random value of 53 bits to produce a result.
|
||||
*/
|
||||
private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53);
|
||||
|
||||
/**
|
||||
* The seed. Updated only via method nextSeed.
|
||||
*/
|
||||
|
@ -186,6 +186,7 @@ public final class SplittableRandom {
|
|||
|
||||
/**
|
||||
* Computes Stafford variant 13 of 64bit mix function.
|
||||
* http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
|
||||
*/
|
||||
private static long mix64(long z) {
|
||||
z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L;
|
||||
|
@ -195,6 +196,7 @@ public final class SplittableRandom {
|
|||
|
||||
/**
|
||||
* Returns the 32 high bits of Stafford variant 4 mix64 function as int.
|
||||
* http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
|
||||
*/
|
||||
private static int mix32(long z) {
|
||||
z = (z ^ (z >>> 33)) * 0x62a9d9ed799705f5L;
|
||||
|
@ -203,6 +205,8 @@ public final class SplittableRandom {
|
|||
|
||||
/**
|
||||
* Returns the gamma value to use for a new split instance.
|
||||
* Uses the 64bit mix function from MurmurHash3.
|
||||
* https://github.com/aappleby/smhasher/wiki/MurmurHash3
|
||||
*/
|
||||
private static long mixGamma(long z) {
|
||||
z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
|
||||
|
@ -219,141 +223,10 @@ public final class SplittableRandom {
|
|||
return seed += gamma;
|
||||
}
|
||||
|
||||
// IllegalArgumentException messages
|
||||
static final String BAD_BOUND = "bound must be positive";
|
||||
static final String BAD_RANGE = "bound must be greater than origin";
|
||||
static final String BAD_SIZE = "size must be non-negative";
|
||||
|
||||
/**
|
||||
* The seed generator for default constructors.
|
||||
*/
|
||||
private static final AtomicLong defaultGen
|
||||
= new AtomicLong(mix64(System.currentTimeMillis()) ^
|
||||
mix64(System.nanoTime()));
|
||||
|
||||
// at end of <clinit> to survive static initialization circularity
|
||||
static {
|
||||
if (java.security.AccessController.doPrivileged(
|
||||
new java.security.PrivilegedAction<Boolean>() {
|
||||
public Boolean run() {
|
||||
return Boolean.getBoolean("java.util.secureRandomSeed");
|
||||
}})) {
|
||||
byte[] seedBytes = java.security.SecureRandom.getSeed(8);
|
||||
long s = (long)seedBytes[0] & 0xffL;
|
||||
for (int i = 1; i < 8; ++i)
|
||||
s = (s << 8) | ((long)seedBytes[i] & 0xffL);
|
||||
defaultGen.set(s);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal versions of nextX methods used by streams, as well as
|
||||
* the public nextX(origin, bound) methods. These exist mainly to
|
||||
* avoid the need for multiple versions of stream spliterators
|
||||
* across the different exported forms of streams.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The form of nextLong used by LongStream Spliterators. If
|
||||
* origin is greater than bound, acts as unbounded form of
|
||||
* nextLong, else as bounded form.
|
||||
*
|
||||
* @param origin the least value, unless greater than bound
|
||||
* @param bound the upper bound (exclusive), must not equal origin
|
||||
* @return a pseudorandom value
|
||||
*/
|
||||
final long internalNextLong(long origin, long bound) {
|
||||
/*
|
||||
* Four Cases:
|
||||
*
|
||||
* 1. If the arguments indicate unbounded form, act as
|
||||
* nextLong().
|
||||
*
|
||||
* 2. If the range is an exact power of two, apply the
|
||||
* associated bit mask.
|
||||
*
|
||||
* 3. If the range is positive, loop to avoid potential bias
|
||||
* when the implicit nextLong() bound (2<sup>64</sup>) is not
|
||||
* evenly divisible by the range. The loop rejects candidates
|
||||
* computed from otherwise over-represented values. The
|
||||
* expected number of iterations under an ideal generator
|
||||
* varies from 1 to 2, depending on the bound. The loop itself
|
||||
* takes an unlovable form. Because the first candidate is
|
||||
* already available, we need a break-in-the-middle
|
||||
* construction, which is concisely but cryptically performed
|
||||
* within the while-condition of a body-less for loop.
|
||||
*
|
||||
* 4. Otherwise, the range cannot be represented as a positive
|
||||
* long. The loop repeatedly generates unbounded longs until
|
||||
* obtaining a candidate meeting constraints (with an expected
|
||||
* number of iterations of less than two).
|
||||
*/
|
||||
|
||||
long r = mix64(nextSeed());
|
||||
if (origin < bound) {
|
||||
long n = bound - origin, m = n - 1;
|
||||
if ((n & m) == 0L) // power of two
|
||||
r = (r & m) + origin;
|
||||
else if (n > 0L) { // reject over-represented candidates
|
||||
for (long u = r >>> 1; // ensure nonnegative
|
||||
u + m - (r = u % n) < 0L; // rejection check
|
||||
u = mix64(nextSeed()) >>> 1) // retry
|
||||
;
|
||||
r += origin;
|
||||
}
|
||||
else { // range not representable as long
|
||||
while (r < origin || r >= bound)
|
||||
r = mix64(nextSeed());
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* The form of nextInt used by IntStream Spliterators.
|
||||
* Exactly the same as long version, except for types.
|
||||
*
|
||||
* @param origin the least value, unless greater than bound
|
||||
* @param bound the upper bound (exclusive), must not equal origin
|
||||
* @return a pseudorandom value
|
||||
*/
|
||||
final int internalNextInt(int origin, int bound) {
|
||||
int r = mix32(nextSeed());
|
||||
if (origin < bound) {
|
||||
int n = bound - origin, m = n - 1;
|
||||
if ((n & m) == 0)
|
||||
r = (r & m) + origin;
|
||||
else if (n > 0) {
|
||||
for (int u = r >>> 1;
|
||||
u + m - (r = u % n) < 0;
|
||||
u = mix32(nextSeed()) >>> 1)
|
||||
;
|
||||
r += origin;
|
||||
}
|
||||
else {
|
||||
while (r < origin || r >= bound)
|
||||
r = mix32(nextSeed());
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* The form of nextDouble used by DoubleStream Spliterators.
|
||||
*
|
||||
* @param origin the least value, unless greater than bound
|
||||
* @param bound the upper bound (exclusive), must not equal origin
|
||||
* @return a pseudorandom value
|
||||
*/
|
||||
final double internalNextDouble(double origin, double bound) {
|
||||
double r = (nextLong() >>> 11) * DOUBLE_UNIT;
|
||||
if (origin < bound) {
|
||||
r = r * (bound - origin) + origin;
|
||||
if (r >= bound) // correct for rounding
|
||||
r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
private static final AtomicLong defaultGen = new AtomicLong(RandomSupport.initialSeed());
|
||||
|
||||
/* ---------------- public methods ---------------- */
|
||||
|
||||
|
@ -375,7 +248,7 @@ public final class SplittableRandom {
|
|||
* may, and typically does, vary across program invocations.
|
||||
*/
|
||||
public SplittableRandom() { // emulate defaultGen.split()
|
||||
long s = defaultGen.getAndAdd(GOLDEN_GAMMA << 1);
|
||||
long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA);
|
||||
this.seed = mix64(s);
|
||||
this.gamma = mixGamma(s + GOLDEN_GAMMA);
|
||||
}
|
||||
|
@ -399,186 +272,102 @@ public final class SplittableRandom {
|
|||
}
|
||||
|
||||
/**
|
||||
* Fills a user-supplied byte array with generated pseudorandom bytes.
|
||||
*
|
||||
* @param bytes the byte array to fill with pseudorandom bytes
|
||||
* @throws NullPointerException if bytes is null
|
||||
* @since 10
|
||||
* {@inheritDoc}
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
* @since 17
|
||||
*/
|
||||
public void nextBytes(byte[] bytes) {
|
||||
int i = 0;
|
||||
int len = bytes.length;
|
||||
for (int words = len >> 3; words--> 0; ) {
|
||||
long rnd = nextLong();
|
||||
for (int n = 8; n--> 0; rnd >>>= Byte.SIZE)
|
||||
bytes[i++] = (byte)rnd;
|
||||
}
|
||||
if (i < len)
|
||||
for (long rnd = nextLong(); i < len; rnd >>>= Byte.SIZE)
|
||||
bytes[i++] = (byte)rnd;
|
||||
public SplittableRandom split(SplittableGenerator source) {
|
||||
return new SplittableRandom(source.nextLong(), mixGamma(source.nextLong()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code int} value.
|
||||
*
|
||||
* @return a pseudorandom {@code int} value
|
||||
* @hidden
|
||||
*/
|
||||
@Override
|
||||
public Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
|
||||
return super.makeIntsSpliterator(index, fence, origin, bound);
|
||||
}
|
||||
|
||||
/**
|
||||
* @hidden
|
||||
*/
|
||||
@Override
|
||||
public Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
|
||||
return super.makeLongsSpliterator(index, fence, origin, bound);
|
||||
}
|
||||
|
||||
/**
|
||||
* @hidden
|
||||
*/
|
||||
@Override
|
||||
public Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
|
||||
return super.makeDoublesSpliterator(index, fence, origin, bound);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextInt() {
|
||||
return mix32(nextSeed());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code int} value between zero (inclusive)
|
||||
* and the specified bound (exclusive).
|
||||
*
|
||||
* @param bound the upper bound (exclusive). Must be positive.
|
||||
* @return a pseudorandom {@code int} value between zero
|
||||
* (inclusive) and the bound (exclusive)
|
||||
* @throws IllegalArgumentException if {@code bound} is not positive
|
||||
*/
|
||||
public int nextInt(int bound) {
|
||||
if (bound <= 0)
|
||||
throw new IllegalArgumentException(BAD_BOUND);
|
||||
// Specialize internalNextInt for origin 0
|
||||
int r = mix32(nextSeed());
|
||||
int m = bound - 1;
|
||||
if ((bound & m) == 0) // power of two
|
||||
r &= m;
|
||||
else { // reject over-represented candidates
|
||||
for (int u = r >>> 1;
|
||||
u + m - (r = u % bound) < 0;
|
||||
u = mix32(nextSeed()) >>> 1)
|
||||
;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code int} value between the specified
|
||||
* origin (inclusive) and the specified bound (exclusive).
|
||||
*
|
||||
* @param origin the least value returned
|
||||
* @param bound the upper bound (exclusive)
|
||||
* @return a pseudorandom {@code int} value between the origin
|
||||
* (inclusive) and the bound (exclusive)
|
||||
* @throws IllegalArgumentException if {@code origin} is greater than
|
||||
* or equal to {@code bound}
|
||||
*/
|
||||
public int nextInt(int origin, int bound) {
|
||||
if (origin >= bound)
|
||||
throw new IllegalArgumentException(BAD_RANGE);
|
||||
return internalNextInt(origin, bound);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code long} value.
|
||||
*
|
||||
* @return a pseudorandom {@code long} value
|
||||
*/
|
||||
@Override
|
||||
public long nextLong() {
|
||||
return mix64(nextSeed());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code long} value between zero (inclusive)
|
||||
* and the specified bound (exclusive).
|
||||
*
|
||||
* @param bound the upper bound (exclusive). Must be positive.
|
||||
* @return a pseudorandom {@code long} value between zero
|
||||
* (inclusive) and the bound (exclusive)
|
||||
* @throws IllegalArgumentException if {@code bound} is not positive
|
||||
* {@inheritDoc}
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
* @since 10
|
||||
*/
|
||||
public long nextLong(long bound) {
|
||||
if (bound <= 0)
|
||||
throw new IllegalArgumentException(BAD_BOUND);
|
||||
// Specialize internalNextLong for origin 0
|
||||
long r = mix64(nextSeed());
|
||||
long m = bound - 1;
|
||||
if ((bound & m) == 0L) // power of two
|
||||
r &= m;
|
||||
else { // reject over-represented candidates
|
||||
for (long u = r >>> 1;
|
||||
u + m - (r = u % bound) < 0L;
|
||||
u = mix64(nextSeed()) >>> 1)
|
||||
;
|
||||
}
|
||||
return r;
|
||||
@Override
|
||||
public void nextBytes(byte[] bytes) {
|
||||
super.nextBytes(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code long} value between the specified
|
||||
* origin (inclusive) and the specified bound (exclusive).
|
||||
*
|
||||
* @param origin the least value returned
|
||||
* @param bound the upper bound (exclusive)
|
||||
* @return a pseudorandom {@code long} value between the origin
|
||||
* (inclusive) and the bound (exclusive)
|
||||
* @throws IllegalArgumentException if {@code origin} is greater than
|
||||
* or equal to {@code bound}
|
||||
* {@inheritDoc}
|
||||
* @implSpec {@inheritDoc}
|
||||
* @since 17
|
||||
*/
|
||||
public long nextLong(long origin, long bound) {
|
||||
if (origin >= bound)
|
||||
throw new IllegalArgumentException(BAD_RANGE);
|
||||
return internalNextLong(origin, bound);
|
||||
@Override
|
||||
public Stream<SplittableGenerator> splits() {
|
||||
return super.splits();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code double} value between zero
|
||||
* (inclusive) and one (exclusive).
|
||||
*
|
||||
* @return a pseudorandom {@code double} value between zero
|
||||
* (inclusive) and one (exclusive)
|
||||
* {@inheritDoc}
|
||||
* @throws IllegalArgumentException {@inheritDoc}
|
||||
* @implSpec {@inheritDoc}
|
||||
* @since 17
|
||||
*/
|
||||
public double nextDouble() {
|
||||
return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT;
|
||||
@Override
|
||||
public Stream<SplittableGenerator> splits(long streamSize) {
|
||||
return super.splits(streamSize, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code double} value between 0.0
|
||||
* (inclusive) and the specified bound (exclusive).
|
||||
*
|
||||
* @param bound the upper bound (exclusive). Must be positive.
|
||||
* @return a pseudorandom {@code double} value between zero
|
||||
* (inclusive) and the bound (exclusive)
|
||||
* @throws IllegalArgumentException if {@code bound} is not positive
|
||||
* {@inheritDoc}
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
* @implSpec {@inheritDoc}
|
||||
* @since 17
|
||||
*/
|
||||
public double nextDouble(double bound) {
|
||||
if (!(bound > 0.0))
|
||||
throw new IllegalArgumentException(BAD_BOUND);
|
||||
double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound;
|
||||
return (result < bound) ? result : // correct for rounding
|
||||
Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1);
|
||||
@Override
|
||||
public Stream<SplittableGenerator> splits(SplittableGenerator source) {
|
||||
return super.splits(Long.MAX_VALUE, source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code double} value between the specified
|
||||
* origin (inclusive) and bound (exclusive).
|
||||
*
|
||||
* @param origin the least value returned
|
||||
* @param bound the upper bound (exclusive)
|
||||
* @return a pseudorandom {@code double} value between the origin
|
||||
* (inclusive) and the bound (exclusive)
|
||||
* @throws IllegalArgumentException if {@code origin} is greater than
|
||||
* or equal to {@code bound}
|
||||
* {@inheritDoc}
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
* @throws IllegalArgumentException {@inheritDoc}
|
||||
* @implSpec {@inheritDoc}
|
||||
* @since 17
|
||||
*/
|
||||
public double nextDouble(double origin, double bound) {
|
||||
if (!(origin < bound))
|
||||
throw new IllegalArgumentException(BAD_RANGE);
|
||||
return internalNextDouble(origin, bound);
|
||||
@Override
|
||||
public Stream<SplittableGenerator> splits(long streamSize, SplittableGenerator source) {
|
||||
return super.splits(streamSize, source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom {@code boolean} value.
|
||||
*
|
||||
* @return a pseudorandom {@code boolean} value
|
||||
*/
|
||||
public boolean nextBoolean() {
|
||||
return mix32(nextSeed()) < 0;
|
||||
}
|
||||
|
||||
// stream methods, coded in a way intended to better isolate for
|
||||
// maintenance purposes the small differences across forms.
|
||||
|
||||
/**
|
||||
* Returns a stream producing the given {@code streamSize} number
|
||||
* of pseudorandom {@code int} values from this generator and/or
|
||||
|
@ -589,13 +378,9 @@ public final class SplittableRandom {
|
|||
* @throws IllegalArgumentException if {@code streamSize} is
|
||||
* less than zero
|
||||
*/
|
||||
@Override
|
||||
public IntStream ints(long streamSize) {
|
||||
if (streamSize < 0L)
|
||||
throw new IllegalArgumentException(BAD_SIZE);
|
||||
return StreamSupport.intStream
|
||||
(new RandomIntsSpliterator
|
||||
(this, 0L, streamSize, Integer.MAX_VALUE, 0),
|
||||
false);
|
||||
return super.ints(streamSize);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -607,11 +392,9 @@ public final class SplittableRandom {
|
|||
*
|
||||
* @return a stream of pseudorandom {@code int} values
|
||||
*/
|
||||
@Override
|
||||
public IntStream ints() {
|
||||
return StreamSupport.intStream
|
||||
(new RandomIntsSpliterator
|
||||
(this, 0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0),
|
||||
false);
|
||||
return super.ints();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -629,16 +412,9 @@ public final class SplittableRandom {
|
|||
* less than zero, or {@code randomNumberOrigin}
|
||||
* is greater than or equal to {@code randomNumberBound}
|
||||
*/
|
||||
public IntStream ints(long streamSize, int randomNumberOrigin,
|
||||
int randomNumberBound) {
|
||||
if (streamSize < 0L)
|
||||
throw new IllegalArgumentException(BAD_SIZE);
|
||||
if (randomNumberOrigin >= randomNumberBound)
|
||||
throw new IllegalArgumentException(BAD_RANGE);
|
||||
return StreamSupport.intStream
|
||||
(new RandomIntsSpliterator
|
||||
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
|
||||
false);
|
||||
@Override
|
||||
public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) {
|
||||
return super.ints(streamSize, randomNumberOrigin, randomNumberBound);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -656,13 +432,9 @@ public final class SplittableRandom {
|
|||
* @throws IllegalArgumentException if {@code randomNumberOrigin}
|
||||
* is greater than or equal to {@code randomNumberBound}
|
||||
*/
|
||||
@Override
|
||||
public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
|
||||
if (randomNumberOrigin >= randomNumberBound)
|
||||
throw new IllegalArgumentException(BAD_RANGE);
|
||||
return StreamSupport.intStream
|
||||
(new RandomIntsSpliterator
|
||||
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
|
||||
false);
|
||||
return super.ints(randomNumberOrigin, randomNumberBound);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -675,13 +447,9 @@ public final class SplittableRandom {
|
|||
* @throws IllegalArgumentException if {@code streamSize} is
|
||||
* less than zero
|
||||
*/
|
||||
@Override
|
||||
public LongStream longs(long streamSize) {
|
||||
if (streamSize < 0L)
|
||||
throw new IllegalArgumentException(BAD_SIZE);
|
||||
return StreamSupport.longStream
|
||||
(new RandomLongsSpliterator
|
||||
(this, 0L, streamSize, Long.MAX_VALUE, 0L),
|
||||
false);
|
||||
return super.longs(streamSize);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -693,11 +461,9 @@ public final class SplittableRandom {
|
|||
*
|
||||
* @return a stream of pseudorandom {@code long} values
|
||||
*/
|
||||
@Override
|
||||
public LongStream longs() {
|
||||
return StreamSupport.longStream
|
||||
(new RandomLongsSpliterator
|
||||
(this, 0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L),
|
||||
false);
|
||||
return super.longs();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -715,16 +481,9 @@ public final class SplittableRandom {
|
|||
* less than zero, or {@code randomNumberOrigin}
|
||||
* is greater than or equal to {@code randomNumberBound}
|
||||
*/
|
||||
public LongStream longs(long streamSize, long randomNumberOrigin,
|
||||
long randomNumberBound) {
|
||||
if (streamSize < 0L)
|
||||
throw new IllegalArgumentException(BAD_SIZE);
|
||||
if (randomNumberOrigin >= randomNumberBound)
|
||||
throw new IllegalArgumentException(BAD_RANGE);
|
||||
return StreamSupport.longStream
|
||||
(new RandomLongsSpliterator
|
||||
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
|
||||
false);
|
||||
@Override
|
||||
public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) {
|
||||
return super.longs(streamSize, randomNumberOrigin, randomNumberBound);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -742,13 +501,9 @@ public final class SplittableRandom {
|
|||
* @throws IllegalArgumentException if {@code randomNumberOrigin}
|
||||
* is greater than or equal to {@code randomNumberBound}
|
||||
*/
|
||||
@Override
|
||||
public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
|
||||
if (randomNumberOrigin >= randomNumberBound)
|
||||
throw new IllegalArgumentException(BAD_RANGE);
|
||||
return StreamSupport.longStream
|
||||
(new RandomLongsSpliterator
|
||||
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
|
||||
false);
|
||||
return super.longs(randomNumberOrigin, randomNumberBound);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -761,13 +516,9 @@ public final class SplittableRandom {
|
|||
* @throws IllegalArgumentException if {@code streamSize} is
|
||||
* less than zero
|
||||
*/
|
||||
@Override
|
||||
public DoubleStream doubles(long streamSize) {
|
||||
if (streamSize < 0L)
|
||||
throw new IllegalArgumentException(BAD_SIZE);
|
||||
return StreamSupport.doubleStream
|
||||
(new RandomDoublesSpliterator
|
||||
(this, 0L, streamSize, Double.MAX_VALUE, 0.0),
|
||||
false);
|
||||
return super.doubles(streamSize);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -780,11 +531,9 @@ public final class SplittableRandom {
|
|||
*
|
||||
* @return a stream of pseudorandom {@code double} values
|
||||
*/
|
||||
@Override
|
||||
public DoubleStream doubles() {
|
||||
return StreamSupport.doubleStream
|
||||
(new RandomDoublesSpliterator
|
||||
(this, 0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0),
|
||||
false);
|
||||
return super.doubles();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -802,16 +551,9 @@ public final class SplittableRandom {
|
|||
* less than zero, or {@code randomNumberOrigin}
|
||||
* is greater than or equal to {@code randomNumberBound}
|
||||
*/
|
||||
public DoubleStream doubles(long streamSize, double randomNumberOrigin,
|
||||
double randomNumberBound) {
|
||||
if (streamSize < 0L)
|
||||
throw new IllegalArgumentException(BAD_SIZE);
|
||||
if (!(randomNumberOrigin < randomNumberBound))
|
||||
throw new IllegalArgumentException(BAD_RANGE);
|
||||
return StreamSupport.doubleStream
|
||||
(new RandomDoublesSpliterator
|
||||
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound),
|
||||
false);
|
||||
@Override
|
||||
public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) {
|
||||
return super.doubles(streamSize, randomNumberOrigin, randomNumberBound);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -829,187 +571,8 @@ public final class SplittableRandom {
|
|||
* @throws IllegalArgumentException if {@code randomNumberOrigin}
|
||||
* is greater than or equal to {@code randomNumberBound}
|
||||
*/
|
||||
@Override
|
||||
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
|
||||
if (!(randomNumberOrigin < randomNumberBound))
|
||||
throw new IllegalArgumentException(BAD_RANGE);
|
||||
return StreamSupport.doubleStream
|
||||
(new RandomDoublesSpliterator
|
||||
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
|
||||
false);
|
||||
return super.doubles(randomNumberOrigin, randomNumberBound);
|
||||
}
|
||||
|
||||
/**
|
||||
* Spliterator for int streams. We multiplex the four int
|
||||
* versions into one class by treating a bound less than origin as
|
||||
* unbounded, and also by treating "infinite" as equivalent to
|
||||
* Long.MAX_VALUE. For splits, it uses the standard divide-by-two
|
||||
* approach. The long and double versions of this class are
|
||||
* identical except for types.
|
||||
*/
|
||||
private static final class RandomIntsSpliterator
|
||||
implements Spliterator.OfInt {
|
||||
final SplittableRandom rng;
|
||||
long index;
|
||||
final long fence;
|
||||
final int origin;
|
||||
final int bound;
|
||||
RandomIntsSpliterator(SplittableRandom rng, long index, long fence,
|
||||
int origin, int bound) {
|
||||
this.rng = rng; this.index = index; this.fence = fence;
|
||||
this.origin = origin; this.bound = bound;
|
||||
}
|
||||
|
||||
public RandomIntsSpliterator trySplit() {
|
||||
long i = index, m = (i + fence) >>> 1;
|
||||
return (m <= i) ? null :
|
||||
new RandomIntsSpliterator(rng.split(), i, index = m, origin, bound);
|
||||
}
|
||||
|
||||
public long estimateSize() {
|
||||
return fence - index;
|
||||
}
|
||||
|
||||
public int characteristics() {
|
||||
return (Spliterator.SIZED | Spliterator.SUBSIZED |
|
||||
Spliterator.NONNULL | Spliterator.IMMUTABLE);
|
||||
}
|
||||
|
||||
public boolean tryAdvance(IntConsumer consumer) {
|
||||
if (consumer == null) throw new NullPointerException();
|
||||
long i = index, f = fence;
|
||||
if (i < f) {
|
||||
consumer.accept(rng.internalNextInt(origin, bound));
|
||||
index = i + 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void forEachRemaining(IntConsumer consumer) {
|
||||
if (consumer == null) throw new NullPointerException();
|
||||
long i = index, f = fence;
|
||||
if (i < f) {
|
||||
index = f;
|
||||
SplittableRandom r = rng;
|
||||
int o = origin, b = bound;
|
||||
do {
|
||||
consumer.accept(r.internalNextInt(o, b));
|
||||
} while (++i < f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Spliterator for long streams.
|
||||
*/
|
||||
private static final class RandomLongsSpliterator
|
||||
implements Spliterator.OfLong {
|
||||
final SplittableRandom rng;
|
||||
long index;
|
||||
final long fence;
|
||||
final long origin;
|
||||
final long bound;
|
||||
RandomLongsSpliterator(SplittableRandom rng, long index, long fence,
|
||||
long origin, long bound) {
|
||||
this.rng = rng; this.index = index; this.fence = fence;
|
||||
this.origin = origin; this.bound = bound;
|
||||
}
|
||||
|
||||
public RandomLongsSpliterator trySplit() {
|
||||
long i = index, m = (i + fence) >>> 1;
|
||||
return (m <= i) ? null :
|
||||
new RandomLongsSpliterator(rng.split(), i, index = m, origin, bound);
|
||||
}
|
||||
|
||||
public long estimateSize() {
|
||||
return fence - index;
|
||||
}
|
||||
|
||||
public int characteristics() {
|
||||
return (Spliterator.SIZED | Spliterator.SUBSIZED |
|
||||
Spliterator.NONNULL | Spliterator.IMMUTABLE);
|
||||
}
|
||||
|
||||
public boolean tryAdvance(LongConsumer consumer) {
|
||||
if (consumer == null) throw new NullPointerException();
|
||||
long i = index, f = fence;
|
||||
if (i < f) {
|
||||
consumer.accept(rng.internalNextLong(origin, bound));
|
||||
index = i + 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void forEachRemaining(LongConsumer consumer) {
|
||||
if (consumer == null) throw new NullPointerException();
|
||||
long i = index, f = fence;
|
||||
if (i < f) {
|
||||
index = f;
|
||||
SplittableRandom r = rng;
|
||||
long o = origin, b = bound;
|
||||
do {
|
||||
consumer.accept(r.internalNextLong(o, b));
|
||||
} while (++i < f);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Spliterator for double streams.
|
||||
*/
|
||||
private static final class RandomDoublesSpliterator
|
||||
implements Spliterator.OfDouble {
|
||||
final SplittableRandom rng;
|
||||
long index;
|
||||
final long fence;
|
||||
final double origin;
|
||||
final double bound;
|
||||
RandomDoublesSpliterator(SplittableRandom rng, long index, long fence,
|
||||
double origin, double bound) {
|
||||
this.rng = rng; this.index = index; this.fence = fence;
|
||||
this.origin = origin; this.bound = bound;
|
||||
}
|
||||
|
||||
public RandomDoublesSpliterator trySplit() {
|
||||
long i = index, m = (i + fence) >>> 1;
|
||||
return (m <= i) ? null :
|
||||
new RandomDoublesSpliterator(rng.split(), i, index = m, origin, bound);
|
||||
}
|
||||
|
||||
public long estimateSize() {
|
||||
return fence - index;
|
||||
}
|
||||
|
||||
public int characteristics() {
|
||||
return (Spliterator.SIZED | Spliterator.SUBSIZED |
|
||||
Spliterator.NONNULL | Spliterator.IMMUTABLE);
|
||||
}
|
||||
|
||||
public boolean tryAdvance(DoubleConsumer consumer) {
|
||||
if (consumer == null) throw new NullPointerException();
|
||||
long i = index, f = fence;
|
||||
if (i < f) {
|
||||
consumer.accept(rng.internalNextDouble(origin, bound));
|
||||
index = i + 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void forEachRemaining(DoubleConsumer consumer) {
|
||||
if (consumer == null) throw new NullPointerException();
|
||||
long i = index, f = fence;
|
||||
if (i < f) {
|
||||
index = f;
|
||||
SplittableRandom r = rng;
|
||||
double o = origin, b = bound;
|
||||
do {
|
||||
consumer.accept(r.internalNextDouble(o, b));
|
||||
} while (++i < f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue