author | jlaskey |
Fri, 26 Jul 2019 15:37:05 -0300 | |
branch | JDK-8193209-branch |
changeset 57547 | 56cbdc3ea079 |
parent 57437 | f02ffcb61dce |
child 57684 | 7cb325557832 |
permissions | -rw-r--r-- |
57388 | 1 |
/* |
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
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 |
|
23 |
* questions. |
|
24 |
*/ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
25 |
|
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
26 |
package java.util.random; |
57388 | 27 |
|
28 |
import java.math.BigInteger; |
|
29 |
import java.util.concurrent.atomic.AtomicLong; |
|
57547 | 30 |
import java.util.random.RandomGenerator.SplittableGenerator; |
31 |
import java.util.random.RandomSupport.AbstractSplittableGenerator; |
|
32 |
||
57388 | 33 |
|
34 |
/** |
|
35 |
* A generator of uniform pseudorandom values applicable for use in |
|
36 |
* (among other contexts) isolated parallel computations that may |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
37 |
* generate subtasks. Class {@link L128X256MixRandom} implements |
57547 | 38 |
* interfaces {@link RandomGenerator} and {@link SplittableGenerator}, |
57388 | 39 |
* and therefore supports methods for producing pseudorandomly chosen |
40 |
* numbers of type {@code int}, {@code long}, {@code float}, and {@code double} |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
41 |
* as well as creating new split-off {@link L128X256MixRandom} objects, |
57388 | 42 |
* with similar usages as for class {@link java.util.SplittableRandom}. |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
43 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
44 |
* Series of generated values pass the TestU01 BigCrush and PractRand test suites |
57388 | 45 |
* that measure independence and uniformity properties of random number generators. |
46 |
* (Most recently validated with |
|
47 |
* <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a> |
|
48 |
* and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>. |
|
49 |
* Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()} |
|
50 |
* method but also the result of bit-reversing each value produced by {@code nextLong()}.) |
|
51 |
* These tests validate only the methods for certain |
|
52 |
* types and ranges, but similar properties are expected to hold, at |
|
53 |
* least approximately, for others as well. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
54 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
55 |
* {@link L128X256MixRandom} is a specific member of the LXM family of algorithms |
57388 | 56 |
* for pseudorandom number generators. Every LXM generator consists of two |
57 |
* subgenerators; one is an LCG (Linear Congruential Generator) and the other is |
|
58 |
* an Xorshift generator. Each output of an LXM generator is the sum of one |
|
59 |
* output from each subgenerator, possibly processed by a final mixing function |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
60 |
* (and {@link L128X256MixRandom} does use a mixing function). |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
61 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
62 |
* The LCG subgenerator for {@link L128X256MixRandom} has an update step of the |
57388 | 63 |
* form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all |
64 |
* 128-bit integers; {@code s} is the mutable state, the multiplier {@code m} |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
65 |
* is fixed (the same for all instances of {@link L128X256MixRandom}}) and the addend |
57388 | 66 |
* {@code a} is a parameter (a final field of the instance). The parameter |
67 |
* {@code a} is required to be odd (this allows the LCG to have the maximal |
|
68 |
* period, namely 2<sup>128</sup>); therefore there are 2<sup>127</sup> distinct choices |
|
69 |
* of parameter. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
70 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
71 |
* The Xorshift subgenerator for {@link L128X256MixRandom} is the {@code xoshiro256} algorithm, |
57388 | 72 |
* version 1.0 (parameters 17, 45), without any final scrambler such as "+" or "**". |
73 |
* Its state consists of four {@code long} fields {@code x0}, {@code x1}, {@code x2}, |
|
74 |
* and {@code x3}, which can take on any values provided that they are not all zero. |
|
75 |
* The period of this subgenerator is 2<sup>256</sup>-1. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
76 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
77 |
* The mixing function for {@link L128X256MixRandom} is the 64-bit MurmurHash3 finalizer. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
78 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
79 |
* Because the periods 2<sup>128</sup> and 2<sup>256</sup>-1 of the two subgenerators |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
80 |
* are relatively prime, the <em>period</em> of any single {@link L128X256MixRandom} object |
57388 | 81 |
* (the length of the series of generated 64-bit values before it repeats) is the product |
82 |
* of the periods of the subgenerators, that is, 2<sup>128</sup>(2<sup>256</sup>-1), |
|
83 |
* which is just slightly smaller than 2<sup>384</sup>. Moreover, if two distinct |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
84 |
* {@link L128X256MixRandom} objects have different {@code a} parameters, then their |
57388 | 85 |
* cycles of produced values will be different. |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
86 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
87 |
* The 64-bit values produced by the {@code nextLong()} method are exactly equidistributed. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
88 |
* For any specific instance of {@link L128X256MixRandom}, over the course of its cycle each |
57388 | 89 |
* of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>256</sup>-1 times. |
90 |
* The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()} |
|
91 |
* methods are likewise exactly equidistributed. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
92 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
93 |
* In fact, the 64-bit values produced by the {@code nextLong()} method are exactly |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
94 |
* 2-equidistributed. For any specific instance of {@link L128X256MixRandom}, consider |
57388 | 95 |
* the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by |
96 |
* {@code nextLong()} (assuming no other methods are called that would affect the state). |
|
97 |
* There are 2<sup>128</sup>(2<sup>256</sup>-1) such subsequences, and each subsequence, |
|
98 |
* which consists of 2 64-bit values, can have one of 2<sup>128</sup> values, and each |
|
99 |
* such value occurs 2<sup>256</sup>-1 times. The values produced by the {@code nextInt()}, |
|
100 |
* {@code nextFloat()}, and {@code nextDouble()} methods are likewise exactly 2-equidistributed. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
101 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
102 |
* Moreover, the 64-bit values produced by the {@code nextLong()} method are 4-equidistributed. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
103 |
* To be precise: for any specific instance of {@link L128X256MixRandom}, consider |
57388 | 104 |
* the (overlapping) length-4 subsequences of the cycle of 64-bit values produced by |
105 |
* {@code nextLong()} (assuming no other methods are called that would affect the state). |
|
106 |
* There are <sup>128</sup>(2<sup>256</sup>-1) such subsequences, and each subsequence, |
|
107 |
* which consists of 4 64-bit values, can have one of 2<sup>256</sup> values. Of those |
|
108 |
* 2<sup>256</sup> subsequence values, nearly all of them (2<sup>256</sup>-2<sup>128</sup>) |
|
109 |
* occur 2<sup>128</sup> times over the course of the entire cycle, and the other |
|
110 |
* 2<sup>128</sup> subsequence values occur only 2<sup>128</sup>-1 times. So the ratio |
|
111 |
* of the probability of getting one of the less common subsequence values and the |
|
112 |
* probability of getting one of the more common subsequence values is 1-2<sup>-128</sup>. |
|
113 |
* (Note that the set of 2<sup>128</sup> less-common subsequence values will differ from |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
114 |
* one instance of {@link L128X256MixRandom} to another, as a function of the additive |
57388 | 115 |
* parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()}, |
116 |
* and {@code nextDouble()} methods are likewise 4-equidistributed. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
117 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
118 |
* Method {@link #split} constructs and returns a new {@link L128X256MixRandom} |
57388 | 119 |
* instance that shares no mutable state with the current instance. However, with |
120 |
* very high probability, the values collectively generated by the two objects |
|
121 |
* have the same statistical properties as if the same quantity of values were |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
122 |
* generated by a single thread using a single {@link L128X256MixRandom} object. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
123 |
* This is because, with high probability, distinct {@link L128X256MixRandom} objects |
57388 | 124 |
* have distinct {@code a} parameters and therefore use distinct members of the |
125 |
* algorithmic family; and even if their {@code a} parameters are the same, with |
|
126 |
* very high probability they will traverse different parts of their common state |
|
127 |
* cycle. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
128 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
129 |
* As with {@link java.util.SplittableRandom}, instances of |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
130 |
* {@link L128X256MixRandom} are <em>not</em> thread-safe. |
57388 | 131 |
* They are designed to be split, not shared, across threads. For |
132 |
* example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style |
|
133 |
* computation using random numbers might include a construction |
|
134 |
* of the form {@code new Subtask(someL128X256MixRandom.split()).fork()}. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
135 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
136 |
* This class provides additional methods for generating random |
57388 | 137 |
* streams, that employ the above techniques when used in |
138 |
* {@code stream.parallel()} mode. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
139 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
140 |
* Instances of {@link L128X256MixRandom} are not cryptographically |
57388 | 141 |
* secure. Consider instead using {@link java.security.SecureRandom} |
142 |
* in security-sensitive applications. Additionally, |
|
143 |
* default-constructed instances do not use a cryptographically random |
|
144 |
* seed unless the {@linkplain System#getProperty system property} |
|
145 |
* {@code java.util.secureRandomSeed} is set to {@code true}. |
|
146 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
147 |
* @since 14 |
57388 | 148 |
*/ |
57547 | 149 |
public final class L128X256MixRandom extends AbstractSplittableGenerator { |
57388 | 150 |
|
151 |
/* |
|
152 |
* Implementation Overview. |
|
153 |
* |
|
154 |
* The 128-bit parameter `a` is represented as two long fields `ah` and `al`. |
|
155 |
* The 128-bit state variable `s` is represented as two long fields `sh` and `sl`. |
|
156 |
* |
|
157 |
* The split operation uses the current generator to choose eight |
|
158 |
* new 64-bit long values that are then used to initialize the |
|
159 |
* parameters `ah` and `al` and the state variables `sh`, `sl`, |
|
160 |
* `x0`, `x1`, `x2`, and `x3` for a newly constructed generator. |
|
161 |
* |
|
162 |
* With extremely high probability, no two generators so chosen |
|
163 |
* will have the same `a` parameter, and testing has indicated |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
164 |
* that the values generated by two instances of {@link L128X256MixRandom} |
57388 | 165 |
* will be (approximately) independent if have different values for `a`. |
166 |
* |
|
167 |
* The default (no-argument) constructor, in essence, uses |
|
168 |
* "defaultGen" to generate eight new 64-bit values for the same |
|
169 |
* purpose. Multiple generators created in this way will certainly |
|
170 |
* differ in their `a` parameters. The defaultGen state must be accessed |
|
171 |
* in a thread-safe manner, so we use an AtomicLong to represent |
|
172 |
* this state. To bootstrap the defaultGen, we start off using a |
|
173 |
* seed based on current time unless the |
|
174 |
* java.util.secureRandomSeed property is set. This serves as a |
|
175 |
* slimmed-down (and insecure) variant of SecureRandom that also |
|
176 |
* avoids stalls that may occur when using /dev/random. |
|
177 |
* |
|
178 |
* File organization: First static fields, then instance |
|
179 |
* fields, then constructors, then instance methods. |
|
180 |
*/ |
|
181 |
||
182 |
/* ---------------- static fields ---------------- */ |
|
183 |
||
184 |
/** |
|
185 |
* The seed generator for default constructors. |
|
186 |
*/ |
|
57547 | 187 |
private static final AtomicLong defaultGen = new AtomicLong(RandomSupport.initialSeed()); |
57388 | 188 |
|
189 |
/* |
|
190 |
* The period of this generator, which is (2**256 - 1) * 2**128. |
|
191 |
*/ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
192 |
private static final BigInteger PERIOD = |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
193 |
BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE).shiftLeft(128); |
57388 | 194 |
|
195 |
/* |
|
196 |
* The multiplier used in the LCG portion of the algorithm is 2**64 + m; |
|
197 |
* where m is taken from |
|
198 |
* Pierre L'Ecuyer, Tables of linear congruential generators of |
|
199 |
* different sizes and good lattice structure, <em>Mathematics of |
|
200 |
* Computation</em> 68, 225 (January 1999), pages 249-260, |
|
201 |
* Table 4 (first multiplier for size 2<sup>64</sup>). |
|
202 |
* |
|
203 |
* This is almost certainly not the best possible 128-bit multiplier |
|
204 |
* for an LCG, but it is sufficient for our purposes here; because |
|
205 |
* is is larger than 2**64, the 64-bit values produced by nextLong() |
|
206 |
* are exactly 2-equidistributed, and the fact that it is of the |
|
207 |
* form (2**64 + m) simplifies the code, given that we have only |
|
208 |
* 64-bit arithmetic to work with. |
|
209 |
*/ |
|
210 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
211 |
private static final long M = 2862933555777941757L; |
57388 | 212 |
|
213 |
/* ---------------- instance fields ---------------- */ |
|
214 |
||
215 |
/** |
|
216 |
* The parameter that is used as an additive constant for the LCG. |
|
217 |
* Must be odd. |
|
218 |
*/ |
|
219 |
private final long ah, al; |
|
220 |
||
221 |
/** |
|
222 |
* The per-instance state: sh and sl for the LCG; x0, x1, x2, and x3 for the xorshift. |
|
223 |
* At least one of the four fields x0, x1, x2, and x3 must be nonzero. |
|
224 |
*/ |
|
225 |
private long sh, sl, x0, x1, x2, x3; |
|
226 |
||
227 |
/* ---------------- constructors ---------------- */ |
|
228 |
||
229 |
/** |
|
230 |
* Basic constructor that initializes all fields from parameters. |
|
231 |
* It then adjusts the field values if necessary to ensure that |
|
232 |
* all constraints on the values of fields are met. |
|
233 |
* |
|
234 |
* @param ah high half of the additive parameter for the LCG |
|
235 |
* @param al low half of the additive parameter for the LCG |
|
236 |
* @param sh high half of the initial state for the LCG |
|
237 |
* @param sl low half of the initial state for the LCG |
|
238 |
* @param x0 first word of the initial state for the xorshift generator |
|
239 |
* @param x1 second word of the initial state for the xorshift generator |
|
240 |
* @param x2 third word of the initial state for the xorshift generator |
|
241 |
* @param x3 fourth word of the initial state for the xorshift generator |
|
242 |
*/ |
|
243 |
public L128X256MixRandom(long ah, long al, long sh, long sl, long x0, long x1, long x2, long x3) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
244 |
// Force a to be odd. |
57388 | 245 |
this.ah = ah; |
246 |
this.al = al | 1; |
|
247 |
this.sh = sh; |
|
248 |
this.sl = sl; |
|
249 |
this.x0 = x0; |
|
250 |
this.x1 = x1; |
|
251 |
this.x2 = x2; |
|
252 |
this.x3 = x3; |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
253 |
// If x0, x1, x2, and x3 are all zero, we must choose nonzero values. |
57388 | 254 |
if ((x0 | x1 | x2 | x3) == 0) { |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
255 |
// At least three of the four values generated here will be nonzero. |
57547 | 256 |
this.x0 = RandomSupport.mixStafford13(sh += RandomSupport.GOLDEN_RATIO_64); |
257 |
this.x1 = RandomSupport.mixStafford13(sh += RandomSupport.GOLDEN_RATIO_64); |
|
258 |
this.x2 = RandomSupport.mixStafford13(sh += RandomSupport.GOLDEN_RATIO_64); |
|
259 |
this.x3 = RandomSupport.mixStafford13(sh + RandomSupport.GOLDEN_RATIO_64); |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
260 |
} |
57388 | 261 |
} |
262 |
||
263 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
264 |
* Creates a new instance of {@link L128X256MixRandom} using the |
57388 | 265 |
* specified {@code long} value as the initial seed. Instances of |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
266 |
* {@link L128X256MixRandom} created with the same seed in the same |
57388 | 267 |
* program generate identical sequences of values. |
268 |
* |
|
269 |
* @param seed the initial seed |
|
270 |
*/ |
|
271 |
public L128X256MixRandom(long seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
272 |
// Using a value with irregularly spaced 1-bits to xor the seed |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
273 |
// argument tends to improve "pedestrian" seeds such as 0 or |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
274 |
// other small integers. We may as well use SILVER_RATIO_64. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
275 |
// |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
276 |
// The seed is hashed by mixMurmur64 to produce the `a` parameter. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
277 |
// The seed is hashed by mixStafford13 to produce the initial `x0`, |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
278 |
// which will then be used to produce the first generated value. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
279 |
// The other x values are filled in as if by a SplitMix PRNG with |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
280 |
// GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer. |
57547 | 281 |
this(RandomSupport.mixMurmur64(seed ^= RandomSupport.SILVER_RATIO_64), |
282 |
RandomSupport.mixMurmur64(seed += RandomSupport.GOLDEN_RATIO_64), |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
283 |
0, |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
284 |
1, |
57547 | 285 |
RandomSupport.mixStafford13(seed), |
286 |
RandomSupport.mixStafford13(seed += RandomSupport.GOLDEN_RATIO_64), |
|
287 |
RandomSupport.mixStafford13(seed += RandomSupport.GOLDEN_RATIO_64), |
|
288 |
RandomSupport.mixStafford13(seed + RandomSupport.GOLDEN_RATIO_64)); |
|
57388 | 289 |
} |
290 |
||
291 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
292 |
* Creates a new instance of {@link L128X256MixRandom} that is likely to |
57388 | 293 |
* generate sequences of values that are statistically independent |
294 |
* of those of any other instances in the current program execution, |
|
295 |
* but may, and typically does, vary across program invocations. |
|
296 |
*/ |
|
297 |
public L128X256MixRandom() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
298 |
// Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values. |
57547 | 299 |
this(defaultGen.getAndAdd(RandomSupport.GOLDEN_RATIO_64)); |
57388 | 300 |
} |
301 |
||
302 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
303 |
* Creates a new instance of {@link L128X256MixRandom} using the specified array of |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
304 |
* initial seed bytes. Instances of {@link L128X256MixRandom} created with the same |
57388 | 305 |
* seed array in the same program execution generate identical sequences of values. |
306 |
* |
|
307 |
* @param seed the initial seed |
|
308 |
*/ |
|
309 |
public L128X256MixRandom(byte[] seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
310 |
// Convert the seed to 6 long values, of which the last 4 are not all zero. |
57547 | 311 |
long[] data = RandomSupport.convertSeedBytesToLongs(seed, 6, 4); |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
312 |
long ah = data[0], al = data[1], sh = data[2], sl = data[3], x0 = data[4], x1 = data[5], x2 = data[6], x3 = data[7]; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
313 |
// Force a to be odd. |
57388 | 314 |
this.ah = ah; |
315 |
this.al = al | 1; |
|
316 |
this.sh = sh; |
|
317 |
this.sl = sl; |
|
318 |
this.x0 = x0; |
|
319 |
this.x1 = x1; |
|
320 |
this.x2 = x2; |
|
321 |
this.x3 = x3; |
|
322 |
} |
|
323 |
||
324 |
/* ---------------- public methods ---------------- */ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
325 |
|
57388 | 326 |
/** |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
327 |
* Constructs and returns a new instance of {@link L128X256MixRandom} |
57388 | 328 |
* that shares no mutable state with this instance. |
329 |
* However, with very high probability, the set of values collectively |
|
330 |
* generated by the two objects has the same statistical properties as if |
|
331 |
* same the quantity of values were generated by a single thread using |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
332 |
* a single {@link L128X256MixRandom} object. Either or both of the two |
57388 | 333 |
* objects may be further split using the {@code split} method, |
334 |
* and the same expected statistical properties apply to the |
|
335 |
* entire set of generators constructed by such recursive splitting. |
|
336 |
* |
|
57547 | 337 |
* @param source a {@link SplittableGenerator} instance to be used instead |
57388 | 338 |
* of this one as a source of pseudorandom bits used to |
339 |
* initialize the state of the new ones. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
340 |
* @return a new instance of {@link L128X256MixRandom} |
57388 | 341 |
*/ |
57547 | 342 |
public L128X256MixRandom split(SplittableGenerator source) { |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
343 |
// Literally pick a new instance "at random". |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
344 |
return new L128X256MixRandom(source.nextLong(), source.nextLong(), |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
345 |
source.nextLong(), source.nextLong(), |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
346 |
source.nextLong(), source.nextLong(), |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
347 |
source.nextLong(), source.nextLong()); |
57388 | 348 |
} |
349 |
||
350 |
/** |
|
351 |
* Returns a pseudorandom {@code long} value. |
|
352 |
* |
|
353 |
* @return a pseudorandom {@code long} value |
|
354 |
*/ |
|
355 |
public long nextLong() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
356 |
final long z = sh + x0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
357 |
// The LCG: in effect, s = ((1LL << 64) + M) * s + a, if only we had 128-bit arithmetic. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
358 |
final long u = M * sl; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
359 |
sh = (M * sh) + Math.multiplyHigh(M, sl) + sl + ah; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
360 |
sl = u + al; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
361 |
if (Long.compareUnsigned(sl, u) < 0) ++sh; // Handle the carry propagation from low half to high half. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
362 |
long q0 = x0, q1 = x1, q2 = x2, q3 = x3; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
363 |
{ long t = q1 << 17; q2 ^= q0; q3 ^= q1; q1 ^= q2; q0 ^= q3; q2 ^= t; q3 = Long.rotateLeft(q3, 45); } // xoshiro256 1.0 |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
364 |
x0 = q0; x1 = q1; x2 = q2; x3 = q3; |
57547 | 365 |
return RandomSupport.mixLea64(z); // mixing function |
57388 | 366 |
} |
367 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
368 |
public BigInteger period() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
369 |
return PERIOD; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
370 |
} |
57388 | 371 |
} |