20 * |
20 * |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
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 |
22 * or visit www.oracle.com if you need additional information or have any |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 package java.util; |
25 |
26 |
26 package java.util.random; |
|
27 |
|
28 import java.util.Spliterator; |
27 import java.util.function.Consumer; |
29 import java.util.function.Consumer; |
|
30 import java.util.function.DoubleConsumer; |
28 import java.util.function.IntConsumer; |
31 import java.util.function.IntConsumer; |
29 import java.util.function.LongConsumer; |
32 import java.util.function.LongConsumer; |
30 import java.util.function.DoubleConsumer; |
33 import java.util.stream.Stream; |
31 import java.util.Spliterator; |
|
32 import java.util.stream.StreamSupport; |
34 import java.util.stream.StreamSupport; |
33 import java.util.stream.Stream; |
|
34 |
35 |
35 /** |
36 /** |
36 * This class provides much of the implementation of the {@code SplittableRng} |
37 * This class provides much of the implementation of the {@link SplittableRNG} interface, to |
37 * interface, to minimize the effort required to implement this interface. |
38 * minimize the effort required to implement this interface. |
38 * |
39 * <p> |
39 * To implement a pseudorandom number generator, the programmer needs |
40 * To implement a pseudorandom number generator, the programmer needs only to extend this class and |
40 * only to extend this class and provide implementations for the |
41 * provide implementations for the methods {@code nextInt()}, {@code nextLong()}, {@code period()}, |
41 * methods {@code nextInt()}, {@code nextLong()}, {@code period()}, |
42 * and {@code split(SplittableRNG)}. |
42 * and {@code split(SplittableRng)}. |
43 * <p> |
43 * |
44 * (If the pseudorandom number generator also has the ability to jump, then the programmer may wish |
44 * (If the pseudorandom number generator also has the ability to jump, |
45 * to consider instead extending the class {@link ArbitrarilyJumpableRNG}. But if the pseudorandom |
45 * then the programmer may wish to consider instead extending |
46 * number generator furthermore has the ability to jump an arbitrary specified distance, then the |
46 * the class {@code AbstractSplittableJumpableRng} or (if it can also leap) |
47 * programmer may wish to consider instead extending the class {@link |
47 * {@code AbstractSplittableLeapableRng}. But if the pseudorandom number |
48 * AbstractArbitrarilyJumpableRNG}.) |
48 * generator furthermore has the ability to jump an arbitrary specified |
49 * <p> |
49 * distance, then the programmer may wish to consider instead extending |
50 * The programmer should generally provide at least three constructors: one that takes no arguments, |
50 * the class {@code * AbstractSplittableArbitrarilyJumpableRng}.) |
51 * one that accepts a {@code long} seed value, and one that accepts an array of seed {@code byte} |
51 * |
52 * values. This class provides a public {@code initialSeed()} method that may be useful in |
52 * The programmer should generally provide at least three constructors: |
53 * initializing some static state from which to derive defaults seeds for use by the no-argument |
53 * one that takes no arguments, one that accepts a {@code long} |
54 * constructor. |
54 * seed value, and one that accepts an array of seed {@code byte} values. |
55 * <p> |
55 * This class provides a public {@code initialSeed()} method that may |
56 * For the stream methods (such as {@code ints()} and {@code splits()}), this class provides {@link |
56 * be useful in initializing some static state from which to derive |
57 * Spliterator} based implementations that allow parallel execution when appropriate. |
57 * defaults seeds for use by the no-argument constructor. |
58 * <p> |
58 * |
59 * The documentation for each non-abstract method in this class describes its implementation in |
59 * For the stream methods (such as {@code ints()} and {@code splits()}), |
60 * detail. Each of these methods may be overridden if the pseudorandom number generator being |
60 * this class provides {@code Spliterator}-based implementations that |
|
61 * allow parallel execution when appropriate. |
|
62 * |
|
63 * The documentation for each non-abstract method in this class |
|
64 * describes its implementation in detail. Each of these methods may |
|
65 * be overridden if the pseudorandom number generator being |
|
66 * implemented admits a more efficient implementation. |
61 * implemented admits a more efficient implementation. |
67 * |
62 * |
68 * @author Guy Steele |
63 * @since 14 |
69 * @author Doug Lea |
|
70 * @since 1.9 |
|
71 */ |
64 */ |
72 public abstract class AbstractSplittableRng extends AbstractSpliteratorRng implements SplittableRng { |
65 public abstract class AbstractSplittableRNG extends AbstractSpliteratorRNG implements SplittableRNG { |
73 |
66 |
74 /* |
67 /* |
75 * Implementation Overview. |
68 * Implementation Overview. |
76 * |
69 * |
77 * This class provides most of the "user API" methods needed to |
70 * This class provides most of the "user API" methods needed to |
78 * satisfy the interface java.util.JumpableRng. Most of these methods |
71 * satisfy the interface JumpableRNG. Most of these methods |
79 * are in turn inherited from AbstractRng and the non-public class |
72 * are in turn inherited from AbstractRNG and the non-public class |
80 * AbstractSpliteratorRng; this file implements two versions of the |
73 * AbstractSpliteratorRNG; this file implements two versions of the |
81 * splits method and defines the spliterators necessary to support |
74 * splits method and defines the spliterators necessary to support |
82 * them. |
75 * them. |
83 * |
76 * |
84 * The abstract split() method from interface SplittableRng is redeclared |
77 * The abstract split() method from interface SplittableRNG is redeclared |
85 * here so as to narrow the return type to AbstractSplittableRng. |
78 * here so as to narrow the return type to AbstractSplittableRNG. |
86 * |
79 * |
87 * File organization: First the non-public methods needed by the class |
80 * File organization: First the non-public methods needed by the class |
88 * AbstractSpliteratorRng, then the main public methods, followed by some |
81 * AbstractSpliteratorRNG, then the main public methods, followed by some |
89 * custom spliterator classes. |
82 * custom spliterator classes. |
90 */ |
83 */ |
91 |
84 |
92 Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) { |
85 Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) { |
93 return new RandomIntsSpliterator(this, index, fence, origin, bound); |
86 return new RandomIntsSpliterator(this, index, fence, origin, bound); |
94 } |
87 } |
95 |
88 |
96 Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) { |
89 Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) { |
97 return new RandomLongsSpliterator(this, index, fence, origin, bound); |
90 return new RandomLongsSpliterator(this, index, fence, origin, bound); |
98 } |
91 } |
99 |
92 |
100 Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) { |
93 Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) { |
101 return new RandomDoublesSpliterator(this, index, fence, origin, bound); |
94 return new RandomDoublesSpliterator(this, index, fence, origin, bound); |
102 } |
95 } |
103 |
96 |
104 Spliterator<SplittableRng> makeSplitsSpliterator(long index, long fence, SplittableRng source) { |
97 Spliterator<SplittableRNG> makeSplitsSpliterator(long index, long fence, SplittableRNG source) { |
105 return new RandomSplitsSpliterator(source, index, fence, this); |
98 return new RandomSplitsSpliterator(source, index, fence, this); |
106 } |
99 } |
107 |
100 |
108 /* ---------------- public methods ---------------- */ |
101 /* ---------------- public methods ---------------- */ |
109 |
102 |
110 /** |
103 /** |
111 * Implements the @code{split()} method as {@code this.split(this) }. |
104 * Implements the @code{split()} method as {@code this.split(this) }. |
112 * |
105 * |
113 * @return the new {@code AbstractSplittableRng} instance |
106 * @return the new {@link AbstractSplittableRNG} instance |
114 */ |
107 */ |
115 public SplittableRng split() { return this.split(this); } |
108 public SplittableRNG split() { |
116 |
109 return this.split(this); |
|
110 } |
|
111 |
117 // Stream methods for splittings |
112 // Stream methods for splittings |
118 |
113 |
119 /** |
114 /** |
120 * Returns an effectively unlimited stream of new pseudorandom |
115 * Returns an effectively unlimited stream of new pseudorandom number generators, each of which |
121 * number generators, each of which implements the {@code SplittableRng} |
116 * implements the {@link SplittableRNG} interface. |
122 * interface. |
117 * <p> |
123 * |
118 * This pseudorandom number generator provides the entropy used to seed the new ones. |
124 * This pseudorandom number generator provides the |
119 * |
125 * entropy used to seed the new ones. |
120 * @return a stream of {@link SplittableRNG} objects |
126 * |
121 * |
127 * @implNote This method is implemented to be equivalent to |
122 * @implNote This method is implemented to be equivalent to {@code splits(Long.MAX_VALUE)}. |
128 * {@code splits(Long.MAX_VALUE)}. |
123 */ |
129 * |
124 public Stream<SplittableRNG> splits() { |
130 * @return a stream of {@code SplittableRng} objects |
|
131 */ |
|
132 public Stream<SplittableRng> splits() { |
|
133 return this.splits(Long.MAX_VALUE, this); |
125 return this.splits(Long.MAX_VALUE, this); |
134 } |
126 } |
135 |
127 |
136 /** |
128 /** |
137 * Returns a stream producing the given {@code streamSize} number of |
129 * Returns a stream producing the given {@code streamSize} number of new pseudorandom number |
138 * new pseudorandom number generators, each of which implements the |
130 * generators, each of which implements the {@link SplittableRNG} interface. |
139 * {@code SplittableRng} interface. |
131 * <p> |
140 * |
132 * This pseudorandom number generator provides the entropy used to seed the new ones. |
141 * This pseudorandom number generator provides the |
|
142 * entropy used to seed the new ones. |
|
143 * |
133 * |
144 * @param streamSize the number of values to generate |
134 * @param streamSize the number of values to generate |
145 * @return a stream of {@code SplittableRng} objects |
135 * |
146 * @throws IllegalArgumentException if {@code streamSize} is |
136 * @return a stream of {@link SplittableRNG} objects |
147 * less than zero |
137 * |
148 */ |
138 * @throws IllegalArgumentException if {@code streamSize} is less than zero |
149 public Stream<SplittableRng> splits(long streamSize) { |
139 */ |
150 return this.splits(streamSize, this); |
140 public Stream<SplittableRNG> splits(long streamSize) { |
151 } |
141 return this.splits(streamSize, this); |
152 |
142 } |
153 /** |
143 |
154 * Returns an effectively unlimited stream of new pseudorandom |
144 /** |
155 * number generators, each of which implements the {@code SplittableRng} |
145 * Returns an effectively unlimited stream of new pseudorandom number generators, each of which |
156 * interface. |
146 * implements the {@link SplittableRNG} interface. |
157 * |
147 * |
158 * @implNote This method is implemented to be equivalent to |
148 * @param source a {@link SplittableRNG} instance to be used instead of this one as a source of |
159 * {@code splits(Long.MAX_VALUE)}. |
149 * pseudorandom bits used to initialize the state of the new ones. |
160 * |
150 * |
161 * @param source a {@code SplittableRng} instance to be used instead |
151 * @return a stream of {@link SplittableRNG} objects |
162 * of this one as a source of pseudorandom bits used to |
152 * |
163 * initialize the state of the new ones. |
153 * @implNote This method is implemented to be equivalent to {@code splits(Long.MAX_VALUE)}. |
164 * @return a stream of {@code SplittableRng} objects |
154 */ |
165 */ |
155 public Stream<SplittableRNG> splits(SplittableRNG source) { |
166 public Stream<SplittableRng> splits(SplittableRng source) { |
|
167 return this.splits(Long.MAX_VALUE, source); |
156 return this.splits(Long.MAX_VALUE, source); |
168 } |
157 } |
169 |
158 |
170 /** |
159 /** |
171 * Returns a stream producing the given {@code streamSize} number of |
160 * Returns a stream producing the given {@code streamSize} number of new pseudorandom number |
172 * new pseudorandom number generators, each of which implements the |
161 * generators, each of which implements the {@link SplittableRNG} interface. |
173 * {@code SplittableRng} interface. |
|
174 * |
162 * |
175 * @param streamSize the number of values to generate |
163 * @param streamSize the number of values to generate |
176 * @param source a {@code SplittableRng} instance to be used instead |
164 * @param source a {@link SplittableRNG} instance to be used instead of this one as a source |
177 * of this one as a source of pseudorandom bits used to |
165 * of pseudorandom bits used to initialize the state of the new ones. |
178 * initialize the state of the new ones. |
166 * |
179 * @return a stream of {@code SplittableRng} objects |
167 * @return a stream of {@link SplittableRNG} objects |
180 * @throws IllegalArgumentException if {@code streamSize} is |
168 * |
181 * less than zero |
169 * @throws IllegalArgumentException if {@code streamSize} is less than zero |
182 */ |
170 */ |
183 public Stream<SplittableRng> splits(long streamSize, SplittableRng source) { |
171 public Stream<SplittableRNG> splits(long streamSize, SplittableRNG source) { |
184 RngSupport.checkStreamSize(streamSize); |
172 RNGSupport.checkStreamSize(streamSize); |
185 return StreamSupport.stream(makeSplitsSpliterator(0L, streamSize, source), false); |
173 return StreamSupport.stream(makeSplitsSpliterator(0L, streamSize, source), false); |
186 } |
174 } |
187 |
175 |
188 /** |
176 /** |
189 * Spliterator for int streams. We multiplex the four int |
177 * Spliterator for int streams. We multiplex the four int versions into one class by treating a |
190 * versions into one class by treating a bound less than origin as |
178 * bound less than origin as unbounded, and also by treating "infinite" as equivalent to |
191 * unbounded, and also by treating "infinite" as equivalent to |
179 * {@code Long.MAX_VALUE}. For splits, it uses the standard divide-by-two approach. The long and |
192 * Long.MAX_VALUE. For splits, it uses the standard divide-by-two |
180 * double versions of this class are identical except for types. |
193 * approach. The long and double versions of this class are |
181 */ |
194 * identical except for types. |
182 static class RandomIntsSpliterator extends RNGSupport.RandomSpliterator implements Spliterator.OfInt { |
195 */ |
183 final SplittableRNG generatingRNG; |
196 static class RandomIntsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfInt { |
|
197 final SplittableRng generatingRng; |
|
198 final int origin; |
184 final int origin; |
199 final int bound; |
185 final int bound; |
200 |
186 |
201 RandomIntsSpliterator(SplittableRng generatingRng, long index, long fence, int origin, int bound) { |
187 RandomIntsSpliterator(SplittableRNG generatingRNG, long index, long fence, int origin, int bound) { |
202 super(index, fence); |
188 super(index, fence); |
203 this.generatingRng = generatingRng; |
189 this.generatingRNG = generatingRNG; |
204 this.origin = origin; this.bound = bound; |
190 this.origin = origin; this.bound = bound; |
205 } |
191 } |
206 |
192 |
207 public Spliterator.OfInt trySplit() { |
193 public Spliterator.OfInt trySplit() { |
208 long i = index, m = (i + fence) >>> 1; |
194 long i = index, m = (i + fence) >>> 1; |
209 if (m <= i) return null; |
195 if (m <= i) return null; |
210 index = m; |
196 index = m; |
211 return new RandomIntsSpliterator(generatingRng.split(), i, m, origin, bound); |
197 return new RandomIntsSpliterator(generatingRNG.split(), i, m, origin, bound); |
212 } |
198 } |
213 |
199 |
214 public boolean tryAdvance(IntConsumer consumer) { |
200 public boolean tryAdvance(IntConsumer consumer) { |
215 if (consumer == null) throw new NullPointerException(); |
201 if (consumer == null) throw new NullPointerException(); |
216 long i = index, f = fence; |
202 long i = index, f = fence; |
217 if (i < f) { |
203 if (i < f) { |
218 consumer.accept(RngSupport.boundedNextInt(generatingRng, origin, bound)); |
204 consumer.accept(RNGSupport.boundedNextInt(generatingRNG, origin, bound)); |
219 index = i + 1; |
205 index = i + 1; |
220 return true; |
206 return true; |
221 } |
207 } |
222 else return false; |
208 else return false; |
223 } |
209 } |
317 public void forEachRemaining(DoubleConsumer consumer) { |
303 public void forEachRemaining(DoubleConsumer consumer) { |
318 if (consumer == null) throw new NullPointerException(); |
304 if (consumer == null) throw new NullPointerException(); |
319 long i = index, f = fence; |
305 long i = index, f = fence; |
320 if (i < f) { |
306 if (i < f) { |
321 index = f; |
307 index = f; |
322 Rng r = generatingRng; |
308 RandomNumberGenerator r = generatingRNG; |
323 double o = origin, b = bound; |
309 double o = origin, b = bound; |
324 do { |
310 do { |
325 consumer.accept(RngSupport.boundedNextDouble(r, o, b)); |
311 consumer.accept(RNGSupport.boundedNextDouble(r, o, b)); |
326 } while (++i < f); |
312 } while (++i < f); |
327 } |
313 } |
328 } |
314 } |
329 } |
315 } |
330 |
316 |
331 /** |
317 /** |
332 * Spliterator for stream of generators of type SplittableRng. We multiplex the two |
318 * Spliterator for stream of generators of type SplittableRNG. We multiplex the two |
333 * versions into one class by treating "infinite" as equivalent to Long.MAX_VALUE. |
319 * versions into one class by treating "infinite" as equivalent to Long.MAX_VALUE. |
334 * For splits, it uses the standard divide-by-two approach. |
320 * For splits, it uses the standard divide-by-two approach. |
335 */ |
321 */ |
336 static class RandomSplitsSpliterator extends RngSupport.RandomSpliterator implements Spliterator<SplittableRng> { |
322 static class RandomSplitsSpliterator extends RNGSupport.RandomSpliterator implements Spliterator<SplittableRNG> { |
337 final SplittableRng generatingRng; |
323 final SplittableRNG generatingRNG; |
338 final SplittableRng constructingRng; |
324 final SplittableRNG constructingRNG; |
339 |
325 |
340 RandomSplitsSpliterator(SplittableRng generatingRng, long index, long fence, SplittableRng constructingRng) { |
326 RandomSplitsSpliterator(SplittableRNG generatingRNG, long index, long fence, SplittableRNG constructingRNG) { |
341 super(index, fence); |
327 super(index, fence); |
342 this.generatingRng = generatingRng; |
328 this.generatingRNG = generatingRNG; |
343 this.constructingRng = constructingRng; |
329 this.constructingRNG = constructingRNG; |
344 } |
330 } |
345 |
331 |
346 public Spliterator<SplittableRng> trySplit() { |
332 public Spliterator<SplittableRNG> trySplit() { |
347 long i = index, m = (i + fence) >>> 1; |
333 long i = index, m = (i + fence) >>> 1; |
348 if (m <= i) return null; |
334 if (m <= i) return null; |
349 index = m; |
335 index = m; |
350 return new RandomSplitsSpliterator(generatingRng.split(), i, m, constructingRng); |
336 return new RandomSplitsSpliterator(generatingRNG.split(), i, m, constructingRNG); |
351 } |
337 } |
352 |
338 |
353 public boolean tryAdvance(Consumer<? super SplittableRng> consumer) { |
339 public boolean tryAdvance(Consumer<? super SplittableRNG> consumer) { |
354 if (consumer == null) throw new NullPointerException(); |
340 if (consumer == null) throw new NullPointerException(); |
355 long i = index, f = fence; |
341 long i = index, f = fence; |
356 if (i < f) { |
342 if (i < f) { |
357 consumer.accept(constructingRng.split(generatingRng)); |
343 consumer.accept(constructingRNG.split(generatingRNG)); |
358 index = i + 1; |
344 index = i + 1; |
359 return true; |
345 return true; |
360 } |
346 } |
361 else return false; |
347 else return false; |
362 } |
348 } |
363 |
349 |
364 public void forEachRemaining(Consumer<? super SplittableRng> consumer) { |
350 public void forEachRemaining(Consumer<? super SplittableRNG> consumer) { |
365 if (consumer == null) throw new NullPointerException(); |
351 if (consumer == null) throw new NullPointerException(); |
366 long i = index, f = fence; |
352 long i = index, f = fence; |
367 if (i < f) { |
353 if (i < f) { |
368 index = f; |
354 index = f; |
369 SplittableRng c = constructingRng; |
355 SplittableRNG c = constructingRNG; |
370 SplittableRng r = generatingRng; |
356 SplittableRNG r = generatingRNG; |
371 do { |
357 do { |
372 consumer.accept(c.split(r)); |
358 consumer.accept(c.split(r)); |
373 } while (++i < f); |
359 } while (++i < f); |
374 } |
360 } |
375 } |
361 } |