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.math.BigInteger; |
28 import java.math.BigInteger; |
28 import java.util.concurrent.atomic.AtomicLong; |
29 import java.util.concurrent.atomic.AtomicLong; |
29 |
30 |
30 /** |
31 /** |
31 * A generator of uniform pseudorandom values applicable for use in |
32 * A generator of uniform pseudorandom values applicable for use in |
32 * (among other contexts) isolated parallel computations that may |
33 * (among other contexts) isolated parallel computations that may |
33 * generate subtasks. Class {@code L32X64MixRandom} implements |
34 * generate subtasks. Class {@link L32X64MixRandom} implements |
34 * interfaces {@link java.util.Rng} and {@link java.util.SplittableRng}, |
35 * interfaces {@link RandomNumberGenerator} and {@link SplittableRNG}, |
35 * and therefore supports methods for producing pseudorandomly chosen |
36 * and therefore supports methods for producing pseudorandomly chosen |
36 * numbers of type {@code int}, {@code long}, {@code float}, and {@code double} |
37 * numbers of type {@code int}, {@code long}, {@code float}, and {@code double} |
37 * as well as creating new split-off {@code L32X64MixRandom} objects, |
38 * as well as creating new split-off {@link L32X64MixRandom} objects, |
38 * with similar usages as for class {@link java.util.SplittableRandom}. |
39 * with similar usages as for class {@link java.util.SplittableRandom}. |
39 * |
40 * <p> |
40 * <p>Series of generated values pass the TestU01 BigCrush and PractRand test suites |
41 * Series of generated values pass the TestU01 BigCrush and PractRand test suites |
41 * that measure independence and uniformity properties of random number generators. |
42 * that measure independence and uniformity properties of random number generators. |
42 * (Most recently validated with |
43 * (Most recently validated with |
43 * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a> |
44 * <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a> |
44 * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>. |
45 * and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>. |
45 * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()} |
46 * Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()} |
46 * method but also the result of bit-reversing each value produced by {@code nextLong()}.) |
47 * method but also the result of bit-reversing each value produced by {@code nextLong()}.) |
47 * These tests validate only the methods for certain |
48 * These tests validate only the methods for certain |
48 * types and ranges, but similar properties are expected to hold, at |
49 * types and ranges, but similar properties are expected to hold, at |
49 * least approximately, for others as well. |
50 * least approximately, for others as well. |
50 * |
51 * <p> |
51 * <p>{@code L32X64MixRandom} is a specific member of the LXM family of algorithms |
52 * {@link L32X64MixRandom} is a specific member of the LXM family of algorithms |
52 * for pseudorandom number generators. Every LXM generator consists of two |
53 * for pseudorandom number generators. Every LXM generator consists of two |
53 * subgenerators; one is an LCG (Linear Congruential Generator) and the other is |
54 * subgenerators; one is an LCG (Linear Congruential Generator) and the other is |
54 * an Xorshift generator. Each output of an LXM generator is the sum of one |
55 * an Xorshift generator. Each output of an LXM generator is the sum of one |
55 * output from each subgenerator, possibly processed by a final mixing function |
56 * output from each subgenerator, possibly processed by a final mixing function |
56 * (and {@code L32X64MixRandom} does use a mixing function). |
57 * (and {@link L32X64MixRandom} does use a mixing function). |
57 * |
58 * <p> |
58 * <p>The LCG subgenerator for {@code L32X64MixRandom} has an update step of the |
59 * The LCG subgenerator for {@link L32X64MixRandom} has an update step of the |
59 * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all |
60 * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all |
60 * of type {@code int}; {@code s} is the mutable state, the multiplier {@code m} |
61 * of type {@code int}; {@code s} is the mutable state, the multiplier {@code m} |
61 * is fixed (the same for all instances of {@code L32X64MixRandom}}) and the addend |
62 * is fixed (the same for all instances of {@link L32X64MixRandom}}) and the addend |
62 * {@code a} is a parameter (a final field of the instance). The parameter |
63 * {@code a} is a parameter (a final field of the instance). The parameter |
63 * {@code a} is required to be odd (this allows the LCG to have the maximal |
64 * {@code a} is required to be odd (this allows the LCG to have the maximal |
64 * period, namely 2<sup>32</sup>); therefore there are 2<sup>31</sup> distinct choices |
65 * period, namely 2<sup>32</sup>); therefore there are 2<sup>31</sup> distinct choices |
65 * of parameter. |
66 * of parameter. |
66 * |
67 * <p> |
67 * <p>The Xorshift subgenerator for {@code L32X64MixRandom} is the {@code xoroshiro64} algorithm, |
68 * The Xorshift subgenerator for {@link L32X64MixRandom} is the {@code xoroshiro64} algorithm, |
68 * version 1.0 (parameters 26, 9, 13), without any final scrambler such as "+" or "**". |
69 * version 1.0 (parameters 26, 9, 13), without any final scrambler such as "+" or "**". |
69 * Its state consists of two {@code int} fields {@code x0} and {@code x1}, |
70 * Its state consists of two {@code int} fields {@code x0} and {@code x1}, |
70 * which can take on any values provided that they are not both zero. |
71 * which can take on any values provided that they are not both zero. |
71 * The period of this subgenerator is 2<sup>64</sup>-1. |
72 * The period of this subgenerator is 2<sup>64</sup>-1. |
72 * |
73 * <p> |
73 * <p> The mixing function for {@code L32X64MixRandom} is the "starstar" mixing function. |
74 * The mixing function for {@link L32X64MixRandom} is the "starstar" mixing function. |
74 * |
75 * <p> |
75 * <p> Because the periods 2<sup>32</sup> and 2<sup>64</sup>-1 of the two subgenerators |
76 * Because the periods 2<sup>32</sup> and 2<sup>64</sup>-1 of the two subgenerators |
76 * are relatively prime, the <em>period</em> of any single {@code L32X64MixRandom} object |
77 * are relatively prime, the <em>period</em> of any single {@link L32X64MixRandom} object |
77 * (the length of the series of generated 32-bit values before it repeats) is the product |
78 * (the length of the series of generated 32-bit values before it repeats) is the product |
78 * of the periods of the subgenerators, that is, 2<sup>32</sup>(2<sup>64</sup>-1), |
79 * of the periods of the subgenerators, that is, 2<sup>32</sup>(2<sup>64</sup>-1), |
79 * which is just slightly smaller than 2<sup>96</sup>. Moreover, if two distinct |
80 * which is just slightly smaller than 2<sup>96</sup>. Moreover, if two distinct |
80 * {@code L32X64MixRandom} objects have different {@code a} parameters, then their |
81 * {@link L32X64MixRandom} objects have different {@code a} parameters, then their |
81 * cycles of produced values will be different. |
82 * cycles of produced values will be different. |
82 * |
83 * <p> |
83 * <p>The 32-bit values produced by the {@code nextInt()} method are exactly equidistributed. |
84 * The 32-bit values produced by the {@code nextInt()} method are exactly equidistributed. |
84 * For any specific instance of {@code L32X64MixRandom}, over the course of its cycle each |
85 * For any specific instance of {@link L32X64MixRandom}, over the course of its cycle each |
85 * of the 2<sup>32</sup> possible {@code int} values will be produced 2<sup>64</sup>-1 times. |
86 * of the 2<sup>32</sup> possible {@code int} values will be produced 2<sup>64</sup>-1 times. |
86 * The values produced by the {@code nextFloat()} method are likewise exactly equidistributed. |
87 * The values produced by the {@code nextFloat()} method are likewise exactly equidistributed. |
87 * |
88 * <p> |
88 * <p>In fact, the 32-bit values produced by the {@code nextInt()} method are 2-equidistributed. |
89 * In fact, the 32-bit values produced by the {@code nextInt()} method are 2-equidistributed. |
89 * To be precise: for any specific instance of {@code L32X64MixRandom}, consider |
90 * To be precise: for any specific instance of {@link L32X64MixRandom}, consider |
90 * the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by |
91 * the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by |
91 * {@code nextInt()} (assuming no other methods are called that would affect the state). |
92 * {@code nextInt()} (assuming no other methods are called that would affect the state). |
92 * There are 2<sup>32</sup>(2<sup>64</sup>-1) such subsequences, and each subsequence, |
93 * There are 2<sup>32</sup>(2<sup>64</sup>-1) such subsequences, and each subsequence, |
93 * which consists of 2 32-bit values, can have one of 2<sup>64</sup> values. Of those |
94 * which consists of 2 32-bit values, can have one of 2<sup>64</sup> values. Of those |
94 * 2<sup>64</sup> subsequence values, nearly all of them (2<sup>64</sup>-2<sup>32</sup>) |
95 * 2<sup>64</sup> subsequence values, nearly all of them (2<sup>64</sup>-2<sup>32</sup>) |
95 * occur 2<sup>32</sup> times over the course of the entire cycle, and the other |
96 * occur 2<sup>32</sup> times over the course of the entire cycle, and the other |
96 * 2<sup>32</sup> subsequence values occur only 2<sup>32</sup>-1 times. So the ratio |
97 * 2<sup>32</sup> subsequence values occur only 2<sup>32</sup>-1 times. So the ratio |
97 * of the probability of getting one of the less common subsequence values and the |
98 * of the probability of getting one of the less common subsequence values and the |
98 * probability of getting one of the more common subsequence values is 1-2<sup>-32</sup>. |
99 * probability of getting one of the more common subsequence values is 1-2<sup>-32</sup>. |
99 * (Note that the set of 2<sup>32</sup> less-common subsequence values will differ from |
100 * (Note that the set of 2<sup>32</sup> less-common subsequence values will differ from |
100 * one instance of {@code L32X64MixRandom} to another, as a function of the additive |
101 * one instance of {@link L32X64MixRandom} to another, as a function of the additive |
101 * parameter of the LCG.) As a consequence, the values produced by the {@code nextFloat()} |
102 * parameter of the LCG.) As a consequence, the values produced by the {@code nextFloat()} |
102 * method are likewise 2-equidistributed, and the values produced by the {@code nextLong()} |
103 * method are likewise 2-equidistributed, and the values produced by the {@code nextLong()} |
103 * and {@code nextDouble()} methods are equidistributed (but not 2-equidistributed). |
104 * and {@code nextDouble()} methods are equidistributed (but not 2-equidistributed). |
104 * |
105 * <p> |
105 * <p>Method {@link #split} constructs and returns a new {@code L32X64MixRandom} |
106 * Method {@link #split} constructs and returns a new {@link L32X64MixRandom} |
106 * instance that shares no mutable state with the current instance. However, with |
107 * instance that shares no mutable state with the current instance. However, with |
107 * very high probability, the values collectively generated by the two objects |
108 * very high probability, the values collectively generated by the two objects |
108 * have the same statistical properties as if the same quantity of values were |
109 * have the same statistical properties as if the same quantity of values were |
109 * generated by a single thread using a single {@code L32X64MixRandom} object. |
110 * generated by a single thread using a single {@link L32X64MixRandom} object. |
110 * This is because, with high probability, distinct {@code L32X64MixRandom} objects |
111 * This is because, with high probability, distinct {@link L32X64MixRandom} objects |
111 * have distinct {@code a} parameters and therefore use distinct members of the |
112 * have distinct {@code a} parameters and therefore use distinct members of the |
112 * algorithmic family; and even if their {@code a} parameters are the same, with |
113 * algorithmic family; and even if their {@code a} parameters are the same, with |
113 * very high probability they will traverse different parts of their common state |
114 * very high probability they will traverse different parts of their common state |
114 * cycle. |
115 * cycle. |
115 * |
116 * <p> |
116 * <p>As with {@link java.util.SplittableRandom}, instances of |
117 * As with {@link java.util.SplittableRandom}, instances of |
117 * {@code L32X64MixRandom} are <em>not</em> thread-safe. |
118 * {@link L32X64MixRandom} are <em>not</em> thread-safe. |
118 * They are designed to be split, not shared, across threads. For |
119 * They are designed to be split, not shared, across threads. For |
119 * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style |
120 * example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style |
120 * computation using random numbers might include a construction |
121 * computation using random numbers might include a construction |
121 * of the form {@code new Subtask(someL32X64MixRandom.split()).fork()}. |
122 * of the form {@code new Subtask(someL32X64MixRandom.split()).fork()}. |
122 * |
123 * <p> |
123 * <p>This class provides additional methods for generating random |
124 * This class provides additional methods for generating random |
124 * streams, that employ the above techniques when used in |
125 * streams, that employ the above techniques when used in |
125 * {@code stream.parallel()} mode. |
126 * {@code stream.parallel()} mode. |
126 * |
127 * <p> |
127 * <p>Instances of {@code L32X64MixRandom} are not cryptographically |
128 * Instances of {@link L32X64MixRandom} are not cryptographically |
128 * secure. Consider instead using {@link java.security.SecureRandom} |
129 * secure. Consider instead using {@link java.security.SecureRandom} |
129 * in security-sensitive applications. Additionally, |
130 * in security-sensitive applications. Additionally, |
130 * default-constructed instances do not use a cryptographically random |
131 * default-constructed instances do not use a cryptographically random |
131 * seed unless the {@linkplain System#getProperty system property} |
132 * seed unless the {@linkplain System#getProperty system property} |
132 * {@code java.util.secureRandomSeed} is set to {@code true}. |
133 * {@code java.util.secureRandomSeed} is set to {@code true}. |
133 * |
134 * |
134 * @author Guy Steele |
135 * @since 14 |
135 * @since 1.9 |
|
136 */ |
136 */ |
137 public final class L32X64MixRandom extends AbstractSplittableRng { |
137 public final class L32X64MixRandom extends AbstractSplittableRNG { |
138 |
138 |
139 /* |
139 /* |
140 * Implementation Overview. |
140 * Implementation Overview. |
141 * |
141 * |
142 * The split operation uses the current generator to choose four new 64-bit |
142 * The split operation uses the current generator to choose four new 64-bit |
143 * int values that are then used to initialize the parameter `a` and the |
143 * int values that are then used to initialize the parameter `a` and the |
144 * state variables `s`, `x0`, and `x1` for a newly constructed generator. |
144 * state variables `s`, `x0`, and `x1` for a newly constructed generator. |
145 * |
145 * |
146 * With high probability, no two generators so chosen |
146 * With high probability, no two generators so chosen |
147 * will have the same `a` parameter, and testing has indicated |
147 * will have the same `a` parameter, and testing has indicated |
148 * that the values generated by two instances of {@code L32X64MixRandom} |
148 * that the values generated by two instances of {@link L32X64MixRandom} |
149 * will be (approximately) independent if have different values for `a`. |
149 * will be (approximately) independent if have different values for `a`. |
150 * |
150 * |
151 * The default (no-argument) constructor, in essence, uses |
151 * The default (no-argument) constructor, in essence, uses |
152 * "defaultGen" to generate four new 32-bit values for the same |
152 * "defaultGen" to generate four new 32-bit values for the same |
153 * purpose. Multiple generators created in this way will certainly |
153 * purpose. Multiple generators created in this way will certainly |
211 * @param s initial state for the LCG |
211 * @param s initial state for the LCG |
212 * @param x0 first word of the initial state for the xorshift generator |
212 * @param x0 first word of the initial state for the xorshift generator |
213 * @param x1 second word of the initial state for the xorshift generator |
213 * @param x1 second word of the initial state for the xorshift generator |
214 */ |
214 */ |
215 public L32X64MixRandom(int a, int s, int x0, int x1) { |
215 public L32X64MixRandom(int a, int s, int x0, int x1) { |
216 // Force a to be odd. |
216 // Force a to be odd. |
217 this.a = a | 1; |
217 this.a = a | 1; |
218 this.s = s; |
218 this.s = s; |
219 // If x0 and x1 are both zero, we must choose nonzero values. |
219 // If x0 and x1 are both zero, we must choose nonzero values. |
220 if ((x0 | x1) == 0) { |
220 if ((x0 | x1) == 0) { |
221 // At least one of the two values generated here will be nonzero. |
221 // At least one of the two values generated here will be nonzero. |
222 this.x0 = RngSupport.mixMurmur32(s += RngSupport.GOLDEN_RATIO_32); |
222 this.x0 = RNGSupport.mixMurmur32(s += RNGSupport.GOLDEN_RATIO_32); |
223 this.x1 = RngSupport.mixMurmur32(s + RngSupport.GOLDEN_RATIO_32); |
223 this.x1 = RNGSupport.mixMurmur32(s + RNGSupport.GOLDEN_RATIO_32); |
224 } |
224 } |
225 } |
225 } |
226 |
226 |
227 /** |
227 /** |
228 * Creates a new instance of {@code L32X64MixRandom} using the |
228 * Creates a new instance of {@link L32X64MixRandom} using the |
229 * specified {@code long} value as the initial seed. Instances of |
229 * specified {@code long} value as the initial seed. Instances of |
230 * {@code L32X64MixRandom} created with the same seed in the same |
230 * {@link L32X64MixRandom} created with the same seed in the same |
231 * program generate identical sequences of values. |
231 * program generate identical sequences of values. |
232 * |
232 * |
233 * @param seed the initial seed |
233 * @param seed the initial seed |
234 */ |
234 */ |
235 public L32X64MixRandom(long seed) { |
235 public L32X64MixRandom(long seed) { |
236 // Using a value with irregularly spaced 1-bits to xor the seed |
236 // Using a value with irregularly spaced 1-bits to xor the seed |
237 // argument tends to improve "pedestrian" seeds such as 0 or |
237 // argument tends to improve "pedestrian" seeds such as 0 or |
238 // other small integers. We may as well use SILVER_RATIO_64. |
238 // other small integers. We may as well use SILVER_RATIO_64. |
239 // |
239 // |
240 // The high half of the seed is hashed by mixMurmur32 to produce the `a` parameter. |
240 // The high half of the seed is hashed by mixMurmur32 to produce the `a` parameter. |
241 // The low half of the seed is hashed by mixMurmur32 to produce the initial `x0`, |
241 // The low half of the seed is hashed by mixMurmur32 to produce the initial `x0`, |
242 // which will then be used to produce the first generated value. |
242 // which will then be used to produce the first generated value. |
243 // Then x1 is filled in as if by a SplitMix PRNG with |
243 // Then x1 is filled in as if by a SplitMix PRNG with |
244 // GOLDEN_RATIO_32 as the gamma value and Murmur32 as the mixer. |
244 // GOLDEN_RATIO_32 as the gamma value and Murmur32 as the mixer. |
245 this(RngSupport.mixMurmur32((int)((seed ^= RngSupport.SILVER_RATIO_64) >>> 32)), |
245 this(RNGSupport.mixMurmur32((int)((seed ^= RNGSupport.SILVER_RATIO_64) >>> 32)), |
246 1, |
246 1, |
247 RngSupport.mixLea32((int)(seed)), |
247 RNGSupport.mixLea32((int)(seed)), |
248 RngSupport.mixLea32((int)(seed) + RngSupport.GOLDEN_RATIO_32)); |
248 RNGSupport.mixLea32((int)(seed) + RNGSupport.GOLDEN_RATIO_32)); |
249 } |
249 } |
250 |
250 |
251 /** |
251 /** |
252 * Creates a new instance of {@code L32X64MixRandom} that is likely to |
252 * Creates a new instance of {@link L32X64MixRandom} that is likely to |
253 * generate sequences of values that are statistically independent |
253 * generate sequences of values that are statistically independent |
254 * of those of any other instances in the current program execution, |
254 * of those of any other instances in the current program execution, |
255 * but may, and typically does, vary across program invocations. |
255 * but may, and typically does, vary across program invocations. |
256 */ |
256 */ |
257 public L32X64MixRandom() { |
257 public L32X64MixRandom() { |
258 // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values. |
258 // Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values. |
259 this(defaultGen.getAndAdd(RngSupport.GOLDEN_RATIO_64)); |
259 this(defaultGen.getAndAdd(RNGSupport.GOLDEN_RATIO_64)); |
260 } |
260 } |
261 |
261 |
262 /** |
262 /** |
263 * Creates a new instance of {@code L32X64MixRandom} using the specified array of |
263 * Creates a new instance of {@link L32X64MixRandom} using the specified array of |
264 * initial seed bytes. Instances of {@code L32X64MixRandom} created with the same |
264 * initial seed bytes. Instances of {@link L32X64MixRandom} created with the same |
265 * seed array in the same program execution generate identical sequences of values. |
265 * seed array in the same program execution generate identical sequences of values. |
266 * |
266 * |
267 * @param seed the initial seed |
267 * @param seed the initial seed |
268 */ |
268 */ |
269 public L32X64MixRandom(byte[] seed) { |
269 public L32X64MixRandom(byte[] seed) { |
270 // Convert the seed to 4 int values, of which the last 2 are not all zero. |
270 // Convert the seed to 4 int values, of which the last 2 are not all zero. |
271 int[] data = RngSupport.convertSeedBytesToInts(seed, 4, 2); |
271 int[] data = RNGSupport.convertSeedBytesToInts(seed, 4, 2); |
272 int a = data[0], s = data[1], x0 = data[2], x1 = data[3]; |
272 int a = data[0], s = data[1], x0 = data[2], x1 = data[3]; |
273 // Force a to be odd. |
273 // Force a to be odd. |
274 this.a = a | 1; |
274 this.a = a | 1; |
275 this.s = s; |
275 this.s = s; |
276 this.x0 = x0; |
276 this.x0 = x0; |
277 this.x1 = x1; |
277 this.x1 = x1; |
278 } |
278 } |
279 |
279 |
280 /* ---------------- public methods ---------------- */ |
280 /* ---------------- public methods ---------------- */ |
281 |
281 |
282 /** |
282 /** |
283 * Constructs and returns a new instance of {@code L32X64MixRandom} |
283 * Constructs and returns a new instance of {@link L32X64MixRandom} that shares no mutable state |
284 * that shares no mutable state with this instance. |
284 * with this instance. However, with very high probability, the set of values collectively |
285 * However, with very high probability, the set of values collectively |
285 * generated by the two objects has the same statistical properties as if same the quantity of |
286 * generated by the two objects has the same statistical properties as if |
286 * values were generated by a single thread using a single {@link L32X64MixRandom} object. |
287 * same the quantity of values were generated by a single thread using |
287 * Either or both of the two objects may be further split using the {@code split} method, and |
288 * a single {@code L32X64MixRandom} object. Either or both of the two |
288 * the same expected statistical properties apply to the entire set of generators constructed by |
289 * objects may be further split using the {@code split} method, |
289 * such recursive splitting. |
290 * and the same expected statistical properties apply to the |
290 * |
291 * entire set of generators constructed by such recursive splitting. |
291 * @param source a {@link SplittableRNG} instance to be used instead of this one as a source of |
292 * |
292 * pseudorandom bits used to initialize the state of the new ones. |
293 * @param source a {@code SplittableRng} instance to be used instead |
293 * |
294 * of this one as a source of pseudorandom bits used to |
294 * @return a new instance of {@link L32X64MixRandom} |
295 * initialize the state of the new ones. |
295 */ |
296 * @return a new instance of {@code L32X64MixRandom} |
296 public L32X64MixRandom split(SplittableRNG source) { |
297 */ |
297 // Literally pick a new instance "at random". |
298 public L32X64MixRandom split(SplittableRng source) { |
|
299 // Literally pick a new instance "at random". |
|
300 return new L32X64MixRandom(source.nextInt(), source.nextInt(), |
298 return new L32X64MixRandom(source.nextInt(), source.nextInt(), |
301 source.nextInt(), source.nextInt()); |
299 source.nextInt(), source.nextInt()); |
302 } |
300 } |
303 |
301 |
304 /** |
302 /** |
305 * Returns a pseudorandom {@code int} value. |
303 * Returns a pseudorandom {@code int} value. |
306 * |
304 * |
307 * @return a pseudorandom {@code int} value |
305 * @return a pseudorandom {@code int} value |
308 */ |
306 */ |
309 public int nextInt() { |
307 public int nextInt() { |
310 final int z = s + x0; |
308 final int z = s + x0; |
311 s = m * s + a; // LCG |
309 s = M * s + a; // LCG |
312 int q0 = x0, q1 = x1; |
310 int q0 = x0, q1 = x1; |
313 { q1 ^= q0; q0 = Integer.rotateLeft(q0, 26); q0 = q0 ^ q1 ^ (q1 << 9); q1 = Integer.rotateLeft(q1, 13); } // xoroshiro64 |
311 { q1 ^= q0; q0 = Integer.rotateLeft(q0, 26); q0 = q0 ^ q1 ^ (q1 << 9); q1 = Integer.rotateLeft(q1, 13); } // xoroshiro64 |
314 x0 = q0; x1 = q1; |
312 x0 = q0; x1 = q1; |
315 return Integer.rotateLeft(z * 5, 7) * 9; // "starstar" mixing function |
313 return Integer.rotateLeft(z * 5, 7) * 9; // "starstar" mixing function |
316 } |
314 } |
317 |
315 |
318 /** |
316 /** |
319 * Returns a pseudorandom {@code long} value. |
317 * Returns a pseudorandom {@code long} value. |
320 * |
318 * |
321 * @return a pseudorandom {@code long} value |
319 * @return a pseudorandom {@code long} value |
322 */ |
320 */ |
323 |
|
324 public long nextLong() { |
321 public long nextLong() { |
325 return ((long)(nextInt()) << 32) | nextInt(); |
322 return ((long)(nextInt()) << 32) | nextInt(); |
326 } |
323 } |
327 |
324 |
328 public BigInteger period() { return thePeriod; } |
325 public BigInteger period() { |
|
326 return PERIOD; |
|
327 } |
329 } |
328 } |