25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
+ |
import java.net.InetAddress; |
29 |
|
import java.util.concurrent.atomic.AtomicLong; |
30 |
|
import java.util.Spliterator; |
31 |
|
import java.util.function.IntConsumer; |
36 |
|
import java.util.stream.LongStream; |
37 |
|
import java.util.stream.DoubleStream; |
38 |
|
|
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 |
< |
* producing pseudorandom nunmbers of type {@code int}, {@code long}, |
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: <ul> |
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. |
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. </li> |
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 set of |
62 |
< |
* values collectively generated by the two objects has the same |
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> |
78 |
|
* </ul> |
79 |
|
* |
80 |
|
* @author Guy Steele |
81 |
+ |
* @author Doug Lea |
82 |
|
* @since 1.8 |
83 |
|
*/ |
84 |
|
public class SplittableRandom { |
85 |
|
|
86 |
|
/* |
82 |
– |
* File organization: First the non-public methods that constitute |
83 |
– |
* the main algorithm, then the main public methods, followed by |
84 |
– |
* some custom spliterator classes needed for stream methods. |
85 |
– |
* |
86 |
– |
* Credits: Primary algorithm and code by Guy Steele. Stream |
87 |
– |
* support methods by Doug Lea. Documentation jointly produced |
88 |
– |
* with additional help from Brian Goetz. |
89 |
– |
*/ |
90 |
– |
|
91 |
– |
/* |
87 |
|
* Implementation Overview. |
88 |
|
* |
89 |
|
* This algorithm was inspired by the "DotMix" algorithm by |
90 |
|
* Leiserson, Schardl, and Sukha "Deterministic Parallel |
91 |
|
* Random-Number Generation for Dynamic-Multithreading Platforms", |
92 |
< |
* PPoPP 2012, but improves and extends it in several ways. |
93 |
< |
* |
94 |
< |
* The primary update step is simply to add a constant ("gamma") |
95 |
< |
* to the current seed, modulo a prime ("George"). However, the |
96 |
< |
* nextLong and nextInt methods do not return this value, but |
97 |
< |
* instead the results of bit-mixing transformations that produce |
98 |
< |
* more uniformly distributed sequences. |
99 |
< |
* |
100 |
< |
* "George" is the otherwise nameless (because it cannot be |
101 |
< |
* represented) prime number 2^64+13. Using a prime number larger |
102 |
< |
* than can fit in a long ensures that all possible long values |
103 |
< |
* can occur, plus 13 others that just get skipped over when they |
104 |
< |
* are encountered; see method addGammaModGeorge. For this to |
105 |
< |
* work, initial gamma values must be at least 13. |
106 |
< |
* |
112 |
< |
* The value of gamma differs for each instance across a series of |
113 |
< |
* splits, and is generated using a slightly stripped-down variant |
114 |
< |
* of the same algorithm, but operating across calls to split(), |
115 |
< |
* not calls to nextLong(): Each instance carries the state of |
116 |
< |
* this generator as nextSplit, and uses mix64(nextSplit) as its |
117 |
< |
* own gamma value. Computations of gammas themselves use a fixed |
118 |
< |
* constant as the second argument to the addGammaModGeorge |
119 |
< |
* function, GAMMA_GAMMA, a "genuinely random" number from a |
120 |
< |
* radioactive decay reading (obtained from |
121 |
< |
* http://www.fourmilab.ch/hotbits/) meeting the above range |
122 |
< |
* constraint. Using a fixed constant maintains the invariant that |
123 |
< |
* the value of gamma is the same for every instance that is at |
124 |
< |
* the same split-distance from their common root. (Note: there is |
125 |
< |
* nothing especially magic about obtaining this constant from a |
126 |
< |
* "truly random" physical source rather than just choosing one |
127 |
< |
* arbitrarily; using "hotbits" was merely an aesthetically pleasing |
128 |
< |
* choice. In either case, good statistical behavior of the |
129 |
< |
* algorithm should be, and was, verified by using the DieHarder |
130 |
< |
* test suite.) |
131 |
< |
* |
132 |
< |
* The mix64 bit-mixing function called by nextLong and other |
133 |
< |
* methods computes the same value as the "64-bit finalizer" |
134 |
< |
* function in Austin Appleby's MurmurHash3 algorithm. See |
92 |
> |
* PPoPP 2012, as well as those in "Parallel random numbers: as |
93 |
> |
* easy as 1, 2, 3" by Salmon, Morae, Dror, and Shaw, SC 2011. It |
94 |
> |
* differs mainly in simplifying and cheapening operations. |
95 |
> |
* |
96 |
> |
* The primary update step (method nextSeed()) is to add a |
97 |
> |
* constant ("gamma") to the current (64 bit) seed, forming a |
98 |
> |
* simple sequence. The seed and the gamma values for any two |
99 |
> |
* SplittableRandom instances are highly likely to be different. |
100 |
> |
* |
101 |
> |
* Methods nextLong, nextInt, and derivatives do not return the |
102 |
> |
* sequence (seed) values, but instead a hash-like bit-mix of |
103 |
> |
* their bits, producing more independently distributed sequences. |
104 |
> |
* For nextLong, the mix64 bit-mixing function computes the same |
105 |
> |
* value as the "64-bit finalizer" function in Austin Appleby's |
106 |
> |
* MurmurHash3 algorithm. See |
107 |
|
* http://code.google.com/p/smhasher/wiki/MurmurHash3 , which |
108 |
|
* comments: "The constants for the finalizers were generated by a |
109 |
|
* simple simulated-annealing algorithm, and both avalanche all |
110 |
< |
* bits of 'h' to within 0.25% bias." It also appears to work to |
111 |
< |
* use instead any of the variants proposed by David Stafford at |
112 |
< |
* http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html |
113 |
< |
* but these variants have not yet been tested as thoroughly |
114 |
< |
* in the context of the implementation of SplittableRandom. |
115 |
< |
* |
116 |
< |
* The mix32 function used for nextInt just consists of two of the |
117 |
< |
* five lines of mix64; avalanche testing shows that the 64-bit result |
118 |
< |
* has its top 32 bits avalanched well, though not the bottom 32 bits. |
119 |
< |
* DieHarder tests show that it is adequate for generating one |
120 |
< |
* random int from the 64-bit result of nextSeed. |
110 |
> |
* bits of 'h' to within 0.25% bias." The mix32 function is |
111 |
> |
* equivalent to (int)(mix64(seed) >>> 32), but faster because it |
112 |
> |
* omits a step that doesn't contribute to result. |
113 |
> |
* |
114 |
> |
* The split operation uses the current generator to form the seed |
115 |
> |
* and gamma for another SplittableRandom. To conservatively |
116 |
> |
* avoid potential correlations between seed and value generation, |
117 |
> |
* gamma selection (method nextGamma) uses the "Mix13" constants |
118 |
> |
* for MurmurHash3 described by David Stafford |
119 |
> |
* (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html) |
120 |
> |
* To avoid potential weaknesses in bit-mixing transformations, we |
121 |
> |
* restrict gammas to odd values with at least 12 and no more than |
122 |
> |
* 52 bits set. Rather than rejecting candidates with too few or |
123 |
> |
* too many bits set, method nextGamma flips some bits (which has |
124 |
> |
* the effect of mapping at most 4 to any given gamma value). |
125 |
> |
* This reduces the effective set of 64bit odd gamma values by |
126 |
> |
* about 2<sup>14</sup>, a very tiny percentage, and serves as an |
127 |
> |
* automated screening for sequence constant selection that is |
128 |
> |
* left as an empirical decision in some other hashing and crypto |
129 |
> |
* algorithms. |
130 |
> |
* |
131 |
> |
* The resulting generator thus transforms a sequence in which |
132 |
> |
* (typically) many bits change on each step, with an inexpensive |
133 |
> |
* mixer with good (but less than cryptographically secure) |
134 |
> |
* avalanching. |
135 |
> |
* |
136 |
> |
* The default (no-argument) constructor, in essence, invokes |
137 |
> |
* split() for a common "seeder" SplittableRandom. Unlike other |
138 |
> |
* cases, this split must be performed in a thread-safe manner, so |
139 |
> |
* we use an AtomicLong to represent the seed rather than use an |
140 |
> |
* explicit SplittableRandom. To bootstrap the seeder, we start |
141 |
> |
* off using a seed based on current time and host. This serves as |
142 |
> |
* a slimmed-down (and insecure) variant of SecureRandom that also |
143 |
> |
* avoids stalls that may occur when using /dev/random. |
144 |
> |
* |
145 |
> |
* It is a relatively simple matter to apply the basic design here |
146 |
> |
* to use 128 bit seeds. However, emulating 128bit arithmetic and |
147 |
> |
* carrying around twice the state add more overhead than appears |
148 |
> |
* warranted for current usages. |
149 |
|
* |
150 |
< |
* Support for the default (no-argument) constructor relies on an |
151 |
< |
* AtomicLong (defaultSeedGenerator) to help perform the |
152 |
< |
* equivalent of a split of a statically constructed |
153 |
< |
* SplittableRandom. Unlike other cases, this split must be |
154 |
< |
* performed in a thread-safe manner. We use |
155 |
< |
* AtomicLong.compareAndSet as the (typically) most efficient |
156 |
< |
* mechanism. To bootstrap, we start off using System.nanotime(), |
157 |
< |
* and update using another "genuinely random" constant |
158 |
< |
* DEFAULT_SEED_GAMMA. The default constructor uses GAMMA_GAMMA, |
159 |
< |
* not 0, for its splitSeed argument (addGammaModGeorge(0, |
160 |
< |
* GAMMA_GAMMA) == GAMMA_GAMMA) to reflect that each is split from |
161 |
< |
* this root generator, even though the root is not explicitly |
162 |
< |
* represented as a SplittableRandom. |
163 |
< |
*/ |
164 |
< |
|
165 |
< |
/** |
166 |
< |
* The "genuinely random" value for producing new gamma values. |
167 |
< |
* The value is arbitrary, subject to the requirement that it be |
168 |
< |
* greater or equal to 13. |
150 |
> |
* File organization: First the non-public methods that constitute |
151 |
> |
* the main algorithm, then the main public methods, followed by |
152 |
> |
* some custom spliterator classes needed for stream methods. |
153 |
|
*/ |
170 |
– |
private static final long GAMMA_GAMMA = 0xF2281E2DBA6606F3L; |
154 |
|
|
155 |
|
/** |
156 |
< |
* The "genuinely random" seed update value for default constructors. |
157 |
< |
* The value is arbitrary, subject to the requirement that it be |
158 |
< |
* greater or equal to 13. |
156 |
> |
* The initial gamma value for (unsplit) SplittableRandoms. Must |
157 |
> |
* be odd with at least 12 and no more than 52 bits set. Currently |
158 |
> |
* set to the golden ratio scaled to 64bits. |
159 |
|
*/ |
160 |
< |
private static final long DEFAULT_SEED_GAMMA = 0xBD24B73A95FB84D9L; |
160 |
> |
private static final long INITIAL_GAMMA = 0x9e3779b97f4a7c15L; |
161 |
|
|
162 |
|
/** |
163 |
< |
* The next seed for default constructors. |
163 |
> |
* The least non-zero value returned by nextDouble(). This value |
164 |
> |
* is scaled by a random value of 53 bits to produce a result. |
165 |
|
*/ |
166 |
< |
private static final AtomicLong defaultSeedGenerator = |
183 |
< |
new AtomicLong(System.nanoTime()); |
166 |
> |
private static final double DOUBLE_UNIT = 1.0 / (1L << 53); |
167 |
|
|
168 |
|
/** |
169 |
< |
* The seed, updated only via method nextSeed. |
169 |
> |
* The seed. Updated only via method nextSeed. |
170 |
|
*/ |
171 |
|
private long seed; |
172 |
|
|
173 |
|
/** |
174 |
< |
* The constant value added to seed (mod George) on each update. |
174 |
> |
* The step value. |
175 |
|
*/ |
176 |
|
private final long gamma; |
177 |
|
|
178 |
|
/** |
179 |
< |
* The next seed to use for splits. Propagated using |
197 |
< |
* addGammaModGeorge across instances. |
179 |
> |
* Internal constructor used by all others except default constructor. |
180 |
|
*/ |
181 |
< |
private final long nextSplit; |
181 |
> |
private SplittableRandom(long seed, long gamma) { |
182 |
> |
this.seed = seed; |
183 |
> |
this.gamma = gamma; |
184 |
> |
} |
185 |
|
|
186 |
|
/** |
187 |
< |
* Internal constructor used by all other constructors and by |
203 |
< |
* method split. Establishes the initial seed for this instance, |
204 |
< |
* and uses the given splitSeed to establish gamma, as well as the |
205 |
< |
* nextSplit to use by this instance. |
187 |
> |
* Computes MurmurHash3 64bit mix function. |
188 |
|
*/ |
189 |
< |
private SplittableRandom(long seed, long splitSeed) { |
190 |
< |
this.seed = seed; |
191 |
< |
long s = splitSeed, g; |
192 |
< |
do { // ensure gamma >= 13, considered as an unsigned integer |
211 |
< |
s = addGammaModGeorge(s, GAMMA_GAMMA); |
212 |
< |
g = mix64(s); |
213 |
< |
} while (Long.compareUnsigned(g, 13L) < 0); |
214 |
< |
this.gamma = g; |
215 |
< |
this.nextSplit = s; |
189 |
> |
private static long mix64(long z) { |
190 |
> |
z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; |
191 |
> |
z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L; |
192 |
> |
return z ^ (z >>> 33); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* Adds the given gamma value, g, to the given seed value s, mod |
197 |
< |
* George (2^64+13). We regard s and g as unsigned values |
198 |
< |
* (ranging from 0 to 2^64-1). We add g to s either once or twice |
199 |
< |
* (mod George) as necessary to produce an (unsigned) result less |
200 |
< |
* than 2^64. We require that g must be at least 13. This |
224 |
< |
* guarantees that if (s+g) mod George >= 2^64 then (s+g+g) mod |
225 |
< |
* George < 2^64; thus we need only a conditional, not a loop, |
226 |
< |
* to be sure of getting a representable value. |
227 |
< |
* |
228 |
< |
* @param s a seed value |
229 |
< |
* @param g a gamma value, 13 <= g (as unsigned) |
230 |
< |
*/ |
231 |
< |
private static long addGammaModGeorge(long s, long g) { |
232 |
< |
long p = s + g; |
233 |
< |
if (Long.compareUnsigned(p, g) >= 0) |
234 |
< |
return p; |
235 |
< |
long q = p - 13L; |
236 |
< |
return (Long.compareUnsigned(p, 13L) >= 0) ? q : (q + g); |
196 |
> |
* Returns the 32 high bits of mix64(z) as int. |
197 |
> |
*/ |
198 |
> |
private static int mix32(long z) { |
199 |
> |
z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; |
200 |
> |
return (int)(((z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L) >>> 32); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
< |
* Updates in-place and returns seed. |
241 |
< |
* See above for explanation. |
204 |
> |
* Returns the gamma value to use for a new split instance. |
205 |
|
*/ |
206 |
< |
private long nextSeed() { |
207 |
< |
return seed = addGammaModGeorge(seed, gamma); |
206 |
> |
private static long nextGamma(long z) { |
207 |
> |
z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9L; // Stafford "Mix13" |
208 |
> |
z = (z ^ (z >>> 27)) * 0x94d049bb133111ebL; |
209 |
> |
z = (z ^ (z >>> 31)) | 1L; // force to be odd |
210 |
> |
int n = Long.bitCount(z); // ensure enough 0 and 1 bits |
211 |
> |
return (n < 12 || n > 52) ? z ^ 0xaaaaaaaaaaaaaaaaL : z; |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* Returns a bit-mixed transformation of its argument. |
249 |
< |
* See above for explanation. |
215 |
> |
* Adds gamma to seed. |
216 |
|
*/ |
217 |
< |
private static long mix64(long z) { |
218 |
< |
z ^= (z >>> 33); |
253 |
< |
z *= 0xff51afd7ed558ccdL; |
254 |
< |
z ^= (z >>> 33); |
255 |
< |
z *= 0xc4ceb9fe1a85ec53L; |
256 |
< |
z ^= (z >>> 33); |
257 |
< |
return z; |
217 |
> |
private long nextSeed() { |
218 |
> |
return seed += gamma; |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* Returns a bit-mixed int transformation of its argument. |
262 |
< |
* See above for explanation. |
222 |
> |
* The seed generator for default constructors. |
223 |
|
*/ |
224 |
< |
private static int mix32(long z) { |
225 |
< |
z ^= (z >>> 33); |
226 |
< |
z *= 0xc4ceb9fe1a85ec53L; |
227 |
< |
return (int)(z >>> 32); |
268 |
< |
} |
224 |
> |
private static final AtomicLong seeder = |
225 |
> |
new AtomicLong(mix64((((long)hashedHostAddress()) << 32) ^ |
226 |
> |
System.currentTimeMillis()) ^ |
227 |
> |
mix64(System.nanoTime())); |
228 |
|
|
229 |
|
/** |
230 |
< |
* Atomically updates and returns next seed for default constructor |
230 |
> |
* Returns hash of local host IP address, if available; else 0. |
231 |
|
*/ |
232 |
< |
private static long nextDefaultSeed() { |
233 |
< |
long oldSeed, newSeed; |
234 |
< |
do { |
235 |
< |
oldSeed = defaultSeedGenerator.get(); |
236 |
< |
newSeed = addGammaModGeorge(oldSeed, DEFAULT_SEED_GAMMA); |
237 |
< |
} while (!defaultSeedGenerator.compareAndSet(oldSeed, newSeed)); |
279 |
< |
return mix64(newSeed); |
232 |
> |
private static int hashedHostAddress() { |
233 |
> |
try { |
234 |
> |
return InetAddress.getLocalHost().hashCode(); |
235 |
> |
} catch (Exception ex) { |
236 |
> |
return 0; |
237 |
> |
} |
238 |
|
} |
239 |
|
|
240 |
+ |
// IllegalArgumentException messages |
241 |
+ |
static final String BadBound = "bound must be positive"; |
242 |
+ |
static final String BadRange = "bound must be greater than origin"; |
243 |
+ |
static final String BadSize = "size must be non-negative"; |
244 |
+ |
|
245 |
|
/* |
246 |
|
* Internal versions of nextX methods used by streams, as well as |
247 |
|
* the public nextX(origin, bound) methods. These exist mainly to |
273 |
|
* evenly divisible by the range. The loop rejects candidates |
274 |
|
* computed from otherwise over-represented values. The |
275 |
|
* expected number of iterations under an ideal generator |
276 |
< |
* varies from 1 to 2, depending on the bound. |
276 |
> |
* varies from 1 to 2, depending on the bound. The loop itself |
277 |
> |
* takes an unlovable form. Because the first candidate is |
278 |
> |
* already available, we need a break-in-the-middle |
279 |
> |
* construction, which is concisely but cryptically performed |
280 |
> |
* within the while-condition of a body-less for loop. |
281 |
|
* |
282 |
|
* 4. Otherwise, the range cannot be represented as a positive |
283 |
< |
* long. Repeatedly generate unbounded longs until obtaining |
284 |
< |
* a candidate meeting constraints (with an expected number of |
285 |
< |
* iterations of less than two). |
283 |
> |
* long. The loop repeatedly generates unbounded longs until |
284 |
> |
* obtaining a candidate meeting constraints (with an expected |
285 |
> |
* number of iterations of less than two). |
286 |
|
*/ |
287 |
|
|
288 |
|
long r = mix64(nextSeed()); |
289 |
|
if (origin < bound) { |
290 |
|
long n = bound - origin, m = n - 1; |
291 |
< |
if ((n & m) == 0L) // power of two |
291 |
> |
if ((n & m) == 0L) // power of two |
292 |
|
r = (r & m) + origin; |
293 |
< |
else if (n > 0) { // reject over-represented candidates |
293 |
> |
else if (n > 0L) { // reject over-represented candidates |
294 |
|
for (long u = r >>> 1; // ensure nonnegative |
295 |
< |
u + m - (r = u % n) < 0L; // reject |
295 |
> |
u + m - (r = u % n) < 0L; // rejection check |
296 |
|
u = mix64(nextSeed()) >>> 1) // retry |
297 |
|
; |
298 |
|
r += origin; |
299 |
|
} |
300 |
< |
else { // range not representable as long |
300 |
> |
else { // range not representable as long |
301 |
|
while (r < origin || r >= bound) |
302 |
|
r = mix64(nextSeed()); |
303 |
|
} |
317 |
|
int r = mix32(nextSeed()); |
318 |
|
if (origin < bound) { |
319 |
|
int n = bound - origin, m = n - 1; |
320 |
< |
if ((n & m) == 0L) |
320 |
> |
if ((n & m) == 0) |
321 |
|
r = (r & m) + origin; |
322 |
|
else if (n > 0) { |
323 |
|
for (int u = r >>> 1; |
324 |
< |
u + m - (r = u % n) < 0L; |
324 |
> |
u + m - (r = u % n) < 0; |
325 |
|
u = mix32(nextSeed()) >>> 1) |
326 |
|
; |
327 |
|
r += origin; |
342 |
|
* @return a pseudorandom value |
343 |
|
*/ |
344 |
|
final double internalNextDouble(double origin, double bound) { |
345 |
< |
long bits = (1023L << 52) | (nextLong() >>> 12); |
379 |
< |
double r = Double.longBitsToDouble(bits) - 1.0; |
345 |
> |
double r = (nextLong() >>> 11) * DOUBLE_UNIT; |
346 |
|
if (origin < bound) { |
347 |
|
r = r * (bound - origin) + origin; |
348 |
< |
if (r == bound) // correct for rounding |
348 |
> |
if (r >= bound) // correct for rounding |
349 |
|
r = Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1); |
350 |
|
} |
351 |
|
return r; |
354 |
|
/* ---------------- public methods ---------------- */ |
355 |
|
|
356 |
|
/** |
357 |
< |
* Creates a new SplittableRandom instance using the given initial |
358 |
< |
* seed. Two SplittableRandom instances created with the same seed |
359 |
< |
* generate identical sequences of values. |
357 |
> |
* Creates a new SplittableRandom instance using the specified |
358 |
> |
* initial seed. SplittableRandom instances created with the same |
359 |
> |
* seed in the same program generate identical sequences of values. |
360 |
|
* |
361 |
|
* @param seed the initial seed |
362 |
|
*/ |
363 |
|
public SplittableRandom(long seed) { |
364 |
< |
this(seed, 0); |
364 |
> |
this(seed, INITIAL_GAMMA); |
365 |
|
} |
366 |
|
|
367 |
|
/** |
370 |
|
* of those of any other instances in the current program; and |
371 |
|
* may, and typically does, vary across program invocations. |
372 |
|
*/ |
373 |
< |
public SplittableRandom() { |
374 |
< |
this(nextDefaultSeed(), GAMMA_GAMMA); |
373 |
> |
public SplittableRandom() { // emulate seeder.split() |
374 |
> |
this.gamma = nextGamma(this.seed = seeder.addAndGet(INITIAL_GAMMA)); |
375 |
|
} |
376 |
|
|
377 |
|
/** |
389 |
|
* @return the new SplittableRandom instance |
390 |
|
*/ |
391 |
|
public SplittableRandom split() { |
392 |
< |
return new SplittableRandom(nextSeed(), nextSplit); |
392 |
> |
long s = nextSeed(); |
393 |
> |
return new SplittableRandom(s, nextGamma(s)); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
|
* Returns a pseudorandom {@code int} value. |
398 |
|
* |
399 |
< |
* @return a pseudorandom value |
399 |
> |
* @return a pseudorandom {@code int} value |
400 |
|
*/ |
401 |
|
public int nextInt() { |
402 |
|
return mix32(nextSeed()); |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
< |
* Returns a pseudorandom {@code int} value between 0 (inclusive) |
406 |
> |
* Returns a pseudorandom {@code int} value between zero (inclusive) |
407 |
|
* and the specified bound (exclusive). |
408 |
|
* |
409 |
|
* @param bound the bound on the random number to be returned. Must be |
410 |
|
* positive. |
411 |
< |
* @return a pseudorandom {@code int} value between {@code 0} |
412 |
< |
* (inclusive) and the bound (exclusive). |
413 |
< |
* @exception IllegalArgumentException if the bound is not positive |
411 |
> |
* @return a pseudorandom {@code int} value between zero |
412 |
> |
* (inclusive) and the bound (exclusive) |
413 |
> |
* @throws IllegalArgumentException if {@code bound} is not positive |
414 |
|
*/ |
415 |
|
public int nextInt(int bound) { |
416 |
|
if (bound <= 0) |
417 |
< |
throw new IllegalArgumentException("bound must be positive"); |
417 |
> |
throw new IllegalArgumentException(BadBound); |
418 |
|
// Specialize internalNextInt for origin 0 |
419 |
|
int r = mix32(nextSeed()); |
420 |
|
int m = bound - 1; |
421 |
< |
if ((bound & m) == 0L) // power of two |
421 |
> |
if ((bound & m) == 0) // power of two |
422 |
|
r &= m; |
423 |
|
else { // reject over-represented candidates |
424 |
|
for (int u = r >>> 1; |
425 |
< |
u + m - (r = u % bound) < 0L; |
425 |
> |
u + m - (r = u % bound) < 0; |
426 |
|
u = mix32(nextSeed()) >>> 1) |
427 |
|
; |
428 |
|
} |
436 |
|
* @param origin the least value returned |
437 |
|
* @param bound the upper bound (exclusive) |
438 |
|
* @return a pseudorandom {@code int} value between the origin |
439 |
< |
* (inclusive) and the bound (exclusive). |
440 |
< |
* @exception IllegalArgumentException if {@code origin} is greater than |
439 |
> |
* (inclusive) and the bound (exclusive) |
440 |
> |
* @throws IllegalArgumentException if {@code origin} is greater than |
441 |
|
* or equal to {@code bound} |
442 |
|
*/ |
443 |
|
public int nextInt(int origin, int bound) { |
444 |
|
if (origin >= bound) |
445 |
< |
throw new IllegalArgumentException("bound must be greater than origin"); |
445 |
> |
throw new IllegalArgumentException(BadRange); |
446 |
|
return internalNextInt(origin, bound); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
|
* Returns a pseudorandom {@code long} value. |
451 |
|
* |
452 |
< |
* @return a pseudorandom value |
452 |
> |
* @return a pseudorandom {@code long} value |
453 |
|
*/ |
454 |
|
public long nextLong() { |
455 |
|
return mix64(nextSeed()); |
456 |
|
} |
457 |
|
|
458 |
|
/** |
459 |
< |
* Returns a pseudorandom {@code long} value between 0 (inclusive) |
459 |
> |
* Returns a pseudorandom {@code long} value between zero (inclusive) |
460 |
|
* and the specified bound (exclusive). |
461 |
|
* |
462 |
|
* @param bound the bound on the random number to be returned. Must be |
463 |
|
* positive. |
464 |
< |
* @return a pseudorandom {@code long} value between {@code 0} |
465 |
< |
* (inclusive) and the bound (exclusive). |
466 |
< |
* @exception IllegalArgumentException if the bound is not positive |
464 |
> |
* @return a pseudorandom {@code long} value between zero |
465 |
> |
* (inclusive) and the bound (exclusive) |
466 |
> |
* @throws IllegalArgumentException if {@code bound} is not positive |
467 |
|
*/ |
468 |
|
public long nextLong(long bound) { |
469 |
|
if (bound <= 0) |
470 |
< |
throw new IllegalArgumentException("bound must be positive"); |
470 |
> |
throw new IllegalArgumentException(BadBound); |
471 |
|
// Specialize internalNextLong for origin 0 |
472 |
|
long r = mix64(nextSeed()); |
473 |
|
long m = bound - 1; |
489 |
|
* @param origin the least value returned |
490 |
|
* @param bound the upper bound (exclusive) |
491 |
|
* @return a pseudorandom {@code long} value between the origin |
492 |
< |
* (inclusive) and the bound (exclusive). |
493 |
< |
* @exception IllegalArgumentException if {@code origin} is greater than |
492 |
> |
* (inclusive) and the bound (exclusive) |
493 |
> |
* @throws IllegalArgumentException if {@code origin} is greater than |
494 |
|
* or equal to {@code bound} |
495 |
|
*/ |
496 |
|
public long nextLong(long origin, long bound) { |
497 |
|
if (origin >= bound) |
498 |
< |
throw new IllegalArgumentException("bound must be greater than origin"); |
498 |
> |
throw new IllegalArgumentException(BadRange); |
499 |
|
return internalNextLong(origin, bound); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
< |
* Returns a pseudorandom {@code double} value between {@code 0.0} |
504 |
< |
* (inclusive) and {@code 1.0} (exclusive). |
503 |
> |
* Returns a pseudorandom {@code double} value between zero |
504 |
> |
* (inclusive) and one (exclusive). |
505 |
|
* |
506 |
< |
* @return a pseudorandom value between {@code 0.0} |
507 |
< |
* (inclusive) and {@code 1.0} (exclusive) |
506 |
> |
* @return a pseudorandom {@code double} value between zero |
507 |
> |
* (inclusive) and one (exclusive) |
508 |
|
*/ |
509 |
|
public double nextDouble() { |
510 |
< |
long bits = (1023L << 52) | (nextLong() >>> 12); |
544 |
< |
return Double.longBitsToDouble(bits) - 1.0; |
510 |
> |
return (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT; |
511 |
|
} |
512 |
|
|
513 |
|
/** |
516 |
|
* |
517 |
|
* @param bound the bound on the random number to be returned. Must be |
518 |
|
* positive. |
519 |
< |
* @return a pseudorandom {@code double} value between {@code 0.0} |
520 |
< |
* (inclusive) and the bound (exclusive). |
519 |
> |
* @return a pseudorandom {@code double} value between zero |
520 |
> |
* (inclusive) and the bound (exclusive) |
521 |
|
* @throws IllegalArgumentException if {@code bound} is not positive |
522 |
|
*/ |
523 |
|
public double nextDouble(double bound) { |
524 |
< |
if (bound <= 0.0) |
525 |
< |
throw new IllegalArgumentException("bound must be positive"); |
526 |
< |
double result = nextDouble() * bound; |
524 |
> |
if (!(bound > 0.0)) |
525 |
> |
throw new IllegalArgumentException(BadBound); |
526 |
> |
double result = (mix64(nextSeed()) >>> 11) * DOUBLE_UNIT * bound; |
527 |
|
return (result < bound) ? result : // correct for rounding |
528 |
|
Double.longBitsToDouble(Double.doubleToLongBits(bound) - 1); |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
< |
* Returns a pseudorandom {@code double} value between the given |
532 |
> |
* Returns a pseudorandom {@code double} value between the specified |
533 |
|
* origin (inclusive) and bound (exclusive). |
534 |
|
* |
535 |
|
* @param origin the least value returned |
536 |
|
* @param bound the upper bound |
537 |
|
* @return a pseudorandom {@code double} value between the origin |
538 |
< |
* (inclusive) and the bound (exclusive). |
538 |
> |
* (inclusive) and the bound (exclusive) |
539 |
|
* @throws IllegalArgumentException if {@code origin} is greater than |
540 |
|
* or equal to {@code bound} |
541 |
|
*/ |
542 |
|
public double nextDouble(double origin, double bound) { |
543 |
< |
if (origin >= bound) |
544 |
< |
throw new IllegalArgumentException("bound must be greater than origin"); |
543 |
> |
if (!(origin < bound)) |
544 |
> |
throw new IllegalArgumentException(BadRange); |
545 |
|
return internalNextDouble(origin, bound); |
546 |
|
} |
547 |
|
|
548 |
+ |
/** |
549 |
+ |
* Returns a pseudorandom {@code boolean} value. |
550 |
+ |
* |
551 |
+ |
* @return a pseudorandom {@code boolean} value |
552 |
+ |
*/ |
553 |
+ |
public boolean nextBoolean() { |
554 |
+ |
return mix32(nextSeed()) < 0; |
555 |
+ |
} |
556 |
+ |
|
557 |
|
// stream methods, coded in a way intended to better isolate for |
558 |
|
// maintenance purposes the small differences across forms. |
559 |
|
|
560 |
|
/** |
561 |
< |
* Returns a stream with the given {@code streamSize} number of |
562 |
< |
* pseudorandom {@code int} values. |
561 |
> |
* Returns a stream producing the given {@code streamSize} number |
562 |
> |
* of pseudorandom {@code int} values from this generator and/or |
563 |
> |
* one split from it. |
564 |
|
* |
565 |
|
* @param streamSize the number of values to generate |
566 |
|
* @return a stream of pseudorandom {@code int} values |
567 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
568 |
< |
* less than zero |
568 |
> |
* less than zero |
569 |
|
*/ |
570 |
|
public IntStream ints(long streamSize) { |
571 |
|
if (streamSize < 0L) |
572 |
< |
throw new IllegalArgumentException("negative Stream size"); |
572 |
> |
throw new IllegalArgumentException(BadSize); |
573 |
|
return StreamSupport.intStream |
574 |
|
(new RandomIntsSpliterator |
575 |
|
(this, 0L, streamSize, Integer.MAX_VALUE, 0), |
578 |
|
|
579 |
|
/** |
580 |
|
* Returns an effectively unlimited stream of pseudorandom {@code int} |
581 |
< |
* values |
581 |
> |
* values from this generator and/or one split from it. |
582 |
|
* |
583 |
|
* @implNote This method is implemented to be equivalent to {@code |
584 |
|
* ints(Long.MAX_VALUE)}. |
593 |
|
} |
594 |
|
|
595 |
|
/** |
596 |
< |
* Returns a stream with the given {@code streamSize} number of |
597 |
< |
* pseudorandom {@code int} values, each conforming to the given |
598 |
< |
* origin and bound. |
596 |
> |
* Returns a stream producing the given {@code streamSize} number |
597 |
> |
* of pseudorandom {@code int} values, each conforming to the |
598 |
> |
* given origin and bound. |
599 |
|
* |
600 |
|
* @param streamSize the number of values to generate |
601 |
|
* @param randomNumberOrigin the origin of each random value |
602 |
|
* @param randomNumberBound the bound of each random value |
603 |
|
* @return a stream of pseudorandom {@code int} values, |
604 |
< |
* each with the given origin and bound. |
604 |
> |
* each with the given origin and bound |
605 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
606 |
< |
* less than zero. |
631 |
< |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
606 |
> |
* less than zero, or {@code randomNumberOrigin} |
607 |
|
* is greater than or equal to {@code randomNumberBound} |
608 |
|
*/ |
609 |
|
public IntStream ints(long streamSize, int randomNumberOrigin, |
610 |
|
int randomNumberBound) { |
611 |
|
if (streamSize < 0L) |
612 |
< |
throw new IllegalArgumentException("negative Stream size"); |
612 |
> |
throw new IllegalArgumentException(BadSize); |
613 |
|
if (randomNumberOrigin >= randomNumberBound) |
614 |
< |
throw new IllegalArgumentException("bound must be greater than origin"); |
614 |
> |
throw new IllegalArgumentException(BadRange); |
615 |
|
return StreamSupport.intStream |
616 |
|
(new RandomIntsSpliterator |
617 |
|
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound), |
628 |
|
* @param randomNumberOrigin the origin of each random value |
629 |
|
* @param randomNumberBound the bound of each random value |
630 |
|
* @return a stream of pseudorandom {@code int} values, |
631 |
< |
* each with the given origin and bound. |
631 |
> |
* each with the given origin and bound |
632 |
|
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
633 |
|
* is greater than or equal to {@code randomNumberBound} |
634 |
|
*/ |
635 |
|
public IntStream ints(int randomNumberOrigin, int randomNumberBound) { |
636 |
|
if (randomNumberOrigin >= randomNumberBound) |
637 |
< |
throw new IllegalArgumentException("bound must be greater than origin"); |
637 |
> |
throw new IllegalArgumentException(BadRange); |
638 |
|
return StreamSupport.intStream |
639 |
|
(new RandomIntsSpliterator |
640 |
|
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
642 |
|
} |
643 |
|
|
644 |
|
/** |
645 |
< |
* Returns a stream with the given {@code streamSize} number of |
646 |
< |
* pseudorandom {@code long} values. |
645 |
> |
* Returns a stream producing the given {@code streamSize} number |
646 |
> |
* of pseudorandom {@code long} values from this generator and/or |
647 |
> |
* one split from it. |
648 |
|
* |
649 |
|
* @param streamSize the number of values to generate |
650 |
< |
* @return a stream of {@code long} values |
650 |
> |
* @return a stream of pseudorandom {@code long} values |
651 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
652 |
< |
* less than zero |
652 |
> |
* less than zero |
653 |
|
*/ |
654 |
|
public LongStream longs(long streamSize) { |
655 |
|
if (streamSize < 0L) |
656 |
< |
throw new IllegalArgumentException("negative Stream size"); |
656 |
> |
throw new IllegalArgumentException(BadSize); |
657 |
|
return StreamSupport.longStream |
658 |
|
(new RandomLongsSpliterator |
659 |
|
(this, 0L, streamSize, Long.MAX_VALUE, 0L), |
661 |
|
} |
662 |
|
|
663 |
|
/** |
664 |
< |
* Returns an effectively unlimited stream of pseudorandom {@code long} |
665 |
< |
* values. |
664 |
> |
* Returns an effectively unlimited stream of pseudorandom {@code |
665 |
> |
* long} values from this generator and/or one split from it. |
666 |
|
* |
667 |
|
* @implNote This method is implemented to be equivalent to {@code |
668 |
|
* longs(Long.MAX_VALUE)}. |
677 |
|
} |
678 |
|
|
679 |
|
/** |
680 |
< |
* Returns a stream with the given {@code streamSize} number of |
680 |
> |
* Returns a stream producing the given {@code streamSize} number of |
681 |
|
* pseudorandom {@code long} values, each conforming to the |
682 |
|
* given origin and bound. |
683 |
|
* |
685 |
|
* @param randomNumberOrigin the origin of each random value |
686 |
|
* @param randomNumberBound the bound of each random value |
687 |
|
* @return a stream of pseudorandom {@code long} values, |
688 |
< |
* each with the given origin and bound. |
688 |
> |
* each with the given origin and bound |
689 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
690 |
< |
* less than zero. |
715 |
< |
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
690 |
> |
* less than zero, or {@code randomNumberOrigin} |
691 |
|
* is greater than or equal to {@code randomNumberBound} |
692 |
|
*/ |
693 |
|
public LongStream longs(long streamSize, long randomNumberOrigin, |
694 |
|
long randomNumberBound) { |
695 |
|
if (streamSize < 0L) |
696 |
< |
throw new IllegalArgumentException("negative Stream size"); |
696 |
> |
throw new IllegalArgumentException(BadSize); |
697 |
|
if (randomNumberOrigin >= randomNumberBound) |
698 |
< |
throw new IllegalArgumentException("bound must be greater than origin"); |
698 |
> |
throw new IllegalArgumentException(BadRange); |
699 |
|
return StreamSupport.longStream |
700 |
|
(new RandomLongsSpliterator |
701 |
|
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound), |
712 |
|
* @param randomNumberOrigin the origin of each random value |
713 |
|
* @param randomNumberBound the bound of each random value |
714 |
|
* @return a stream of pseudorandom {@code long} values, |
715 |
< |
* each with the given origin and bound. |
715 |
> |
* each with the given origin and bound |
716 |
|
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
717 |
|
* is greater than or equal to {@code randomNumberBound} |
718 |
|
*/ |
719 |
|
public LongStream longs(long randomNumberOrigin, long randomNumberBound) { |
720 |
|
if (randomNumberOrigin >= randomNumberBound) |
721 |
< |
throw new IllegalArgumentException("bound must be greater than origin"); |
721 |
> |
throw new IllegalArgumentException(BadRange); |
722 |
|
return StreamSupport.longStream |
723 |
|
(new RandomLongsSpliterator |
724 |
|
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
726 |
|
} |
727 |
|
|
728 |
|
/** |
729 |
< |
* Returns a stream with the given {@code streamSize} number of |
730 |
< |
* pseudorandom {@code double} values. |
729 |
> |
* Returns a stream producing the given {@code streamSize} number of |
730 |
> |
* pseudorandom {@code double} values, each between zero |
731 |
> |
* (inclusive) and one (exclusive). |
732 |
|
* |
733 |
|
* @param streamSize the number of values to generate |
734 |
|
* @return a stream of {@code double} values |
735 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
736 |
< |
* less than zero |
736 |
> |
* less than zero |
737 |
|
*/ |
738 |
|
public DoubleStream doubles(long streamSize) { |
739 |
|
if (streamSize < 0L) |
740 |
< |
throw new IllegalArgumentException("negative Stream size"); |
740 |
> |
throw new IllegalArgumentException(BadSize); |
741 |
|
return StreamSupport.doubleStream |
742 |
|
(new RandomDoublesSpliterator |
743 |
|
(this, 0L, streamSize, Double.MAX_VALUE, 0.0), |
746 |
|
|
747 |
|
/** |
748 |
|
* Returns an effectively unlimited stream of pseudorandom {@code |
749 |
< |
* double} values. |
749 |
> |
* double} values, each between zero (inclusive) and one |
750 |
> |
* (exclusive). |
751 |
|
* |
752 |
|
* @implNote This method is implemented to be equivalent to {@code |
753 |
|
* doubles(Long.MAX_VALUE)}. |
762 |
|
} |
763 |
|
|
764 |
|
/** |
765 |
< |
* Returns a stream with the given {@code streamSize} number of |
765 |
> |
* Returns a stream producing the given {@code streamSize} number of |
766 |
|
* pseudorandom {@code double} values, each conforming to the |
767 |
|
* given origin and bound. |
768 |
|
* |
770 |
|
* @param randomNumberOrigin the origin of each random value |
771 |
|
* @param randomNumberBound the bound of each random value |
772 |
|
* @return a stream of pseudorandom {@code double} values, |
773 |
< |
* each with the given origin and bound. |
773 |
> |
* each with the given origin and bound |
774 |
|
* @throws IllegalArgumentException if {@code streamSize} is |
775 |
< |
* less than zero. |
775 |
> |
* less than zero |
776 |
|
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
777 |
|
* is greater than or equal to {@code randomNumberBound} |
778 |
|
*/ |
779 |
|
public DoubleStream doubles(long streamSize, double randomNumberOrigin, |
780 |
|
double randomNumberBound) { |
781 |
|
if (streamSize < 0L) |
782 |
< |
throw new IllegalArgumentException("negative Stream size"); |
783 |
< |
if (randomNumberOrigin >= randomNumberBound) |
784 |
< |
throw new IllegalArgumentException("bound must be greater than origin"); |
782 |
> |
throw new IllegalArgumentException(BadSize); |
783 |
> |
if (!(randomNumberOrigin < randomNumberBound)) |
784 |
> |
throw new IllegalArgumentException(BadRange); |
785 |
|
return StreamSupport.doubleStream |
786 |
|
(new RandomDoublesSpliterator |
787 |
|
(this, 0L, streamSize, randomNumberOrigin, randomNumberBound), |
798 |
|
* @param randomNumberOrigin the origin of each random value |
799 |
|
* @param randomNumberBound the bound of each random value |
800 |
|
* @return a stream of pseudorandom {@code double} values, |
801 |
< |
* each with the given origin and bound. |
801 |
> |
* each with the given origin and bound |
802 |
|
* @throws IllegalArgumentException if {@code randomNumberOrigin} |
803 |
|
* is greater than or equal to {@code randomNumberBound} |
804 |
|
*/ |
805 |
|
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { |
806 |
< |
if (randomNumberOrigin >= randomNumberBound) |
807 |
< |
throw new IllegalArgumentException("bound must be greater than origin"); |
806 |
> |
if (!(randomNumberOrigin < randomNumberBound)) |
807 |
> |
throw new IllegalArgumentException(BadRange); |
808 |
|
return StreamSupport.doubleStream |
809 |
|
(new RandomDoublesSpliterator |
810 |
|
(this, 0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
813 |
|
|
814 |
|
/** |
815 |
|
* Spliterator for int streams. We multiplex the four int |
816 |
< |
* versions into one class by treating and bound < origin as |
816 |
> |
* versions into one class by treating a bound less than origin as |
817 |
|
* unbounded, and also by treating "infinite" as equivalent to |
818 |
|
* Long.MAX_VALUE. For splits, it uses the standard divide-by-two |
819 |
|
* approach. The long and double versions of this class are |
820 |
|
* identical except for types. |
821 |
|
*/ |
822 |
< |
static class RandomIntsSpliterator implements Spliterator.OfInt { |
822 |
> |
static final class RandomIntsSpliterator implements Spliterator.OfInt { |
823 |
|
final SplittableRandom rng; |
824 |
|
long index; |
825 |
|
final long fence; |
843 |
|
|
844 |
|
public int characteristics() { |
845 |
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
846 |
< |
Spliterator.ORDERED | Spliterator.NONNULL | |
870 |
< |
Spliterator.IMMUTABLE); |
846 |
> |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
847 |
|
} |
848 |
|
|
849 |
|
public boolean tryAdvance(IntConsumer consumer) { |
862 |
|
long i = index, f = fence; |
863 |
|
if (i < f) { |
864 |
|
index = f; |
865 |
+ |
SplittableRandom r = rng; |
866 |
|
int o = origin, b = bound; |
867 |
|
do { |
868 |
< |
consumer.accept(rng.internalNextInt(o, b)); |
868 |
> |
consumer.accept(r.internalNextInt(o, b)); |
869 |
|
} while (++i < f); |
870 |
|
} |
871 |
|
} |
874 |
|
/** |
875 |
|
* Spliterator for long streams. |
876 |
|
*/ |
877 |
< |
static class RandomLongsSpliterator implements Spliterator.OfLong { |
877 |
> |
static final class RandomLongsSpliterator implements Spliterator.OfLong { |
878 |
|
final SplittableRandom rng; |
879 |
|
long index; |
880 |
|
final long fence; |
898 |
|
|
899 |
|
public int characteristics() { |
900 |
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
901 |
< |
Spliterator.ORDERED | Spliterator.NONNULL | |
925 |
< |
Spliterator.IMMUTABLE); |
901 |
> |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
902 |
|
} |
903 |
|
|
904 |
|
public boolean tryAdvance(LongConsumer consumer) { |
917 |
|
long i = index, f = fence; |
918 |
|
if (i < f) { |
919 |
|
index = f; |
920 |
+ |
SplittableRandom r = rng; |
921 |
|
long o = origin, b = bound; |
922 |
|
do { |
923 |
< |
consumer.accept(rng.internalNextLong(o, b)); |
923 |
> |
consumer.accept(r.internalNextLong(o, b)); |
924 |
|
} while (++i < f); |
925 |
|
} |
926 |
|
} |
930 |
|
/** |
931 |
|
* Spliterator for double streams. |
932 |
|
*/ |
933 |
< |
static class RandomDoublesSpliterator implements Spliterator.OfDouble { |
933 |
> |
static final class RandomDoublesSpliterator implements Spliterator.OfDouble { |
934 |
|
final SplittableRandom rng; |
935 |
|
long index; |
936 |
|
final long fence; |
954 |
|
|
955 |
|
public int characteristics() { |
956 |
|
return (Spliterator.SIZED | Spliterator.SUBSIZED | |
957 |
< |
Spliterator.ORDERED | Spliterator.NONNULL | |
981 |
< |
Spliterator.IMMUTABLE); |
957 |
> |
Spliterator.NONNULL | Spliterator.IMMUTABLE); |
958 |
|
} |
959 |
|
|
960 |
|
public boolean tryAdvance(DoubleConsumer consumer) { |
973 |
|
long i = index, f = fence; |
974 |
|
if (i < f) { |
975 |
|
index = f; |
976 |
+ |
SplittableRandom r = rng; |
977 |
|
double o = origin, b = bound; |
978 |
|
do { |
979 |
< |
consumer.accept(rng.internalNextDouble(o, b)); |
979 |
> |
consumer.accept(r.internalNextDouble(o, b)); |
980 |
|
} while (++i < f); |
981 |
|
} |
982 |
|
} |
983 |
|
} |
984 |
|
|
985 |
|
} |
1009 |
– |
|