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 package java.util.random; |
26 |
27 |
27 import java.util.Spliterator; |
28 import java.util.Spliterator; |
28 import java.util.function.Consumer; |
29 import java.util.stream.Stream; |
29 import java.util.function.IntConsumer; |
30 import java.util.stream.DoubleStream; |
30 import java.util.function.LongConsumer; |
|
31 import java.util.function.DoubleConsumer; |
|
32 import java.util.stream.StreamSupport; |
|
33 import java.util.stream.IntStream; |
31 import java.util.stream.IntStream; |
34 import java.util.stream.LongStream; |
32 import java.util.stream.LongStream; |
35 import java.util.stream.DoubleStream; |
33 import java.util.stream.StreamSupport; |
36 |
34 |
37 /** |
35 /** |
38 * This class overrides the stream-producing methods (such as {@code ints()}) |
36 * This class overrides the stream-producing methods (such as {@code ints()}) |
39 * in class {@code AbstractRng} to provide {@code Spliterator}-based |
37 * in class {@link AbstractRNG} to provide {@link Spliterator}-based |
40 * implmentations that support potentially parallel execution. |
38 * implmentations that support potentially parallel execution. |
41 * |
39 * |
42 * To implement a pseudorandom number generator, the programmer needs |
40 * To implement a pseudorandom number generator, the programmer needs |
43 * only to extend this class and provide implementations for the methods |
41 * only to extend this class and provide implementations for the methods |
44 * {@code nextInt()}, {@code nextLong()}, {@code makeIntsSpliterator}, |
42 * {@code nextInt()}, {@code nextLong()}, {@code makeIntsSpliterator}, |
45 * {@code makeLongsSpliterator}, and {@code makeDoublesSpliterator}. |
43 * {@code makeLongsSpliterator}, and {@code makeDoublesSpliterator}. |
46 * |
44 * |
47 * This class is not public; it provides shared code to the public |
45 * This class is not public; it provides shared code to the public |
48 * classes {@code AbstractSplittableRng}, {@code AbstractSharedRng}, |
46 * classes {@link AbstractSplittableRNG}, {@link AbstractSharedRNG}, |
49 * and {@code AbstractArbitrarilyJumpableRng}. |
47 * and {@link AbstractArbitrarilyJumpableRNG}. |
50 * |
48 * |
51 * @author Guy Steele |
49 * @since 14 |
52 * @author Doug Lea |
|
53 * @since 1.9 |
|
54 */ |
50 */ |
55 |
51 |
56 abstract class AbstractSpliteratorRng implements Rng { |
52 abstract class AbstractSpliteratorRNG implements RandomNumberGenerator { |
57 /* |
53 /* |
58 * Implementation Overview. |
54 * Implementation Overview. |
59 * |
55 * |
60 * This class provides most of the "user API" methods needed to |
56 * This class provides most of the "user API" methods needed to |
61 * satisfy the interface java.util.Rng. An implementation of this |
57 * satisfy the interface RandomNumberGenerator. An implementation of this |
62 * interface need only extend this class and provide implementations |
58 * interface need only extend this class and provide implementations |
63 * of six methods: nextInt, nextLong, and nextDouble (the versions |
59 * of six methods: nextInt, nextLong, and nextDouble (the versions |
64 * that take no arguments) and makeIntsSpliterator, |
60 * that take no arguments) and makeIntsSpliterator, |
65 * makeLongsSpliterator, and makeDoublesSpliterator. |
61 * makeLongsSpliterator, and makeDoublesSpliterator. |
66 * |
62 * |
101 * @implNote The implementation of this method is effectively |
107 * @implNote The implementation of this method is effectively |
102 * equivalent to {@code ints(Long.MAX_VALUE)}. |
108 * equivalent to {@code ints(Long.MAX_VALUE)}. |
103 * |
109 * |
104 * @return a stream of pseudorandomly chosen {@code int} values |
110 * @return a stream of pseudorandomly chosen {@code int} values |
105 */ |
111 */ |
106 |
112 |
107 public IntStream ints() { |
113 public IntStream ints() { |
108 return StreamSupport.intStream |
114 return intStream(makeIntsSpliterator(0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0)); |
109 (makeIntsSpliterator(0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0), |
115 } |
110 false); |
116 |
111 } |
117 /** |
112 |
118 * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code int} |
113 /** |
119 * values from this generator and/or one split from it; each value conforms to the given origin |
114 * Returns a stream producing the given {@code streamSize} number |
120 * (inclusive) and bound (exclusive). |
115 * of pseudorandom {@code int} values from this generator and/or one split |
121 * |
116 * from it; each value conforms to the given origin (inclusive) and bound |
122 * @param streamSize the number of values to generate |
117 * (exclusive). |
123 * @param randomNumberOrigin the origin (inclusive) of each random value |
|
124 * @param randomNumberBound the bound (exclusive) of each random value |
|
125 * |
|
126 * @return a stream of pseudorandom {@code int} values, each with the given origin (inclusive) |
|
127 * and bound (exclusive) |
|
128 * |
|
129 * @throws IllegalArgumentException if {@code streamSize} is less than zero, or {@code |
|
130 * randomNumberOrigin} is greater than or equal to {@code |
|
131 * randomNumberBound} |
|
132 */ |
|
133 public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) { |
|
134 RNGSupport.checkStreamSize(streamSize); |
|
135 RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); |
|
136 return intStream(makeIntsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound)); |
|
137 } |
|
138 |
|
139 /** |
|
140 * Returns an effectively unlimited stream of pseudorandom {@code int} values from this |
|
141 * generator and/or one split from it; each value conforms to the given origin (inclusive) and |
|
142 * bound (exclusive). |
|
143 * |
|
144 * @param randomNumberOrigin the origin (inclusive) of each random value |
|
145 * @param randomNumberBound the bound (exclusive) of each random value |
|
146 * |
|
147 * @return a stream of pseudorandom {@code int} values, each with the given origin (inclusive) |
|
148 * and bound (exclusive) |
|
149 * |
|
150 * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to |
|
151 * {@code randomNumberBound} |
|
152 * |
|
153 * @implNote This method is implemented to be equivalent to {@code ints(Long.MAX_VALUE, |
|
154 * randomNumberOrigin, randomNumberBound)}. |
|
155 */ |
|
156 public IntStream ints(int randomNumberOrigin, int randomNumberBound) { |
|
157 RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); |
|
158 return intStream(makeIntsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)); |
|
159 } |
|
160 |
|
161 /** |
|
162 * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code long} |
|
163 * values from this generator and/or one split from it. |
118 * |
164 * |
119 * @param streamSize the number of values to generate |
165 * @param streamSize the number of values to generate |
120 * @param randomNumberOrigin the origin (inclusive) of each random value |
166 * |
121 * @param randomNumberBound the bound (exclusive) of each random value |
167 * @return a stream of pseudorandom {@code long} values |
122 * @return a stream of pseudorandom {@code int} values, |
168 * |
123 * each with the given origin (inclusive) and bound (exclusive) |
169 * @throws IllegalArgumentException if {@code streamSize} is less than zero |
124 * @throws IllegalArgumentException if {@code streamSize} is |
170 */ |
125 * less than zero, or {@code randomNumberOrigin} |
171 public LongStream longs(long streamSize) { |
126 * is greater than or equal to {@code randomNumberBound} |
172 RNGSupport.checkStreamSize(streamSize); |
127 */ |
173 return longStream(makeLongsSpliterator(0L, streamSize, Long.MAX_VALUE, 0L)); |
128 public IntStream ints(long streamSize, int randomNumberOrigin, |
174 } |
129 int randomNumberBound) { |
175 |
130 RngSupport.checkStreamSize(streamSize); |
176 /** |
131 RngSupport.checkRange(randomNumberOrigin, randomNumberBound); |
177 * Returns an effectively unlimited stream of pseudorandom {@code long} values from this |
132 return StreamSupport.intStream |
178 * generator and/or one split from it. |
133 (makeIntsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound), |
179 * |
134 false); |
180 * @return a stream of pseudorandom {@code long} values |
135 } |
|
136 |
|
137 /** |
|
138 * Returns an effectively unlimited stream of pseudorandom {@code |
|
139 * int} values from this generator and/or one split from it; each value |
|
140 * conforms to the given origin (inclusive) and bound (exclusive). |
|
141 * |
181 * |
142 * @implNote This method is implemented to be equivalent to {@code |
182 * @implNote This method is implemented to be equivalent to {@code |
143 * ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
183 * longs(Long.MAX_VALUE)}. |
144 * |
|
145 * @param randomNumberOrigin the origin (inclusive) of each random value |
|
146 * @param randomNumberBound the bound (exclusive) of each random value |
|
147 * @return a stream of pseudorandom {@code int} values, |
|
148 * each with the given origin (inclusive) and bound (exclusive) |
|
149 * @throws IllegalArgumentException if {@code randomNumberOrigin} |
|
150 * is greater than or equal to {@code randomNumberBound} |
|
151 */ |
|
152 public IntStream ints(int randomNumberOrigin, int randomNumberBound) { |
|
153 RngSupport.checkRange(randomNumberOrigin, randomNumberBound); |
|
154 return StreamSupport.intStream |
|
155 (makeIntsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
|
156 false); |
|
157 } |
|
158 |
|
159 /** |
|
160 * Returns a stream producing the given {@code streamSize} number |
|
161 * of pseudorandom {@code long} values from this generator and/or |
|
162 * one split from it. |
|
163 * |
|
164 * @param streamSize the number of values to generate |
|
165 * @return a stream of pseudorandom {@code long} values |
|
166 * @throws IllegalArgumentException if {@code streamSize} is |
|
167 * less than zero |
|
168 */ |
|
169 public LongStream longs(long streamSize) { |
|
170 RngSupport.checkStreamSize(streamSize); |
|
171 return StreamSupport.longStream |
|
172 (makeLongsSpliterator(0L, streamSize, Long.MAX_VALUE, 0L), |
|
173 false); |
|
174 } |
|
175 |
|
176 /** |
|
177 * Returns an effectively unlimited stream of pseudorandom {@code |
|
178 * long} values from this generator and/or one split from it. |
|
179 * |
|
180 * @implNote This method is implemented to be equivalent to {@code |
|
181 * longs(Long.MAX_VALUE)}. |
|
182 * |
|
183 * @return a stream of pseudorandom {@code long} values |
|
184 */ |
184 */ |
185 public LongStream longs() { |
185 public LongStream longs() { |
186 return StreamSupport.longStream |
186 return longStream(makeLongsSpliterator(0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L)); |
187 (makeLongsSpliterator(0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L), |
187 } |
188 false); |
188 |
189 } |
189 /** |
190 |
190 * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code long} |
191 /** |
191 * values from this generator and/or one split from it; each value conforms to the given origin |
192 * Returns a stream producing the given {@code streamSize} number of |
192 * (inclusive) and bound (exclusive). |
193 * pseudorandom {@code long} values from this generator and/or one split |
193 * |
194 * from it; each value conforms to the given origin (inclusive) and bound |
194 * @param streamSize the number of values to generate |
195 * (exclusive). |
195 * @param randomNumberOrigin the origin (inclusive) of each random value |
196 * |
196 * @param randomNumberBound the bound (exclusive) of each random value |
197 * @param streamSize the number of values to generate |
197 * |
198 * @param randomNumberOrigin the origin (inclusive) of each random value |
198 * @return a stream of pseudorandom {@code long} values, each with the given origin (inclusive) |
199 * @param randomNumberBound the bound (exclusive) of each random value |
199 * and bound (exclusive) |
200 * @return a stream of pseudorandom {@code long} values, |
200 * |
201 * each with the given origin (inclusive) and bound (exclusive) |
201 * @throws IllegalArgumentException if {@code streamSize} is less than zero, or {@code |
202 * @throws IllegalArgumentException if {@code streamSize} is |
202 * randomNumberOrigin} is greater than or equal to {@code |
203 * less than zero, or {@code randomNumberOrigin} |
203 * randomNumberBound} |
204 * is greater than or equal to {@code randomNumberBound} |
|
205 */ |
204 */ |
206 public LongStream longs(long streamSize, long randomNumberOrigin, |
205 public LongStream longs(long streamSize, long randomNumberOrigin, |
207 long randomNumberBound) { |
206 long randomNumberBound) { |
208 RngSupport.checkStreamSize(streamSize); |
207 RNGSupport.checkStreamSize(streamSize); |
209 RngSupport.checkRange(randomNumberOrigin, randomNumberBound); |
208 RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); |
210 return StreamSupport.longStream |
209 return longStream(makeLongsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound)); |
211 (makeLongsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound), |
210 } |
212 false); |
211 |
213 } |
212 /** |
214 |
213 * Returns an effectively unlimited stream of pseudorandom {@code long} values from this |
215 /** |
214 * generator and/or one split from it; each value conforms to the given origin (inclusive) and |
216 * Returns an effectively unlimited stream of pseudorandom {@code |
215 * bound (exclusive). |
217 * long} values from this generator and/or one split from it; each value |
216 * |
218 * conforms to the given origin (inclusive) and bound (exclusive). |
217 * @param randomNumberOrigin the origin (inclusive) of each random value |
219 * |
218 * @param randomNumberBound the bound (exclusive) of each random value |
220 * @implNote This method is implemented to be equivalent to {@code |
219 * |
221 * longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
220 * @return a stream of pseudorandom {@code long} values, each with the given origin (inclusive) |
222 * |
221 * and bound (exclusive) |
223 * @param randomNumberOrigin the origin (inclusive) of each random value |
222 * |
224 * @param randomNumberBound the bound (exclusive) of each random value |
223 * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to |
225 * @return a stream of pseudorandom {@code long} values, |
224 * {@code randomNumberBound} |
226 * each with the given origin (inclusive) and bound (exclusive) |
225 * |
227 * @throws IllegalArgumentException if {@code randomNumberOrigin} |
226 * @implNote This method is implemented to be equivalent to {@code longs(Long.MAX_VALUE, |
228 * is greater than or equal to {@code randomNumberBound} |
227 * randomNumberOrigin, randomNumberBound)}. |
229 */ |
228 */ |
230 public LongStream longs(long randomNumberOrigin, long randomNumberBound) { |
229 public LongStream longs(long randomNumberOrigin, long randomNumberBound) { |
231 RngSupport.checkRange(randomNumberOrigin, randomNumberBound); |
230 RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); |
232 return StreamSupport.longStream |
231 return StreamSupport.longStream |
233 (makeLongsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
232 (makeLongsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
234 false); |
233 false); |
235 } |
234 } |
236 |
235 |
237 /** |
236 /** |
238 * Returns a stream producing the given {@code streamSize} number of |
237 * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code double} |
239 * pseudorandom {@code double} values from this generator and/or one split |
238 * values from this generator and/or one split from it; each value is between zero (inclusive) |
240 * from it; each value is between zero (inclusive) and one (exclusive). |
239 * and one (exclusive). |
241 * |
240 * |
242 * @param streamSize the number of values to generate |
241 * @param streamSize the number of values to generate |
|
242 * |
243 * @return a stream of {@code double} values |
243 * @return a stream of {@code double} values |
244 * @throws IllegalArgumentException if {@code streamSize} is |
244 * |
245 * less than zero |
245 * @throws IllegalArgumentException if {@code streamSize} is less than zero |
246 */ |
246 */ |
247 public DoubleStream doubles(long streamSize) { |
247 public DoubleStream doubles(long streamSize) { |
248 RngSupport.checkStreamSize(streamSize); |
248 RNGSupport.checkStreamSize(streamSize); |
249 return StreamSupport.doubleStream |
249 return doubleStream(makeDoublesSpliterator(0L, streamSize, Double.MAX_VALUE, 0.0)); |
250 (makeDoublesSpliterator(0L, streamSize, Double.MAX_VALUE, 0.0), |
250 } |
251 false); |
251 |
252 } |
252 /** |
253 |
253 * Returns an effectively unlimited stream of pseudorandom {@code double} values from this |
254 /** |
254 * generator and/or one split from it; each value is between zero (inclusive) and one |
255 * Returns an effectively unlimited stream of pseudorandom {@code |
255 * (exclusive). |
256 * double} values from this generator and/or one split from it; each value |
256 * |
257 * is between zero (inclusive) and one (exclusive). |
257 * @return a stream of pseudorandom {@code double} values |
258 * |
258 * |
259 * @implNote This method is implemented to be equivalent to {@code |
259 * @implNote This method is implemented to be equivalent to {@code |
260 * doubles(Long.MAX_VALUE)}. |
260 * doubles(Long.MAX_VALUE)}. |
261 * |
|
262 * @return a stream of pseudorandom {@code double} values |
|
263 */ |
261 */ |
264 public DoubleStream doubles() { |
262 public DoubleStream doubles() { |
265 return StreamSupport.doubleStream |
263 return doubleStream(makeDoublesSpliterator(0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0)); |
266 (makeDoublesSpliterator(0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0), |
264 } |
267 false); |
265 |
268 } |
266 /** |
269 |
267 * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code double} |
270 /** |
268 * values from this generator and/or one split from it; each value conforms to the given origin |
271 * Returns a stream producing the given {@code streamSize} number of |
269 * (inclusive) and bound (exclusive). |
272 * pseudorandom {@code double} values from this generator and/or one split |
270 * |
273 * from it; each value conforms to the given origin (inclusive) and bound |
271 * @param streamSize the number of values to generate |
274 * (exclusive). |
272 * @param randomNumberOrigin the origin (inclusive) of each random value |
275 * |
273 * @param randomNumberBound the bound (exclusive) of each random value |
276 * @param streamSize the number of values to generate |
274 * |
277 * @param randomNumberOrigin the origin (inclusive) of each random value |
275 * @return a stream of pseudorandom {@code double} values, each with the given origin |
278 * @param randomNumberBound the bound (exclusive) of each random value |
276 * (inclusive) and bound (exclusive) |
279 * @return a stream of pseudorandom {@code double} values, |
277 * |
280 * each with the given origin (inclusive) and bound (exclusive) |
278 * @throws IllegalArgumentException if {@code streamSize} is less than zero |
281 * @throws IllegalArgumentException if {@code streamSize} is |
279 * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to |
282 * less than zero |
280 * {@code randomNumberBound} |
283 * @throws IllegalArgumentException if {@code randomNumberOrigin} |
281 */ |
284 * is greater than or equal to {@code randomNumberBound} |
282 public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) { |
285 */ |
283 RNGSupport.checkStreamSize(streamSize); |
286 public DoubleStream doubles(long streamSize, double randomNumberOrigin, |
284 RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); |
287 double randomNumberBound) { |
285 return doubleStream(makeDoublesSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound)); |
288 RngSupport.checkStreamSize(streamSize); |
286 } |
289 RngSupport.checkRange(randomNumberOrigin, randomNumberBound); |
287 |
290 return StreamSupport.doubleStream |
288 /** |
291 (makeDoublesSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound), |
289 * Returns an effectively unlimited stream of pseudorandom {@code double} values from this |
292 false); |
290 * generator and/or one split from it; each value conforms to the given origin (inclusive) and |
293 } |
291 * bound (exclusive). |
294 |
292 * |
295 /** |
293 * @param randomNumberOrigin the origin (inclusive) of each random value |
296 * Returns an effectively unlimited stream of pseudorandom {@code |
294 * @param randomNumberBound the bound (exclusive) of each random value |
297 * double} values from this generator and/or one split from it; each value |
295 * |
298 * conforms to the given origin (inclusive) and bound (exclusive). |
296 * @return a stream of pseudorandom {@code double} values, each with the given origin |
|
297 * (inclusive) and bound (exclusive) |
|
298 * |
|
299 * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to |
|
300 * {@code randomNumberBound} |
299 * |
301 * |
300 * @implNote This method is implemented to be equivalent to {@code |
302 * @implNote This method is implemented to be equivalent to {@code |
301 * doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
303 * doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. |
302 * |
|
303 * @param randomNumberOrigin the origin (inclusive) of each random value |
|
304 * @param randomNumberBound the bound (exclusive) of each random value |
|
305 * @return a stream of pseudorandom {@code double} values, |
|
306 * each with the given origin (inclusive) and bound (exclusive) |
|
307 * @throws IllegalArgumentException if {@code randomNumberOrigin} |
|
308 * is greater than or equal to {@code randomNumberBound} |
|
309 */ |
304 */ |
310 public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { |
305 public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { |
311 RngSupport.checkRange(randomNumberOrigin, randomNumberBound); |
306 RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); |
312 return StreamSupport.doubleStream |
307 return doubleStream(makeDoublesSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)); |
313 (makeDoublesSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), |
|
314 false); |
|
315 } |
308 } |
316 |
309 |
317 } |
310 } |