1 |
dl |
1.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 |
dl |
1.12 |
import java.security.SecureRandom; |
29 |
dl |
1.1 |
import java.util.concurrent.atomic.AtomicLong; |
30 |
|
|
import java.util.Spliterator; |
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 SplittableRandom supports methods for |
43 |
jsr166 |
1.3 |
* producing pseudorandom numbers of type {@code int}, {@code long}, |
44 |
dl |
1.1 |
* and {@code double} with similar usages as for class |
45 |
jsr166 |
1.9 |
* {@link java.util.Random} but differs in the following ways: |
46 |
|
|
* |
47 |
|
|
* <ul> |
48 |
dl |
1.1 |
* |
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 |
dl |
1.11 |
* 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 |
dl |
1.1 |
* |
59 |
|
|
* <li> Method {@link #split} constructs and returns a new |
60 |
|
|
* SplittableRandom instance that shares no mutable state with the |
61 |
dl |
1.7 |
* current instance. However, with very high probability, the |
62 |
|
|
* values collectively generated by the two objects have the same |
63 |
dl |
1.1 |
* 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 |
|
|
* @author Guy Steele |
81 |
dl |
1.2 |
* @author Doug Lea |
82 |
dl |
1.1 |
* @since 1.8 |
83 |
|
|
*/ |
84 |
|
|
public class SplittableRandom { |
85 |
|
|
|
86 |
|
|
/* |
87 |
|
|
* File organization: First the non-public methods that constitute |
88 |
|
|
* the main algorithm, then the main public methods, followed by |
89 |
|
|
* some custom spliterator classes needed for stream methods. |
90 |
|
|
* |
91 |
|
|
* Credits: Primary algorithm and code by Guy Steele. Stream |
92 |
|
|
* support methods by Doug Lea. Documentation jointly produced |
93 |
|
|
* with additional help from Brian Goetz. |
94 |
|
|
*/ |
95 |
|
|
|
96 |
|
|
/* |
97 |
|
|
* Implementation Overview. |
98 |
|
|
* |
99 |
|
|
* This algorithm was inspired by the "DotMix" algorithm by |
100 |
|
|
* Leiserson, Schardl, and Sukha "Deterministic Parallel |
101 |
|
|
* Random-Number Generation for Dynamic-Multithreading Platforms", |
102 |
|
|
* PPoPP 2012, but improves and extends it in several ways. |
103 |
|
|
* |
104 |
dl |
1.7 |
* The primary update step (see method nextSeed()) is simply to |
105 |
|
|
* add a constant ("gamma") to the current seed, modulo a prime |
106 |
|
|
* ("George"). However, the nextLong and nextInt methods do not |
107 |
|
|
* return this value, but instead the results of bit-mixing |
108 |
|
|
* transformations that produce more uniformly distributed |
109 |
|
|
* sequences. |
110 |
dl |
1.1 |
* |
111 |
|
|
* "George" is the otherwise nameless (because it cannot be |
112 |
|
|
* represented) prime number 2^64+13. Using a prime number larger |
113 |
|
|
* than can fit in a long ensures that all possible long values |
114 |
|
|
* can occur, plus 13 others that just get skipped over when they |
115 |
|
|
* are encountered; see method addGammaModGeorge. For this to |
116 |
|
|
* work, initial gamma values must be at least 13. |
117 |
|
|
* |
118 |
|
|
* The mix64 bit-mixing function called by nextLong and other |
119 |
|
|
* methods computes the same value as the "64-bit finalizer" |
120 |
|
|
* function in Austin Appleby's MurmurHash3 algorithm. See |
121 |
|
|
* http://code.google.com/p/smhasher/wiki/MurmurHash3 , which |
122 |
|
|
* comments: "The constants for the finalizers were generated by a |
123 |
|
|
* simple simulated-annealing algorithm, and both avalanche all |
124 |
dl |
1.13 |
* bits of 'h' to within 0.25% bias." |
125 |
|
|
* |
126 |
|
|
* The value of gamma differs for each instance across a series of |
127 |
|
|
* splits, and is generated using an independent variant of the |
128 |
|
|
* same algorithm, but operating across calls to split(), not |
129 |
|
|
* calls to nextSeed(): Each instance carries the state of this |
130 |
|
|
* generator as nextSplit. Gammas are treated as 57bit values, |
131 |
|
|
* advancing by adding GAMMA_GAMMA mod GAMMA_PRIME, and bit-mixed |
132 |
dl |
1.14 |
* with a 57-bit version of mix, using the "Mix13" multiplicative |
133 |
dl |
1.13 |
* constants for MurmurHash3 described by David Stafford |
134 |
|
|
* (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html). |
135 |
|
|
* The value of GAMMA_GAMMA is arbitrary (except must be at least |
136 |
|
|
* 13 and less than GAMMA_PRIME), but because it serves as the |
137 |
|
|
* base of split sequences, should be subject to validation of |
138 |
|
|
* consequent random number quality metrics. |
139 |
dl |
1.1 |
* |
140 |
|
|
* The mix32 function used for nextInt just consists of two of the |
141 |
dl |
1.13 |
* five lines of mix64; avalanche testing shows that the 64-bit |
142 |
|
|
* result has its top 32 bits avalanched well, though not the |
143 |
|
|
* bottom 32 bits. DieHarder tests show that it is adequate for |
144 |
|
|
* generating one random int from the 64-bit result of nextSeed. |
145 |
dl |
1.1 |
* |
146 |
|
|
* Support for the default (no-argument) constructor relies on an |
147 |
|
|
* AtomicLong (defaultSeedGenerator) to help perform the |
148 |
|
|
* equivalent of a split of a statically constructed |
149 |
|
|
* SplittableRandom. Unlike other cases, this split must be |
150 |
|
|
* performed in a thread-safe manner. We use |
151 |
|
|
* AtomicLong.compareAndSet as the (typically) most efficient |
152 |
dl |
1.12 |
* mechanism. To bootstrap, we start off using a SecureRandom |
153 |
|
|
* initial default seed, and update using a fixed |
154 |
|
|
* DEFAULT_SEED_GAMMA. The default constructor uses GAMMA_GAMMA, |
155 |
|
|
* not 0, for its splitSeed argument (addGammaModGeorge(0, |
156 |
|
|
* GAMMA_GAMMA) == GAMMA_GAMMA) to reflect that each is split from |
157 |
|
|
* this root generator, even though the root is not explicitly |
158 |
|
|
* represented as a SplittableRandom. |
159 |
dl |
1.1 |
*/ |
160 |
|
|
|
161 |
|
|
/** |
162 |
dl |
1.13 |
* The prime modulus for gamma values. |
163 |
|
|
*/ |
164 |
|
|
private static final long GAMMA_PRIME = (1L << 57) - 13L; |
165 |
|
|
|
166 |
|
|
/** |
167 |
dl |
1.12 |
* The value for producing new gamma values. Must be greater or |
168 |
dl |
1.13 |
* equal to 13 and less than GAMMA_PRIME. Otherwise, the value is |
169 |
|
|
* arbitrary subject to validation of the resulting statistical |
170 |
|
|
* quality of splits. |
171 |
dl |
1.1 |
*/ |
172 |
dl |
1.13 |
private static final long GAMMA_GAMMA = 0x00aae38294f712aabL; |
173 |
dl |
1.1 |
|
174 |
|
|
/** |
175 |
dl |
1.12 |
* The seed update value for default constructors. Must be |
176 |
dl |
1.13 |
* greater or equal to 13. Otherwise, the value is arbitrary |
177 |
|
|
* subject to quality checks. |
178 |
dl |
1.1 |
*/ |
179 |
dl |
1.13 |
private static final long DEFAULT_SEED_GAMMA = 0x9e3779b97f4a7c15L; |
180 |
dl |
1.1 |
|
181 |
|
|
/** |
182 |
dl |
1.11 |
* The value 13 with 64bit sign bit set. Used in the signed |
183 |
|
|
* comparison in addGammaModGeorge. |
184 |
|
|
*/ |
185 |
|
|
private static final long BOTTOM13 = 0x800000000000000DL; |
186 |
|
|
|
187 |
|
|
/** |
188 |
dl |
1.5 |
* The least non-zero value returned by nextDouble(). This value |
189 |
dl |
1.7 |
* is scaled by a random value of 53 bits to produce a result. |
190 |
dl |
1.5 |
*/ |
191 |
|
|
private static final double DOUBLE_UNIT = 1.0 / (1L << 53); |
192 |
|
|
|
193 |
|
|
/** |
194 |
dl |
1.1 |
* The next seed for default constructors. |
195 |
|
|
*/ |
196 |
|
|
private static final AtomicLong defaultSeedGenerator = |
197 |
dl |
1.12 |
new AtomicLong(getInitialDefaultSeed()); |
198 |
dl |
1.1 |
|
199 |
|
|
/** |
200 |
|
|
* The seed, updated only via method nextSeed. |
201 |
|
|
*/ |
202 |
|
|
private long seed; |
203 |
|
|
|
204 |
|
|
/** |
205 |
|
|
* The constant value added to seed (mod George) on each update. |
206 |
|
|
*/ |
207 |
|
|
private final long gamma; |
208 |
|
|
|
209 |
|
|
/** |
210 |
|
|
* The next seed to use for splits. Propagated using |
211 |
|
|
* addGammaModGeorge across instances. |
212 |
|
|
*/ |
213 |
|
|
private final long nextSplit; |
214 |
|
|
|
215 |
|
|
/** |
216 |
|
|
* Adds the given gamma value, g, to the given seed value s, mod |
217 |
|
|
* George (2^64+13). We regard s and g as unsigned values |
218 |
|
|
* (ranging from 0 to 2^64-1). We add g to s either once or twice |
219 |
|
|
* (mod George) as necessary to produce an (unsigned) result less |
220 |
|
|
* than 2^64. We require that g must be at least 13. This |
221 |
|
|
* guarantees that if (s+g) mod George >= 2^64 then (s+g+g) mod |
222 |
|
|
* George < 2^64; thus we need only a conditional, not a loop, |
223 |
|
|
* to be sure of getting a representable value. |
224 |
|
|
* |
225 |
dl |
1.11 |
* Because Java comparison operators are signed, we implement this |
226 |
|
|
* by conceptually offsetting seed values downwards by 2^63, so |
227 |
|
|
* 0..13 is represented as Long.MIN_VALUE..BOTTOM13. |
228 |
|
|
* |
229 |
|
|
* @param s a seed value, viewed as a signed long |
230 |
dl |
1.1 |
* @param g a gamma value, 13 <= g (as unsigned) |
231 |
|
|
*/ |
232 |
|
|
private static long addGammaModGeorge(long s, long g) { |
233 |
|
|
long p = s + g; |
234 |
dl |
1.11 |
return (p >= s) ? p : ((p >= BOTTOM13) ? p : p + g) - 13L; |
235 |
dl |
1.1 |
} |
236 |
|
|
|
237 |
|
|
/** |
238 |
|
|
* Returns a bit-mixed transformation of its argument. |
239 |
|
|
* See above for explanation. |
240 |
|
|
*/ |
241 |
|
|
private static long mix64(long z) { |
242 |
|
|
z ^= (z >>> 33); |
243 |
|
|
z *= 0xff51afd7ed558ccdL; |
244 |
|
|
z ^= (z >>> 33); |
245 |
|
|
z *= 0xc4ceb9fe1a85ec53L; |
246 |
|
|
z ^= (z >>> 33); |
247 |
|
|
return z; |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
/** |
251 |
|
|
* Returns a bit-mixed int transformation of its argument. |
252 |
|
|
* See above for explanation. |
253 |
|
|
*/ |
254 |
|
|
private static int mix32(long z) { |
255 |
|
|
z ^= (z >>> 33); |
256 |
|
|
z *= 0xc4ceb9fe1a85ec53L; |
257 |
|
|
return (int)(z >>> 32); |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
/** |
261 |
dl |
1.13 |
* Returns a 57-bit mixed transformation of its argument. See |
262 |
|
|
* above for explanation. |
263 |
|
|
*/ |
264 |
|
|
private static long mix57(long z) { |
265 |
dl |
1.14 |
z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L; |
266 |
dl |
1.13 |
z &= 0x01FFFFFFFFFFFFFFL; |
267 |
dl |
1.14 |
z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL; |
268 |
dl |
1.13 |
z &= 0x01FFFFFFFFFFFFFFL; |
269 |
dl |
1.14 |
z ^= (z >>> 31); |
270 |
dl |
1.13 |
return z; |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
/** |
274 |
dl |
1.7 |
* Internal constructor used by all other constructors and by |
275 |
|
|
* method split. Establishes the initial seed for this instance, |
276 |
|
|
* and uses the given splitSeed to establish gamma, as well as the |
277 |
|
|
* nextSplit to use by this instance. The loop to skip ineligible |
278 |
|
|
* gammas very rarely iterates, and does so at most 13 times. |
279 |
|
|
*/ |
280 |
|
|
private SplittableRandom(long seed, long splitSeed) { |
281 |
|
|
this.seed = seed; |
282 |
|
|
long s = splitSeed, g; |
283 |
|
|
do { // ensure gamma >= 13, considered as an unsigned integer |
284 |
dl |
1.13 |
s += GAMMA_GAMMA; |
285 |
|
|
if (s >= GAMMA_PRIME) |
286 |
|
|
s -= GAMMA_PRIME; |
287 |
|
|
g = mix57(s); |
288 |
|
|
} while (g < 13L); |
289 |
dl |
1.7 |
this.gamma = g; |
290 |
|
|
this.nextSplit = s; |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
/** |
294 |
|
|
* Updates in-place and returns seed. |
295 |
|
|
* See above for explanation. |
296 |
|
|
*/ |
297 |
|
|
private long nextSeed() { |
298 |
|
|
return seed = addGammaModGeorge(seed, gamma); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
/** |
302 |
|
|
* Atomically updates and returns next seed for default constructor. |
303 |
dl |
1.1 |
*/ |
304 |
|
|
private static long nextDefaultSeed() { |
305 |
|
|
long oldSeed, newSeed; |
306 |
|
|
do { |
307 |
|
|
oldSeed = defaultSeedGenerator.get(); |
308 |
|
|
newSeed = addGammaModGeorge(oldSeed, DEFAULT_SEED_GAMMA); |
309 |
|
|
} while (!defaultSeedGenerator.compareAndSet(oldSeed, newSeed)); |
310 |
|
|
return mix64(newSeed); |
311 |
|
|
} |
312 |
|
|
|
313 |
dl |
1.12 |
/** |
314 |
|
|
* Returns an initial default seed. |
315 |
|
|
*/ |
316 |
|
|
private static long getInitialDefaultSeed() { |
317 |
|
|
byte[] seedBytes = java.security.SecureRandom.getSeed(8); |
318 |
|
|
long s = (long)(seedBytes[0]) & 0xffL; |
319 |
|
|
for (int i = 1; i < 8; ++i) |
320 |
|
|
s = (s << 8) | ((long)(seedBytes[i]) & 0xffL); |
321 |
|
|
return s; |
322 |
|
|
} |
323 |
|
|
|
324 |
dl |
1.1 |
/* |
325 |
|
|
* Internal versions of nextX methods used by streams, as well as |
326 |
|
|
* the public nextX(origin, bound) methods. These exist mainly to |
327 |
|
|
* avoid the need for multiple versions of stream spliterators |
328 |
|
|
* across the different exported forms of streams. |
329 |
|
|
*/ |
330 |
|
|
|
331 |
|
|
/** |
332 |
|
|
* The form of nextLong used by LongStream Spliterators. If |
333 |
|
|
* origin is greater than bound, acts as unbounded form of |
334 |
|
|
* nextLong, else as bounded form. |
335 |
|
|
* |
336 |
|
|
* @param origin the least value, unless greater than bound |
337 |
|
|
* @param bound the upper bound (exclusive), must not equal origin |
338 |
|
|
* @return a pseudorandom value |
339 |
|
|
*/ |
340 |
|
|
final long internalNextLong(long origin, long bound) { |
341 |
|
|
/* |
342 |
|
|
* Four Cases: |
343 |
|
|
* |
344 |
|
|
* 1. If the arguments indicate unbounded form, act as |
345 |
|
|
* nextLong(). |
346 |
|
|
* |
347 |
|
|
* 2. If the range is an exact power of two, apply the |
348 |
|
|
* associated bit mask. |
349 |
|
|
* |
350 |
|
|
* 3. If the range is positive, loop to avoid potential bias |
351 |
|
|
* when the implicit nextLong() bound (2<sup>64</sup>) is not |
352 |
|
|
* evenly divisible by the range. The loop rejects candidates |
353 |
|
|
* computed from otherwise over-represented values. The |
354 |
|
|
* expected number of iterations under an ideal generator |
355 |
dl |
1.4 |
* varies from 1 to 2, depending on the bound. The loop itself |
356 |
|
|
* takes an unlovable form. Because the first candidate is |
357 |
|
|
* already available, we need a break-in-the-middle |
358 |
|
|
* construction, which is concisely but cryptically performed |
359 |
|
|
* within the while-condition of a body-less for loop. |
360 |
dl |
1.1 |
* |
361 |
|
|
* 4. Otherwise, the range cannot be represented as a positive |
362 |
dl |
1.4 |
* long. The loop repeatedly generates unbounded longs until |
363 |
|
|
* obtaining a candidate meeting constraints (with an expected |
364 |
|
|
* number of iterations of less than two). |
365 |
dl |
1.1 |
*/ |
366 |
|
|
|
367 |
|
|
long r = mix64(nextSeed()); |
368 |
|
|
if (origin < bound) { |
369 |
|
|
long n = bound - origin, m = n - 1; |
370 |
dl |
1.7 |
if ((n & m) == 0L) // power of two |
371 |
dl |
1.1 |
r = (r & m) + origin; |
372 |
dl |
1.7 |
else if (n > 0L) { // reject over-represented candidates |
373 |
dl |
1.1 |
for (long u = r >>> 1; // ensure nonnegative |
374 |
dl |
1.7 |
u + m - (r = u % n) < 0L; // rejection check |
375 |
dl |
1.1 |
u = mix64(nextSeed()) >>> 1) // retry |
376 |
|
|
; |
377 |
|
|
r += origin; |
378 |
|
|
} |
379 |
dl |
1.7 |
else { // range not representable as long |
380 |
dl |
1.1 |
while (r < origin || r >= bound) |
381 |
|
|
r = mix64(nextSeed()); |
382 |
|
|
} |
383 |
|
|
} |
384 |
|
|
return r; |
385 |
|
|
} |
386 |
|
|
|
387 |
|
|
/** |
388 |
|
|
* The form of nextInt used by IntStream Spliterators. |
389 |
|
|
* Exactly the same as long version, except for types. |
390 |
|
|
* |
391 |
|
|
* @param origin the least value, unless greater than bound |
392 |
|
|
* @param bound the upper bound (exclusive), must not equal origin |
393 |
|
|
* @return a pseudorandom value |
394 |
|
|
*/ |
395 |
|
|
final int internalNextInt(int origin, int bound) { |
396 |
|
|
int r = mix32(nextSeed()); |
397 |
|
|
if (origin < bound) { |
398 |
|
|
int n = bound - origin, m = n - 1; |
399 |
dl |
1.13 |
if ((n & m) == 0) |
400 |
dl |
1.1 |
r = (r & m) + origin; |
401 |
|
|
else if (n > 0) { |
402 |
|
|
for (int u = r >>> 1; |
403 |
dl |
1.7 |
u + m - (r = u % n) < 0; |
404 |
dl |
1.1 |
u = mix32(nextSeed()) >>> 1) |
405 |
|
|
; |
406 |
|
|
r += origin; |
407 |
|
|
} |
408 |
|
|
else { |
409 |
|
|
while (r < origin || r >= bound) |
410 |
|
|
r = mix32(nextSeed()); |
411 |
|
|
} |
412 |
|
|
} |
413 |
|
|
return r; |
414 |
|
|
} |
415 |
|
|
|
416 |
|
|
/** |
417 |
|
|
* The form of nextDouble used by DoubleStream Spliterators. |
418 |
|
|
* |
419 |
|
|
* @param origin the least value, unless greater than bound |
420 |
|
|
* @param bound the upper bound (exclusive), must not equal origin |
421 |
|
|
* @return a pseudorandom value |
422 |
|
|
*/ |
423 |
|
|
final double internalNextDouble(double origin, double bound) { |
424 |
dl |
1.5 |
double r = (nextLong() >>> 11) * DOUBLE_UNIT; |
425 |
dl |
1.1 |
if (origin < bound) { |
426 |
|
|
r = r * (bound - origin) + origin; |
427 |
dl |
1.7 |
if (r >= bound) // correct for rounding |
428 |
dl |
1.1 |
r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1); |
429 |
|
|
} |
430 |
|
|
return r; |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
/* ---------------- public methods ---------------- */ |
434 |
|
|
|
435 |
|
|
/** |
436 |
dl |
1.7 |
* Creates a new SplittableRandom instance using the specified |
437 |
|
|
* initial seed. SplittableRandom instances created with the same |
438 |
dl |
1.11 |
* seed in the same program generate identical sequences of values. |
439 |
dl |
1.1 |
* |
440 |
|
|
* @param seed the initial seed |
441 |
|
|
*/ |
442 |
|
|
public SplittableRandom(long seed) { |
443 |
dl |
1.13 |
this(seed, 0L); |
444 |
dl |
1.1 |
} |
445 |
|
|
|
446 |
|
|
/** |
447 |
|
|
* Creates a new SplittableRandom instance that is likely to |
448 |
|
|
* generate sequences of values that are statistically independent |
449 |
|
|
* of those of any other instances in the current program; and |
450 |
|
|
* may, and typically does, vary across program invocations. |
451 |
|
|
*/ |
452 |
|
|
public SplittableRandom() { |
453 |
|
|
this(nextDefaultSeed(), GAMMA_GAMMA); |
454 |
|
|
} |
455 |
|
|
|
456 |
|
|
/** |
457 |
|
|
* Constructs and returns a new SplittableRandom instance that |
458 |
|
|
* shares no mutable state with this instance. However, with very |
459 |
|
|
* high probability, the set of values collectively generated by |
460 |
|
|
* the two objects has the same statistical properties as if the |
461 |
|
|
* same quantity of values were generated by a single thread using |
462 |
|
|
* a single SplittableRandom object. Either or both of the two |
463 |
|
|
* objects may be further split using the {@code split()} method, |
464 |
|
|
* and the same expected statistical properties apply to the |
465 |
|
|
* entire set of generators constructed by such recursive |
466 |
|
|
* splitting. |
467 |
|
|
* |
468 |
|
|
* @return the new SplittableRandom instance |
469 |
|
|
*/ |
470 |
|
|
public SplittableRandom split() { |
471 |
|
|
return new SplittableRandom(nextSeed(), nextSplit); |
472 |
|
|
} |
473 |
|
|
|
474 |
|
|
/** |
475 |
|
|
* Returns a pseudorandom {@code int} value. |
476 |
|
|
* |
477 |
dl |
1.7 |
* @return a pseudorandom {@code int} value |
478 |
dl |
1.1 |
*/ |
479 |
|
|
public int nextInt() { |
480 |
|
|
return mix32(nextSeed()); |
481 |
|
|
} |
482 |
|
|
|
483 |
|
|
/** |
484 |
dl |
1.7 |
* Returns a pseudorandom {@code int} value between zero (inclusive) |
485 |
dl |
1.1 |
* and the specified bound (exclusive). |
486 |
|
|
* |
487 |
|
|
* @param bound the bound on the random number to be returned. Must be |
488 |
|
|
* positive. |
489 |
dl |
1.7 |
* @return a pseudorandom {@code int} value between zero |
490 |
jsr166 |
1.10 |
* (inclusive) and the bound (exclusive) |
491 |
dl |
1.7 |
* @throws IllegalArgumentException if the bound is less than zero |
492 |
dl |
1.1 |
*/ |
493 |
|
|
public int nextInt(int bound) { |
494 |
|
|
if (bound <= 0) |
495 |
|
|
throw new IllegalArgumentException("bound must be positive"); |
496 |
|
|
// Specialize internalNextInt for origin 0 |
497 |
|
|
int r = mix32(nextSeed()); |
498 |
|
|
int m = bound - 1; |
499 |
dl |
1.13 |
if ((bound & m) == 0) // power of two |
500 |
dl |
1.1 |
r &= m; |
501 |
|
|
else { // reject over-represented candidates |
502 |
|
|
for (int u = r >>> 1; |
503 |
dl |
1.7 |
u + m - (r = u % bound) < 0; |
504 |
dl |
1.1 |
u = mix32(nextSeed()) >>> 1) |
505 |
|
|
; |
506 |
|
|
} |
507 |
|
|
return r; |
508 |
|
|
} |
509 |
|
|
|
510 |
|
|
/** |
511 |
|
|
* Returns a pseudorandom {@code int} value between the specified |
512 |
|
|
* origin (inclusive) and the specified bound (exclusive). |
513 |
|
|
* |
514 |
|
|
* @param origin the least value returned |
515 |
|
|
* @param bound the upper bound (exclusive) |
516 |
|
|
* @return a pseudorandom {@code int} value between the origin |
517 |
jsr166 |
1.10 |
* (inclusive) and the bound (exclusive) |
518 |
dl |
1.7 |
* @throws IllegalArgumentException if {@code origin} is greater than |
519 |
dl |
1.1 |
* or equal to {@code bound} |
520 |
|
|
*/ |
521 |
|
|
public int nextInt(int origin, int bound) { |
522 |
|
|
if (origin >= bound) |
523 |
|
|
throw new IllegalArgumentException("bound must be greater than origin"); |
524 |
|
|
return internalNextInt(origin, bound); |
525 |
|
|
} |
526 |
|
|
|
527 |
|
|
/** |
528 |
|
|
* Returns a pseudorandom {@code long} value. |
529 |
|
|
* |
530 |
dl |
1.7 |
* @return a pseudorandom {@code long} value |
531 |
dl |
1.1 |
*/ |
532 |
|
|
public long nextLong() { |
533 |
|
|
return mix64(nextSeed()); |
534 |
|
|
} |
535 |
|
|
|
536 |
|
|
/** |
537 |
dl |
1.7 |
* Returns a pseudorandom {@code long} value between zero (inclusive) |
538 |
dl |
1.1 |
* and the specified bound (exclusive). |
539 |
|
|
* |
540 |
|
|
* @param bound the bound on the random number to be returned. Must be |
541 |
|
|
* positive. |
542 |
dl |
1.7 |
* @return a pseudorandom {@code long} value between zero |
543 |
jsr166 |
1.10 |
* (inclusive) and the bound (exclusive) |
544 |
dl |
1.7 |
* @throws IllegalArgumentException if {@code bound} is less than zero |
545 |
dl |
1.1 |
*/ |
546 |
|
|
public long nextLong(long bound) { |
547 |
|
|
if (bound <= 0) |
548 |
|
|
throw new IllegalArgumentException("bound must be positive"); |
549 |
|
|
// Specialize internalNextLong for origin 0 |
550 |
|
|
long r = mix64(nextSeed()); |
551 |
|
|
long m = bound - 1; |
552 |
|
|
if ((bound & m) == 0L) // power of two |
553 |
|
|
r &= m; |
554 |
|
|
else { // reject over-represented candidates |
555 |
|
|
for (long u = r >>> 1; |
556 |
|
|
u + m - (r = u % bound) < 0L; |
557 |
|
|
u = mix64(nextSeed()) >>> 1) |
558 |
|
|
; |
559 |
|
|
} |
560 |
|
|
return r; |
561 |
|
|
} |
562 |
|
|
|
563 |
|
|
/** |
564 |
|
|
* Returns a pseudorandom {@code long} value between the specified |
565 |
|
|
* origin (inclusive) and the specified bound (exclusive). |
566 |
|
|
* |
567 |
|
|
* @param origin the least value returned |
568 |
|
|
* @param bound the upper bound (exclusive) |
569 |
|
|
* @return a pseudorandom {@code long} value between the origin |
570 |
jsr166 |
1.10 |
* (inclusive) and the bound (exclusive) |
571 |
dl |
1.7 |
* @throws IllegalArgumentException if {@code origin} is greater than |
572 |
dl |
1.1 |
* or equal to {@code bound} |
573 |
|
|
*/ |
574 |
|
|
public long nextLong(long origin, long bound) { |
575 |
|
|
if (origin >= bound) |
576 |
|
|
throw new IllegalArgumentException("bound must be greater than origin"); |
577 |
|
|
return internalNextLong(origin, bound); |
578 |
|
|
} |
579 |
|
|
|
580 |
|
|
/** |
581 |
dl |
1.7 |
* Returns a pseudorandom {@code double} value between zero |
582 |
|
|
* (inclusive) and one (exclusive). |
583 |
dl |
1.1 |
* |
584 |
dl |
1.7 |
* @return a pseudorandom {@code double} value between zero |
585 |
|
|
* (inclusive) and one (exclusive) |
586 |
dl |
1.1 |
*/ |
587 |
|
|
public double nextDouble() { |
588 |
dl |
1.11 |
return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT; |
589 |
dl |
1.1 |
} |
590 |
|
|
|
591 |
|
|
/** |
592 |
|
|
* Returns a pseudorandom {@code double} value between 0.0 |
593 |
|
|
* (inclusive) and the specified bound (exclusive). |
594 |
|
|
* |
595 |
|
|
* @param bound the bound on the random number to be returned. Must be |
596 |
|
|
* positive. |
597 |
dl |
1.7 |
* @return a pseudorandom {@code double} value between zero |
598 |
jsr166 |
1.10 |
* (inclusive) and the bound (exclusive) |
599 |
dl |
1.7 |
* @throws IllegalArgumentException if {@code bound} is less than zero |
600 |
dl |
1.1 |
*/ |
601 |
|
|
public double nextDouble(double bound) { |
602 |
dl |
1.7 |
if (!(bound > 0.0)) |
603 |
dl |
1.1 |
throw new IllegalArgumentException("bound must be positive"); |
604 |
dl |
1.11 |
double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound; |
605 |
dl |
1.1 |
return (result < bound) ? result : // correct for rounding |
606 |
|
|
Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1); |
607 |
|
|
} |
608 |
|
|
|
609 |
|
|
/** |
610 |
dl |
1.7 |
* Returns a pseudorandom {@code double} value between the specified |
611 |
dl |
1.1 |
* origin (inclusive) and bound (exclusive). |
612 |
|
|
* |
613 |
|
|
* @param origin the least value returned |
614 |
|
|
* @param bound the upper bound |
615 |
|
|
* @return a pseudorandom {@code double} value between the origin |
616 |
jsr166 |
1.10 |
* (inclusive) and the bound (exclusive) |
617 |
dl |
1.1 |
* @throws IllegalArgumentException if {@code origin} is greater than |
618 |
|
|
* or equal to {@code bound} |
619 |
|
|
*/ |
620 |
|
|
public double nextDouble(double origin, double bound) { |
621 |
dl |
1.7 |
if (!(origin < bound)) |
622 |
dl |
1.1 |
throw new IllegalArgumentException("bound must be greater than origin"); |
623 |
|
|
return internalNextDouble(origin, bound); |
624 |
|
|
} |
625 |
|
|
|
626 |
dl |
1.11 |
/** |
627 |
|
|
* Returns a pseudorandom {@code boolean} value. |
628 |
|
|
* |
629 |
|
|
* @return a pseudorandom {@code boolean} value |
630 |
|
|
*/ |
631 |
|
|
public boolean nextBoolean() { |
632 |
|
|
return mix32(nextSeed()) < 0; |
633 |
|
|
} |
634 |
|
|
|
635 |
dl |
1.1 |
// stream methods, coded in a way intended to better isolate for |
636 |
|
|
// maintenance purposes the small differences across forms. |
637 |
|
|
|
638 |
|
|
/** |
639 |
dl |
1.7 |
* Returns a stream producing the given {@code streamSize} number of |
640 |
dl |
1.1 |
* pseudorandom {@code int} values. |
641 |
|
|
* |
642 |
|
|
* @param streamSize the number of values to generate |
643 |
|
|
* @return a stream of pseudorandom {@code int} values |
644 |
|
|
* @throws IllegalArgumentException if {@code streamSize} is |
645 |
dl |
1.7 |
* less than zero |
646 |
dl |
1.1 |
*/ |
647 |
|
|
public IntStream ints(long streamSize) { |
648 |
|
|
if (streamSize < 0L) |
649 |
|
|
throw new IllegalArgumentException("negative Stream size"); |
650 |
|
|
return StreamSupport.intStream |
651 |
|
|
(new RandomIntsSpliterator |
652 |
|
|
(this, 0L, streamSize, Integer.MAX_VALUE, 0), |
653 |
|
|
false); |
654 |
|
|
} |
655 |
|
|
|
656 |
|
|
/** |
657 |
|
|
* Returns an effectively unlimited stream of pseudorandom {@code int} |
658 |
jsr166 |
1.10 |
* values. |
659 |
dl |
1.1 |
* |
660 |
|
|
* @implNote This method is implemented to be equivalent to {@code |
661 |
|
|
* ints(Long.MAX_VALUE)}. |
662 |
|
|
* |
663 |
|
|
* @return a stream of pseudorandom {@code int} values |
664 |
|
|
*/ |
665 |
|
|
public IntStream ints() { |
666 |
|
|
return StreamSupport.intStream |
667 |
|
|
(new RandomIntsSpliterator |
668 |
|
|
(this, 0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0), |
669 |
|
|
false); |
670 |
|
|
} |
671 |
|
|
|
672 |
|
|
/** |
673 |
dl |
1.7 |
* Returns a stream producing the given {@code streamSize} number of |
674 |
dl |
1.1 |
* pseudorandom {@code int} values, each conforming to the given |
675 |
|
|
* origin and bound. |
676 |
|
|
* |
677 |
|
|
* @param streamSize the number of values to generate |
678 |
|
|
* @param randomNumberOrigin the origin of each random value |
679 |
|
|
* @param randomNumberBound the bound of each random value |
680 |
|
|
* @return a stream of pseudorandom {@code int} values, |
681 |
jsr166 |
1.10 |
* each with the given origin and bound |
682 |
dl |
1.1 |
* @throws IllegalArgumentException if {@code streamSize} is |
683 |
dl |
1.7 |
* less than zero, or {@code randomNumberOrigin} |
684 |
dl |
1.1 |
* is greater than or equal to {@code randomNumberBound} |
685 |
|
|
*/ |
686 |
|
|
public IntStream ints(long streamSize, int randomNumberOrigin, |
687 |
|
|
int randomNumberBound) { |
688 |
|
|
if (streamSize < 0L) |
689 |
|
|
throw new IllegalArgumentException("negative Stream size"); |
690 |
|
|
if (randomNumberOrigin >= randomNumberBound) |
691 |
|
|
throw new IllegalArgumentException("bound must be greater than origin"); |
692 |
|
|
return StreamSupport.intStream |
693 |
|
|
(new RandomIntsSpliterator |
694 |
|
|
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound), |
695 |
|
|
false); |
696 |
|
|
} |
697 |
|
|
|
698 |
|
|
/** |
699 |
|
|
* Returns an effectively unlimited stream of pseudorandom {@code |
700 |
|
|
* int} values, each conforming to the given origin and bound. |
701 |
|
|
* |
702 |
|
|
* @implNote This method is implemented to be equivalent to {@code |
703 |
|
|
* ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
704 |
|
|
* |
705 |
|
|
* @param randomNumberOrigin the origin of each random value |
706 |
|
|
* @param randomNumberBound the bound of each random value |
707 |
|
|
* @return a stream of pseudorandom {@code int} values, |
708 |
jsr166 |
1.10 |
* each with the given origin and bound |
709 |
dl |
1.1 |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
710 |
|
|
* is greater than or equal to {@code randomNumberBound} |
711 |
|
|
*/ |
712 |
|
|
public IntStream ints(int randomNumberOrigin, int randomNumberBound) { |
713 |
|
|
if (randomNumberOrigin >= randomNumberBound) |
714 |
|
|
throw new IllegalArgumentException("bound must be greater than origin"); |
715 |
|
|
return StreamSupport.intStream |
716 |
|
|
(new RandomIntsSpliterator |
717 |
|
|
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
718 |
|
|
false); |
719 |
|
|
} |
720 |
|
|
|
721 |
|
|
/** |
722 |
dl |
1.7 |
* Returns a stream producing the given {@code streamSize} number of |
723 |
dl |
1.1 |
* pseudorandom {@code long} values. |
724 |
|
|
* |
725 |
|
|
* @param streamSize the number of values to generate |
726 |
dl |
1.7 |
* @return a stream of pseudorandom {@code long} values |
727 |
dl |
1.1 |
* @throws IllegalArgumentException if {@code streamSize} is |
728 |
dl |
1.7 |
* less than zero |
729 |
dl |
1.1 |
*/ |
730 |
|
|
public LongStream longs(long streamSize) { |
731 |
|
|
if (streamSize < 0L) |
732 |
|
|
throw new IllegalArgumentException("negative Stream size"); |
733 |
|
|
return StreamSupport.longStream |
734 |
|
|
(new RandomLongsSpliterator |
735 |
|
|
(this, 0L, streamSize, Long.MAX_VALUE, 0L), |
736 |
|
|
false); |
737 |
|
|
} |
738 |
|
|
|
739 |
|
|
/** |
740 |
|
|
* Returns an effectively unlimited stream of pseudorandom {@code long} |
741 |
|
|
* values. |
742 |
|
|
* |
743 |
|
|
* @implNote This method is implemented to be equivalent to {@code |
744 |
|
|
* longs(Long.MAX_VALUE)}. |
745 |
|
|
* |
746 |
|
|
* @return a stream of pseudorandom {@code long} values |
747 |
|
|
*/ |
748 |
|
|
public LongStream longs() { |
749 |
|
|
return StreamSupport.longStream |
750 |
|
|
(new RandomLongsSpliterator |
751 |
|
|
(this, 0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L), |
752 |
|
|
false); |
753 |
|
|
} |
754 |
|
|
|
755 |
|
|
/** |
756 |
dl |
1.7 |
* Returns a stream producing the given {@code streamSize} number of |
757 |
dl |
1.1 |
* pseudorandom {@code long} values, each conforming to the |
758 |
|
|
* given origin and bound. |
759 |
|
|
* |
760 |
|
|
* @param streamSize the number of values to generate |
761 |
|
|
* @param randomNumberOrigin the origin of each random value |
762 |
|
|
* @param randomNumberBound the bound of each random value |
763 |
|
|
* @return a stream of pseudorandom {@code long} values, |
764 |
jsr166 |
1.10 |
* each with the given origin and bound |
765 |
dl |
1.1 |
* @throws IllegalArgumentException if {@code streamSize} is |
766 |
dl |
1.7 |
* less than zero, or {@code randomNumberOrigin} |
767 |
dl |
1.1 |
* is greater than or equal to {@code randomNumberBound} |
768 |
|
|
*/ |
769 |
|
|
public LongStream longs(long streamSize, long randomNumberOrigin, |
770 |
|
|
long randomNumberBound) { |
771 |
|
|
if (streamSize < 0L) |
772 |
|
|
throw new IllegalArgumentException("negative Stream size"); |
773 |
|
|
if (randomNumberOrigin >= randomNumberBound) |
774 |
|
|
throw new IllegalArgumentException("bound must be greater than origin"); |
775 |
|
|
return StreamSupport.longStream |
776 |
|
|
(new RandomLongsSpliterator |
777 |
|
|
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound), |
778 |
|
|
false); |
779 |
|
|
} |
780 |
|
|
|
781 |
|
|
/** |
782 |
|
|
* Returns an effectively unlimited stream of pseudorandom {@code |
783 |
|
|
* long} values, each conforming to the given origin and bound. |
784 |
|
|
* |
785 |
|
|
* @implNote This method is implemented to be equivalent to {@code |
786 |
|
|
* longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
787 |
|
|
* |
788 |
|
|
* @param randomNumberOrigin the origin of each random value |
789 |
|
|
* @param randomNumberBound the bound of each random value |
790 |
|
|
* @return a stream of pseudorandom {@code long} values, |
791 |
jsr166 |
1.10 |
* each with the given origin and bound |
792 |
dl |
1.1 |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
793 |
|
|
* is greater than or equal to {@code randomNumberBound} |
794 |
|
|
*/ |
795 |
|
|
public LongStream longs(long randomNumberOrigin, long randomNumberBound) { |
796 |
|
|
if (randomNumberOrigin >= randomNumberBound) |
797 |
|
|
throw new IllegalArgumentException("bound must be greater than origin"); |
798 |
|
|
return StreamSupport.longStream |
799 |
|
|
(new RandomLongsSpliterator |
800 |
|
|
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
801 |
|
|
false); |
802 |
|
|
} |
803 |
|
|
|
804 |
|
|
/** |
805 |
dl |
1.7 |
* Returns a stream producing the given {@code streamSize} number of |
806 |
|
|
* pseudorandom {@code double} values, each between zero |
807 |
|
|
* (inclusive) and one (exclusive). |
808 |
dl |
1.1 |
* |
809 |
|
|
* @param streamSize the number of values to generate |
810 |
|
|
* @return a stream of {@code double} values |
811 |
|
|
* @throws IllegalArgumentException if {@code streamSize} is |
812 |
dl |
1.7 |
* less than zero |
813 |
dl |
1.1 |
*/ |
814 |
|
|
public DoubleStream doubles(long streamSize) { |
815 |
|
|
if (streamSize < 0L) |
816 |
|
|
throw new IllegalArgumentException("negative Stream size"); |
817 |
|
|
return StreamSupport.doubleStream |
818 |
|
|
(new RandomDoublesSpliterator |
819 |
|
|
(this, 0L, streamSize, Double.MAX_VALUE, 0.0), |
820 |
|
|
false); |
821 |
|
|
} |
822 |
|
|
|
823 |
|
|
/** |
824 |
|
|
* Returns an effectively unlimited stream of pseudorandom {@code |
825 |
dl |
1.7 |
* double} values, each between zero (inclusive) and one |
826 |
|
|
* (exclusive). |
827 |
dl |
1.1 |
* |
828 |
|
|
* @implNote This method is implemented to be equivalent to {@code |
829 |
|
|
* doubles(Long.MAX_VALUE)}. |
830 |
|
|
* |
831 |
|
|
* @return a stream of pseudorandom {@code double} values |
832 |
|
|
*/ |
833 |
|
|
public DoubleStream doubles() { |
834 |
|
|
return StreamSupport.doubleStream |
835 |
|
|
(new RandomDoublesSpliterator |
836 |
|
|
(this, 0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0), |
837 |
|
|
false); |
838 |
|
|
} |
839 |
|
|
|
840 |
|
|
/** |
841 |
dl |
1.7 |
* Returns a stream producing the given {@code streamSize} number of |
842 |
dl |
1.1 |
* pseudorandom {@code double} values, each conforming to the |
843 |
|
|
* given origin and bound. |
844 |
|
|
* |
845 |
|
|
* @param streamSize the number of values to generate |
846 |
|
|
* @param randomNumberOrigin the origin of each random value |
847 |
|
|
* @param randomNumberBound the bound of each random value |
848 |
|
|
* @return a stream of pseudorandom {@code double} values, |
849 |
jsr166 |
1.10 |
* each with the given origin and bound |
850 |
dl |
1.1 |
* @throws IllegalArgumentException if {@code streamSize} is |
851 |
jsr166 |
1.10 |
* less than zero |
852 |
dl |
1.1 |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
853 |
|
|
* is greater than or equal to {@code randomNumberBound} |
854 |
|
|
*/ |
855 |
|
|
public DoubleStream doubles(long streamSize, double randomNumberOrigin, |
856 |
|
|
double randomNumberBound) { |
857 |
|
|
if (streamSize < 0L) |
858 |
|
|
throw new IllegalArgumentException("negative Stream size"); |
859 |
dl |
1.7 |
if (!(randomNumberOrigin < randomNumberBound)) |
860 |
dl |
1.1 |
throw new IllegalArgumentException("bound must be greater than origin"); |
861 |
|
|
return StreamSupport.doubleStream |
862 |
|
|
(new RandomDoublesSpliterator |
863 |
|
|
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound), |
864 |
|
|
false); |
865 |
|
|
} |
866 |
|
|
|
867 |
|
|
/** |
868 |
|
|
* Returns an effectively unlimited stream of pseudorandom {@code |
869 |
|
|
* double} values, each conforming to the given origin and bound. |
870 |
|
|
* |
871 |
|
|
* @implNote This method is implemented to be equivalent to {@code |
872 |
|
|
* doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
873 |
|
|
* |
874 |
|
|
* @param randomNumberOrigin the origin of each random value |
875 |
|
|
* @param randomNumberBound the bound of each random value |
876 |
|
|
* @return a stream of pseudorandom {@code double} values, |
877 |
jsr166 |
1.10 |
* each with the given origin and bound |
878 |
dl |
1.1 |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
879 |
|
|
* is greater than or equal to {@code randomNumberBound} |
880 |
|
|
*/ |
881 |
|
|
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { |
882 |
dl |
1.7 |
if (!(randomNumberOrigin < randomNumberBound)) |
883 |
dl |
1.1 |
throw new IllegalArgumentException("bound must be greater than origin"); |
884 |
|
|
return StreamSupport.doubleStream |
885 |
|
|
(new RandomDoublesSpliterator |
886 |
|
|
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
887 |
|
|
false); |
888 |
|
|
} |
889 |
|
|
|
890 |
|
|
/** |
891 |
|
|
* Spliterator for int streams. We multiplex the four int |
892 |
dl |
1.7 |
* versions into one class by treating a bound less than origin as |
893 |
dl |
1.1 |
* unbounded, and also by treating "infinite" as equivalent to |
894 |
|
|
* Long.MAX_VALUE. For splits, it uses the standard divide-by-two |
895 |
|
|
* approach. The long and double versions of this class are |
896 |
|
|
* identical except for types. |
897 |
|
|
*/ |
898 |
dl |
1.11 |
static final class RandomIntsSpliterator implements Spliterator.OfInt { |
899 |
dl |
1.1 |
final SplittableRandom rng; |
900 |
|
|
long index; |
901 |
|
|
final long fence; |
902 |
|
|
final int origin; |
903 |
|
|
final int bound; |
904 |
|
|
RandomIntsSpliterator(SplittableRandom rng, long index, long fence, |
905 |
|
|
int origin, int bound) { |
906 |
|
|
this.rng = rng; this.index = index; this.fence = fence; |
907 |
|
|
this.origin = origin; this.bound = bound; |
908 |
|
|
} |
909 |
|
|
|
910 |
|
|
public RandomIntsSpliterator trySplit() { |
911 |
|
|
long i = index, m = (i + fence) >>> 1; |
912 |
|
|
return (m <= i) ? null : |
913 |
|
|
new RandomIntsSpliterator(rng.split(), i, index = m, origin, bound); |
914 |
|
|
} |
915 |
|
|
|
916 |
|
|
public long estimateSize() { |
917 |
|
|
return fence - index; |
918 |
|
|
} |
919 |
|
|
|
920 |
|
|
public int characteristics() { |
921 |
|
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
922 |
dl |
1.4 |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
923 |
dl |
1.1 |
} |
924 |
|
|
|
925 |
|
|
public boolean tryAdvance(IntConsumer consumer) { |
926 |
|
|
if (consumer == null) throw new NullPointerException(); |
927 |
|
|
long i = index, f = fence; |
928 |
|
|
if (i < f) { |
929 |
|
|
consumer.accept(rng.internalNextInt(origin, bound)); |
930 |
|
|
index = i + 1; |
931 |
|
|
return true; |
932 |
|
|
} |
933 |
|
|
return false; |
934 |
|
|
} |
935 |
|
|
|
936 |
|
|
public void forEachRemaining(IntConsumer consumer) { |
937 |
|
|
if (consumer == null) throw new NullPointerException(); |
938 |
|
|
long i = index, f = fence; |
939 |
|
|
if (i < f) { |
940 |
|
|
index = f; |
941 |
|
|
int o = origin, b = bound; |
942 |
|
|
do { |
943 |
|
|
consumer.accept(rng.internalNextInt(o, b)); |
944 |
|
|
} while (++i < f); |
945 |
|
|
} |
946 |
|
|
} |
947 |
|
|
} |
948 |
|
|
|
949 |
|
|
/** |
950 |
|
|
* Spliterator for long streams. |
951 |
|
|
*/ |
952 |
dl |
1.11 |
static final class RandomLongsSpliterator implements Spliterator.OfLong { |
953 |
dl |
1.1 |
final SplittableRandom rng; |
954 |
|
|
long index; |
955 |
|
|
final long fence; |
956 |
|
|
final long origin; |
957 |
|
|
final long bound; |
958 |
|
|
RandomLongsSpliterator(SplittableRandom rng, long index, long fence, |
959 |
|
|
long origin, long bound) { |
960 |
|
|
this.rng = rng; this.index = index; this.fence = fence; |
961 |
|
|
this.origin = origin; this.bound = bound; |
962 |
|
|
} |
963 |
|
|
|
964 |
|
|
public RandomLongsSpliterator trySplit() { |
965 |
|
|
long i = index, m = (i + fence) >>> 1; |
966 |
|
|
return (m <= i) ? null : |
967 |
|
|
new RandomLongsSpliterator(rng.split(), i, index = m, origin, bound); |
968 |
|
|
} |
969 |
|
|
|
970 |
|
|
public long estimateSize() { |
971 |
|
|
return fence - index; |
972 |
|
|
} |
973 |
|
|
|
974 |
|
|
public int characteristics() { |
975 |
|
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
976 |
dl |
1.4 |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
977 |
dl |
1.1 |
} |
978 |
|
|
|
979 |
|
|
public boolean tryAdvance(LongConsumer consumer) { |
980 |
|
|
if (consumer == null) throw new NullPointerException(); |
981 |
|
|
long i = index, f = fence; |
982 |
|
|
if (i < f) { |
983 |
|
|
consumer.accept(rng.internalNextLong(origin, bound)); |
984 |
|
|
index = i + 1; |
985 |
|
|
return true; |
986 |
|
|
} |
987 |
|
|
return false; |
988 |
|
|
} |
989 |
|
|
|
990 |
|
|
public void forEachRemaining(LongConsumer consumer) { |
991 |
|
|
if (consumer == null) throw new NullPointerException(); |
992 |
|
|
long i = index, f = fence; |
993 |
|
|
if (i < f) { |
994 |
|
|
index = f; |
995 |
|
|
long o = origin, b = bound; |
996 |
|
|
do { |
997 |
|
|
consumer.accept(rng.internalNextLong(o, b)); |
998 |
|
|
} while (++i < f); |
999 |
|
|
} |
1000 |
|
|
} |
1001 |
|
|
|
1002 |
|
|
} |
1003 |
|
|
|
1004 |
|
|
/** |
1005 |
|
|
* Spliterator for double streams. |
1006 |
|
|
*/ |
1007 |
dl |
1.11 |
static final class RandomDoublesSpliterator implements Spliterator.OfDouble { |
1008 |
dl |
1.1 |
final SplittableRandom rng; |
1009 |
|
|
long index; |
1010 |
|
|
final long fence; |
1011 |
|
|
final double origin; |
1012 |
|
|
final double bound; |
1013 |
|
|
RandomDoublesSpliterator(SplittableRandom rng, long index, long fence, |
1014 |
|
|
double origin, double bound) { |
1015 |
|
|
this.rng = rng; this.index = index; this.fence = fence; |
1016 |
|
|
this.origin = origin; this.bound = bound; |
1017 |
|
|
} |
1018 |
|
|
|
1019 |
|
|
public RandomDoublesSpliterator trySplit() { |
1020 |
|
|
long i = index, m = (i + fence) >>> 1; |
1021 |
|
|
return (m <= i) ? null : |
1022 |
|
|
new RandomDoublesSpliterator(rng.split(), i, index = m, origin, bound); |
1023 |
|
|
} |
1024 |
|
|
|
1025 |
|
|
public long estimateSize() { |
1026 |
|
|
return fence - index; |
1027 |
|
|
} |
1028 |
|
|
|
1029 |
|
|
public int characteristics() { |
1030 |
|
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
1031 |
dl |
1.4 |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
1032 |
dl |
1.1 |
} |
1033 |
|
|
|
1034 |
|
|
public boolean tryAdvance(DoubleConsumer consumer) { |
1035 |
|
|
if (consumer == null) throw new NullPointerException(); |
1036 |
|
|
long i = index, f = fence; |
1037 |
|
|
if (i < f) { |
1038 |
|
|
consumer.accept(rng.internalNextDouble(origin, bound)); |
1039 |
|
|
index = i + 1; |
1040 |
|
|
return true; |
1041 |
|
|
} |
1042 |
|
|
return false; |
1043 |
|
|
} |
1044 |
|
|
|
1045 |
|
|
public void forEachRemaining(DoubleConsumer consumer) { |
1046 |
|
|
if (consumer == null) throw new NullPointerException(); |
1047 |
|
|
long i = index, f = fence; |
1048 |
|
|
if (i < f) { |
1049 |
|
|
index = f; |
1050 |
|
|
double o = origin, b = bound; |
1051 |
|
|
do { |
1052 |
|
|
consumer.accept(rng.internalNextDouble(o, b)); |
1053 |
|
|
} while (++i < f); |
1054 |
|
|
} |
1055 |
|
|
} |
1056 |
|
|
} |
1057 |
|
|
|
1058 |
|
|
} |