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