author | jlaskey |
Thu, 08 Aug 2019 11:06:54 -0300 | |
branch | JDK-8193209-branch |
changeset 57684 | 7cb325557832 |
parent 57547 | 56cbdc3ea079 |
child 59080 | 1b314be4feb2 |
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; |
|
57388 | 32 |
|
33 |
/** |
|
34 |
* A generator of uniform pseudorandom values applicable for use in |
|
35 |
* (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
|
36 |
* generate subtasks. Class {@link L64X128MixRandom} implements |
57547 | 37 |
* interfaces {@link RandomGenerator} and {@link SplittableGenerator}, |
57388 | 38 |
* and therefore supports methods for producing pseudorandomly chosen |
39 |
* 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
|
40 |
* as well as creating new split-off {@link L64X128MixRandom} objects, |
57388 | 41 |
* 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
|
42 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
43 |
* Series of generated values pass the TestU01 BigCrush and PractRand test suites |
57388 | 44 |
* that measure independence and uniformity properties of random number generators. |
45 |
* (Most recently validated with |
|
46 |
* <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a> |
|
47 |
* and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>. |
|
48 |
* Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()} |
|
49 |
* method but also the result of bit-reversing each value produced by {@code nextLong()}.) |
|
50 |
* These tests validate only the methods for certain |
|
51 |
* types and ranges, but similar properties are expected to hold, at |
|
52 |
* 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
|
53 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
54 |
* {@link L64X128MixRandom} is a specific member of the LXM family of algorithms |
57388 | 55 |
* for pseudorandom number generators. Every LXM generator consists of two |
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 |
|
58 |
* 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
|
59 |
* (and {@link L64X128MixRandom} does use a mixing function). |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
60 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
61 |
* The LCG subgenerator for {@link L64X128MixRandom} has an update step of the |
57388 | 62 |
* 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} |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
64 |
* is fixed (the same for all instances of {@link L64X128MixRandom}}) and the addend |
57388 | 65 |
* {@code a} is a parameter (a final field of the instance). The parameter |
66 |
* {@code a} is required to be odd (this allows the LCG to have the maximal |
|
67 |
* period, namely 2<sup>64</sup>); therefore there are 2<sup>63</sup> distinct choices |
|
68 |
* of parameter. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
69 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
70 |
* The Xorshift subgenerator for {@link L64X128MixRandom} is the {@code xoroshiro128} algorithm, |
57388 | 71 |
* 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 |
* 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. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
75 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
76 |
* The mixing function for {@link L64X128MixRandom} is the 64-bit "starstar(5,7,9)" function. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
77 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
78 |
* Because the periods 2<sup>64</sup> and 2<sup>128</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
|
79 |
* are relatively prime, the <em>period</em> of any single {@link L64X128MixRandom} object |
57388 | 80 |
* (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), |
|
82 |
* which is just slightly smaller than 2<sup>192</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
|
83 |
* {@link L64X128MixRandom} objects have different {@code a} parameters, then their |
57388 | 84 |
* 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
|
85 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
86 |
* 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
|
87 |
* For any specific instance of {@link L64X128MixRandom}, over the course of its cycle each |
57388 | 88 |
* of the 2<sup>64</sup> possible {@code long} values will be produced 2<sup>128</sup>-1 times. |
89 |
* The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()} |
|
90 |
* 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
|
91 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
92 |
* In fact, the 64-bit values produced by the {@code nextLong()} method are 2-equidistributed. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
93 |
* To be precise: for any specific instance of {@link L64X128MixRandom}, consider |
57388 | 94 |
* the (overlapping) length-2 subsequences of the cycle of 64-bit values produced by |
95 |
* {@code nextLong()} (assuming no other methods are called that would affect the state). |
|
96 |
* 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 |
|
98 |
* 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 |
|
100 |
* 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 |
|
102 |
* probability of getting 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 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
104 |
* one instance of {@link L64X128MixRandom} to another, as a function of the additive |
57388 | 105 |
* parameter of the LCG.) The values produced by the {@code nextInt()}, {@code nextFloat()}, |
106 |
* and {@code nextDouble()} methods are likewise 2-equidistributed. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
107 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
108 |
* Method {@link #split} constructs and returns a new {@link L64X128MixRandom} |
57388 | 109 |
* instance that shares no mutable state with the current instance. However, with |
110 |
* very high probability, the values collectively generated by the two objects |
|
111 |
* 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
|
112 |
* generated by a single thread using a single {@link L64X128MixRandom} object. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
113 |
* This is because, with high probability, distinct {@link L64X128MixRandom} objects |
57388 | 114 |
* have distinct {@code a} parameters and therefore use distinct members of the |
115 |
* algorithmic family; and even if their {@code a} parameters are the same, with |
|
116 |
* very high probability they will traverse different parts of their common state |
|
117 |
* cycle. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
118 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
119 |
* 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
|
120 |
* {@link L64X128MixRandom} are <em>not</em> thread-safe. |
57388 | 121 |
* They are designed to be split, not shared, across threads. For |
122 |
* example, a {@link java.util.concurrent.ForkJoinTask} fork/join-style |
|
123 |
* computation using random numbers might include a construction |
|
124 |
* of the form {@code new Subtask(someL64X128MixRandom.split()).fork()}. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
125 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
126 |
* This class provides additional methods for generating random |
57388 | 127 |
* streams, that employ the above techniques when used in |
128 |
* {@code stream.parallel()} mode. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
129 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
130 |
* Instances of {@link L64X128MixRandom} are not cryptographically |
57388 | 131 |
* secure. Consider instead using {@link java.security.SecureRandom} |
132 |
* in security-sensitive applications. Additionally, |
|
133 |
* default-constructed instances do not use a cryptographically random |
|
134 |
* seed unless the {@linkplain System#getProperty system property} |
|
135 |
* {@code java.util.secureRandomSeed} is set to {@code true}. |
|
136 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
137 |
* @since 14 |
57388 | 138 |
*/ |
57547 | 139 |
public final class L64X128MixRandom extends AbstractSplittableGenerator { |
57388 | 140 |
|
141 |
/* |
|
142 |
* Implementation Overview. |
|
143 |
* |
|
144 |
* The split operation uses the current generator to choose four new 64-bit |
|
145 |
* long values that are then used to initialize the parameter `a` and the |
|
146 |
* state variables `s`, `x0`, and `x1` for a newly constructed generator. |
|
147 |
* |
|
148 |
* With extremely high probability, no two generators so chosen |
|
149 |
* 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
|
150 |
* that the values generated by two instances of {@link L64X128MixRandom} |
57388 | 151 |
* will be (approximately) independent if have different values for `a`. |
152 |
* |
|
153 |
* The default (no-argument) constructor, in essence, uses |
|
154 |
* "defaultGen" to generate four new 64-bit values for the same |
|
155 |
* purpose. Multiple generators created in this way will certainly |
|
156 |
* differ in their `a` parameters. The defaultGen state must be accessed |
|
157 |
* in a thread-safe manner, so we use an AtomicLong to represent |
|
158 |
* this state. To bootstrap the defaultGen, we start off using a |
|
159 |
* seed based on current time unless the |
|
160 |
* java.util.secureRandomSeed property is set. This serves as a |
|
161 |
* slimmed-down (and insecure) variant of SecureRandom that also |
|
162 |
* avoids stalls that may occur when using /dev/random. |
|
163 |
* |
|
164 |
* File organization: First static fields, then instance |
|
165 |
* fields, then constructors, then instance methods. |
|
166 |
*/ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
167 |
|
57388 | 168 |
/* ---------------- static fields ---------------- */ |
169 |
||
170 |
/** |
|
171 |
* The seed generator for default constructors. |
|
172 |
*/ |
|
57547 | 173 |
private static final AtomicLong defaultGen = new AtomicLong(RandomSupport.initialSeed()); |
57388 | 174 |
|
175 |
/* |
|
176 |
* The period of this generator, which is (2**128 - 1) * 2**64. |
|
177 |
*/ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
178 |
private static final BigInteger PERIOD = |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
179 |
BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).shiftLeft(64); |
57388 | 180 |
|
181 |
/* |
|
182 |
* Multiplier used in the LCG portion of the algorithm, taken from |
|
183 |
* Pierre L'Ecuyer, Tables of linear congruential generators of |
|
184 |
* different sizes and good lattice structure, <em>Mathematics of |
|
185 |
* Computation</em> 68, 225 (January 1999), pages 249-260, |
|
186 |
* Table 4 (first multiplier for size 2<sup>64</sup>). |
|
187 |
*/ |
|
188 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
189 |
private static final long M = 2862933555777941757L; |
57388 | 190 |
|
191 |
/* ---------------- instance fields ---------------- */ |
|
192 |
||
193 |
/** |
|
194 |
* The parameter that is used as an additive constant for the LCG. |
|
195 |
* Must be odd. |
|
196 |
*/ |
|
197 |
private final long a; |
|
198 |
||
199 |
/** |
|
200 |
* The per-instance state: s for the LCG; x0 and x1 for the xorshift. |
|
201 |
* At least one of x0 and x1 must be nonzero. |
|
202 |
*/ |
|
203 |
private long s, x0, x1; |
|
204 |
||
205 |
/* ---------------- constructors ---------------- */ |
|
206 |
||
207 |
/** |
|
208 |
* Basic constructor that initializes all fields from parameters. |
|
209 |
* It then adjusts the field values if necessary to ensure that |
|
210 |
* all constraints on the values of fields are met. |
|
211 |
* |
|
212 |
* @param a additive parameter for the LCG |
|
213 |
* @param s initial state for the LCG |
|
214 |
* @param x0 first word of the initial state for the xorshift generator |
|
215 |
* @param x1 second word of the initial state for the xorshift generator |
|
216 |
*/ |
|
217 |
public L64X128MixRandom(long a, long s, long x0, long x1) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
218 |
// Force a to be odd. |
57388 | 219 |
this.a = a | 1; |
220 |
this.s = s; |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
221 |
this.x0 = x0; |
57388 | 222 |
this.x1 = x1; |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
223 |
// If x0 and x1 are both zero, we must choose nonzero values. |
57388 | 224 |
if ((x0 | x1) == 0) { |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
225 |
// At least one of the two values generated here will be nonzero. |
57547 | 226 |
this.x0 = RandomSupport.mixStafford13(s += RandomSupport.GOLDEN_RATIO_64); |
227 |
this.x1 = RandomSupport.mixStafford13(s + RandomSupport.GOLDEN_RATIO_64); |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
228 |
} |
57388 | 229 |
} |
230 |
||
231 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
232 |
* Creates a new instance of {@link L64X128MixRandom} using the |
57388 | 233 |
* 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
|
234 |
* {@link L64X128MixRandom} created with the same seed in the same |
57388 | 235 |
* program generate identical sequences of values. |
236 |
* |
|
237 |
* @param seed the initial seed |
|
238 |
*/ |
|
239 |
public L64X128MixRandom(long seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
240 |
// 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
|
241 |
// 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
|
242 |
// 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
|
243 |
// |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
244 |
// 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
|
245 |
// 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
|
246 |
// 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
|
247 |
// Then x1 is 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
|
248 |
// GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer. |
57547 | 249 |
this(RandomSupport.mixMurmur64(seed ^= RandomSupport.SILVER_RATIO_64), |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
250 |
1, |
57547 | 251 |
RandomSupport.mixStafford13(seed), |
252 |
RandomSupport.mixStafford13(seed + RandomSupport.GOLDEN_RATIO_64)); |
|
57388 | 253 |
} |
254 |
||
255 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
256 |
* Creates a new instance of {@link L64X128MixRandom} that is likely to |
57388 | 257 |
* generate sequences of values that are statistically independent |
258 |
* of those of any other instances in the current program execution, |
|
259 |
* but may, and typically does, vary across program invocations. |
|
260 |
*/ |
|
261 |
public L64X128MixRandom() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
262 |
// Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values. |
57547 | 263 |
this(defaultGen.getAndAdd(RandomSupport.GOLDEN_RATIO_64)); |
57388 | 264 |
} |
265 |
||
266 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
267 |
* Creates a new instance of {@link L64X128MixRandom} using the specified array of |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
268 |
* initial seed bytes. Instances of {@link L64X128MixRandom} created with the same |
57388 | 269 |
* seed array in the same program execution generate identical sequences of values. |
270 |
* |
|
271 |
* @param seed the initial seed |
|
272 |
*/ |
|
273 |
public L64X128MixRandom(byte[] seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
274 |
// Convert the seed to 4 long values, of which the last 2 are not all zero. |
57547 | 275 |
long[] data = RandomSupport.convertSeedBytesToLongs(seed, 4, 2); |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
276 |
long a = data[0], s = data[1], x0 = data[2], x1 = data[3]; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
277 |
// Force a to be odd. |
57388 | 278 |
this.a = a | 1; |
279 |
this.s = s; |
|
280 |
this.x0 = x0; |
|
281 |
this.x1 = x1; |
|
282 |
} |
|
283 |
||
284 |
/* ---------------- public methods ---------------- */ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
285 |
|
57388 | 286 |
/** |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
287 |
* Constructs and returns a new instance of {@link L64X128MixRandom} |
57388 | 288 |
* that shares no mutable state with this instance. |
289 |
* However, with very high probability, the set of values collectively |
|
290 |
* 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 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
292 |
* a single {@link L64X128MixRandom} object. Either or both of the two |
57388 | 293 |
* objects may be further split using the {@code split} method, |
294 |
* and the same expected statistical properties apply to the |
|
295 |
* entire set of generators constructed by such recursive splitting. |
|
296 |
* |
|
57547 | 297 |
* @param source a {@link SplittableGenerator} instance to be used instead |
57388 | 298 |
* of this one as a source of pseudorandom bits used to |
299 |
* 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
|
300 |
* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
301 |
* @return a new instance of {@link L64X128MixRandom} |
57388 | 302 |
*/ |
57547 | 303 |
public L64X128MixRandom split(SplittableGenerator source) { |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
304 |
// Literally pick a new instance "at random". |
57388 | 305 |
return new L64X128MixRandom(source.nextLong(), source.nextLong(), |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
306 |
source.nextLong(), source.nextLong()); |
57388 | 307 |
} |
308 |
||
309 |
/** |
|
310 |
* Returns a pseudorandom {@code long} value. |
|
311 |
* |
|
312 |
* @return a pseudorandom {@code long} value |
|
313 |
*/ |
|
314 |
public long nextLong() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
315 |
final long z = s + x0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
316 |
s = M * s + a; // LCG |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
317 |
long q0 = x0, q1 = x1; |
57684 | 318 |
{ // xoroshiro128v1_0 |
319 |
q1 ^= q0; |
|
320 |
q0 = Long.rotateLeft(q0, 24); |
|
321 |
q0 = q0 ^ q1 ^ (q1 << 16); |
|
322 |
q1 = Long.rotateLeft(q1, 37); |
|
323 |
} |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
324 |
x0 = q0; x1 = q1; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
325 |
return Long.rotateLeft(z * 5, 7) * 9; // "starstar" mixing function |
57388 | 326 |
} |
327 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
328 |
public BigInteger period() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
329 |
return PERIOD; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
330 |
} |
57388 | 331 |
} |