26 package java.util.random; |
26 package java.util.random; |
27 |
27 |
28 import java.math.BigInteger; |
28 import java.math.BigInteger; |
29 import java.util.concurrent.atomic.AtomicLong; |
29 import java.util.concurrent.atomic.AtomicLong; |
30 import java.util.random.RandomGenerator.SplittableGenerator; |
30 import java.util.random.RandomGenerator.SplittableGenerator; |
31 import java.util.random.RandomSupport.AbstractSplittableGenerator; |
31 import java.util.random.RandomSupport.AbstractSplittableWithBrineGenerator; |
32 |
32 |
33 /** |
33 /** |
34 * A generator of uniform pseudorandom values applicable for use in |
34 * A generator of uniform pseudorandom values applicable for use in |
35 * (among other contexts) isolated parallel computations that may |
35 * (among other contexts) isolated parallel computations that may |
36 * generate subtasks. Class {@link L64X128MixRandom} implements |
36 * generate subtasks. Class {@link L64X128MixRandom} implements |
52 * least approximately, for others as well. |
52 * least approximately, for others as well. |
53 * <p> |
53 * <p> |
54 * {@link L64X128MixRandom} is a specific member of the LXM family of algorithms |
54 * {@link L64X128MixRandom} is a specific member of the LXM family of algorithms |
55 * for pseudorandom number generators. Every LXM generator consists of two |
55 * for pseudorandom number generators. Every LXM generator consists of two |
56 * subgenerators; one is an LCG (Linear Congruential Generator) and the other is |
56 * subgenerators; one is an LCG (Linear Congruential Generator) and the other is |
57 * an Xorshift generator. Each output of an LXM generator is the sum of one |
57 * an Xorshift generator. Each output of an LXM generator is the result of |
58 * output from each subgenerator, possibly processed by a final mixing function |
58 * combining state from the LCG with state from the Xorshift generator by |
59 * (and {@link L64X128MixRandom} does use a mixing function). |
59 * using a Mixing function (and then the state of the LCG and the state of the |
|
60 * Xorshift generator are advanced). |
60 * <p> |
61 * <p> |
61 * The LCG subgenerator for {@link L64X128MixRandom} has an update step of the |
62 * The LCG subgenerator for {@link L64X128MixRandom} has an update step of the |
62 * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all |
63 * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all |
63 * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m} |
64 * of type {@code long}; {@code s} is the mutable state, the multiplier {@code m} |
64 * is fixed (the same for all instances of {@link L64X128MixRandom}}) and the addend |
65 * is fixed (the same for all instances of {@link L64X128MixRandom}}) and the addend |
71 * version 1.0 (parameters 24, 16, 37), without any final scrambler such as "+" or "**". |
72 * version 1.0 (parameters 24, 16, 37), without any final scrambler such as "+" or "**". |
72 * Its state consists of two {@code long} fields {@code x0} and {@code x1}, |
73 * Its state consists of two {@code long} fields {@code x0} and {@code x1}, |
73 * which can take on any values provided that they are not both zero. |
74 * which can take on any values provided that they are not both zero. |
74 * The period of this subgenerator is 2<sup>128</sup>-1. |
75 * The period of this subgenerator is 2<sup>128</sup>-1. |
75 * <p> |
76 * <p> |
76 * The mixing function for {@link L64X128MixRandom} is the 64-bit "starstar(5,7,9)" function. |
77 * The mixing function for {@link L64X128MixRandom} is {@link RandomSupport.mixLea64} |
|
78 * applied to the argument {@code (s + x0)}. |
77 * <p> |
79 * <p> |
78 * Because the periods 2<sup>64</sup> and 2<sup>128</sup>-1 of the two subgenerators |
80 * Because the periods 2<sup>64</sup> and 2<sup>128</sup>-1 of the two subgenerators |
79 * are relatively prime, the <em>period</em> of any single {@link L64X128MixRandom} object |
81 * are relatively prime, the <em>period</em> of any single {@link L64X128MixRandom} object |
80 * (the length of the series of generated 64-bit values before it repeats) is the product |
82 * (the length of the series of generated 64-bit values before it repeats) is the product |
81 * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>128</sup>-1), |
83 * of the periods of the subgenerators, that is, 2<sup>64</sup>(2<sup>128</sup>-1), |
96 * There are 2<sup>64</sup>(2<sup>128</sup>-1) such subsequences, and each subsequence, |
98 * There are 2<sup>64</sup>(2<sup>128</sup>-1) such subsequences, and each subsequence, |
97 * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values. Of those |
99 * which consists of 2 64-bit values, can have one of 2<sup>128</sup> values. Of those |
98 * 2<sup>128</sup> subsequence values, nearly all of them (2<sup>128</sup>-2<sup>64</sup>) |
100 * 2<sup>128</sup> subsequence values, nearly all of them (2<sup>128</sup>-2<sup>64</sup>) |
99 * occur 2<sup>64</sup> times over the course of the entire cycle, and the other |
101 * occur 2<sup>64</sup> times over the course of the entire cycle, and the other |
100 * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times. So the ratio |
102 * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times. So the ratio |
101 * of the probability of getting one of the less common subsequence values and the |
103 * of the probability of getting any specific one of the less common subsequence values and the |
102 * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>. |
104 * probability of getting any specific one of the more common subsequence values is 1-2<sup>-64</sup>. |
103 * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from |
105 * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from |
104 * one instance of {@link L64X128MixRandom} to another, as a function of the additive |
106 * one instance of {@link L64X128MixRandom} to another, as a function of the additive |
105 * parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()}, |
107 * parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()}, |
106 * and {@code nextDouble()} methods are likewise 2-equidistributed. |
108 * and {@code nextDouble()} methods are likewise 2-equidistributed. |
107 * <p> |
109 * <p> |
177 */ |
179 */ |
178 private static final BigInteger PERIOD = |
180 private static final BigInteger PERIOD = |
179 BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).shiftLeft(64); |
181 BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).shiftLeft(64); |
180 |
182 |
181 /* |
183 /* |
182 * Multiplier used in the LCG portion of the algorithm, taken from |
184 * Multiplier used in the LCG portion of the algorithm. |
183 * Pierre L'Ecuyer, Tables of linear congruential generators of |
185 * Chosen based on research by Sebastiano Vigna and Guy Steele (2019). |
184 * different sizes and good lattice structure, <em>Mathematics of |
186 * The spectral scores for dimensions 2 through 8 for the multiplier 0xd1342543de82ef95 |
185 * Computation</em> 68, 225 (January 1999), pages 249-260, |
187 * are [0.958602, 0.937479, 0.870757, 0.822326, 0.820405, 0.813065, 0.760215]. |
186 * Table 4 (first multiplier for size 2<sup>64</sup>). |
188 */ |
187 */ |
189 |
188 |
190 private static final long M = 0xd1342543de82ef95L; |
189 private static final long M = 2862933555777941757L; |
|
190 |
191 |
191 /* ---------------- instance fields ---------------- */ |
192 /* ---------------- instance fields ---------------- */ |
192 |
193 |
193 /** |
194 /** |
194 * The parameter that is used as an additive constant for the LCG. |
195 * The parameter that is used as an additive constant for the LCG. |
243 // |
245 // |
244 // The seed is hashed by mixMurmur64 to produce the `a` parameter. |
246 // The seed is hashed by mixMurmur64 to produce the `a` parameter. |
245 // The seed is hashed by mixStafford13 to produce the initial `x0`, |
247 // The seed is hashed by mixStafford13 to produce the initial `x0`, |
246 // which will then be used to produce the first generated value. |
248 // which will then be used to produce the first generated value. |
247 // Then x1 is filled in as if by a SplitMix PRNG with |
249 // Then x1 is filled in as if by a SplitMix PRNG with |
248 // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer. |
250 // GOLDEN_RATIO_64 as the gamma value and mixStafford13 as the mixer. |
249 this(RandomSupport.mixMurmur64(seed ^= RandomSupport.SILVER_RATIO_64), |
251 this(RandomSupport.mixMurmur64(seed ^= RandomSupport.SILVER_RATIO_64), |
250 1, |
252 1, |
251 RandomSupport.mixStafford13(seed), |
253 RandomSupport.mixStafford13(seed), |
252 RandomSupport.mixStafford13(seed + RandomSupport.GOLDEN_RATIO_64)); |
254 RandomSupport.mixStafford13(seed + RandomSupport.GOLDEN_RATIO_64)); |
253 } |
255 } |
282 } |
284 } |
283 |
285 |
284 /* ---------------- public methods ---------------- */ |
286 /* ---------------- public methods ---------------- */ |
285 |
287 |
286 /** |
288 /** |
287 * Constructs and returns a new instance of {@link L64X128MixRandom} |
289 * Given 63 bits of "brine", constructs and returns a new instance of |
288 * that shares no mutable state with this instance. |
290 * {@code L64X128MixRandom} that shares no mutable state with this instance. |
289 * However, with very high probability, the set of values collectively |
291 * However, with very high probability, the set of values collectively |
290 * generated by the two objects has the same statistical properties as if |
292 * generated by the two objects has the same statistical properties as if |
291 * same the quantity of values were generated by a single thread using |
293 * same the quantity of values were generated by a single thread using |
292 * a single {@link L64X128MixRandom} object. Either or both of the two |
294 * a single {@code L64X128MixRandom} object. Either or both of the two |
293 * objects may be further split using the {@code split} method, |
295 * objects may be further split using the {@code split} method, |
294 * and the same expected statistical properties apply to the |
296 * and the same expected statistical properties apply to the |
295 * entire set of generators constructed by such recursive splitting. |
297 * entire set of generators constructed by such recursive splitting. |
296 * |
298 * |
297 * @param source a {@link SplittableGenerator} instance to be used instead |
299 * @param source a {@code SplittableGenerator} instance to be used instead |
298 * of this one as a source of pseudorandom bits used to |
300 * of this one as a source of pseudorandom bits used to |
299 * initialize the state of the new ones. |
301 * initialize the state of the new ones. |
300 * |
302 * @param brine a long value, of which the low 63 bits are used to choose |
301 * @return a new instance of {@link L64X128MixRandom} |
303 * the {@code a} parameter for the new instance. |
302 */ |
304 * @return a new instance of {@code L64X128MixRandom} |
303 public L64X128MixRandom split(SplittableGenerator source) { |
305 */ |
304 // Literally pick a new instance "at random". |
306 public SplittableGenerator split(SplittableGenerator source, long brine) { |
305 return new L64X128MixRandom(source.nextLong(), source.nextLong(), |
307 // Pick a new instance "at random", but use the brine for `a`. |
306 source.nextLong(), source.nextLong()); |
308 return new L64X128MixRandom(brine << 1, source.nextLong(), |
|
309 source.nextLong(), source.nextLong()); |
307 } |
310 } |
308 |
311 |
309 /** |
312 /** |
310 * Returns a pseudorandom {@code long} value. |
313 * Returns a pseudorandom {@code long} value. |
311 * |
314 * |
312 * @return a pseudorandom {@code long} value |
315 * @return a pseudorandom {@code long} value |
313 */ |
316 */ |
314 public long nextLong() { |
317 public long nextLong() { |
315 final long z = s + x0; |
318 // Compute the result based on current state information |
316 s = M * s + a; // LCG |
319 // (this allows the computation to be overlapped with state update). |
|
320 final long result = RandomSupport.mixLea64(s + x0); |
|
321 // Update the LCG subgenerator |
|
322 s = M * s + a; |
|
323 // Update the Xorshift subgenerator |
317 long q0 = x0, q1 = x1; |
324 long q0 = x0, q1 = x1; |
318 { // xoroshiro128v1_0 |
325 { // xoroshiro128v1_0 |
319 q1 ^= q0; |
326 q1 ^= q0; |
320 q0 = Long.rotateLeft(q0, 24); |
327 q0 = Long.rotateLeft(q0, 24); |
321 q0 = q0 ^ q1 ^ (q1 << 16); |
328 q0 = q0 ^ q1 ^ (q1 << 16); |
322 q1 = Long.rotateLeft(q1, 37); |
329 q1 = Long.rotateLeft(q1, 37); |
323 } |
330 } |
324 x0 = q0; x1 = q1; |
331 x0 = q0; x1 = q1; |
325 return Long.rotateLeft(z * 5, 7) * 9; // "starstar" mixing function |
332 return result; |
326 } |
333 } |
327 |
334 |
|
335 /** |
|
336 * Returns the period of this random generator. |
|
337 * |
|
338 * @return a {@link BigInteger} whose value is the number of distinct possible states of this |
|
339 * {@link RandomGenerator} object (2<sup>64</sup>(2<sup>128</sup>-1)). |
|
340 */ |
328 public BigInteger period() { |
341 public BigInteger period() { |
329 return PERIOD; |
342 return PERIOD; |
330 } |
343 } |
331 } |
344 } |