author | jlaskey |
Thu, 27 Jun 2019 18:30:27 -0300 | |
branch | JDK-8193209-branch |
changeset 57437 | f02ffcb61dce |
parent 57436 | b0c958c0e6c6 |
child 57547 | 56cbdc3ea079 |
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; |
|
30 |
||
31 |
/** |
|
32 |
* A generator of uniform pseudorandom values applicable for use in |
|
33 |
* (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
|
34 |
* generate subtasks. Class {@link Xoshiro256StarStar} implements |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
35 |
* interfaces {@link RandomNumberGenerator} and {@link LeapableRNG}, |
57388 | 36 |
* and therefore supports methods for producing pseudorandomly chosen |
37 |
* 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
|
38 |
* as well as creating new {@link Xoshiro256StarStar} objects |
57388 | 39 |
* by "jumping" or "leaping". |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
40 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
41 |
* Series of generated values pass the TestU01 BigCrush and PractRand test suites |
57388 | 42 |
* that measure independence and uniformity properties of random number generators. |
43 |
* (Most recently validated with |
|
44 |
* <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">version 1.2.3 of TestU01</a> |
|
45 |
* and <a href="http://pracrand.sourceforge.net">version 0.90 of PractRand</a>. |
|
46 |
* Note that TestU01 BigCrush was used to test not only values produced by the {@code nextLong()} |
|
47 |
* method but also the result of bit-reversing each value produced by {@code nextLong()}.) |
|
48 |
* These tests validate only the methods for certain |
|
49 |
* types and ranges, but similar properties are expected to hold, at |
|
50 |
* 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
|
51 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
52 |
* The class {@link Xoshiro256StarStar} uses the {@code xoshiro256} algorithm, |
57388 | 53 |
* version 1.0 (parameters 17, 45), with the "**" scrambler (a mixing function). |
54 |
* Its state consists of four {@code long} fields {@code x0}, {@code x1}, {@code x2}, |
|
55 |
* and {@code x3}, which can take on any values provided that they are not all zero. |
|
56 |
* The period of this generator 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
|
57 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
58 |
* The 64-bit values produced by the {@code nextLong()} method are equidistributed. |
57388 | 59 |
* To be precise, over the course of the cycle of length 2<sup>256</sup>-1, |
60 |
* each nonzero {@code long} value is generated 2<sup>192</sup> times, |
|
61 |
* but the value 0 is generated only 2<sup>192</sup>-1 times. |
|
62 |
* The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()} |
|
63 |
* methods are likewise equidistributed. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
64 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
65 |
* In fact, the 64-bit values produced by the {@code nextLong()} method are 4-equidistributed. |
57388 | 66 |
* To be precise: consider the (overlapping) length-4 subsequences of the cycle of 64-bit |
67 |
* values produced by {@code nextLong()} (assuming no other methods are called that would |
|
68 |
* affect the state). There are 2<sup>256</sup>-1 such subsequences, and each subsequence, |
|
69 |
* which consists of 4 64-bit values, can have one of 2<sup>256</sup> values. Of those |
|
70 |
* 2<sup>256</sup> subsequence values, each one is generated exactly once over the course |
|
71 |
* of the entire cycle, except that the subsequence (0, 0, 0, 0) never appears. |
|
72 |
* The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()} |
|
73 |
* methods are likewise 4-equidistributed, but note that that the subsequence (0, 0, 0, 0) |
|
74 |
* can also appear (but occurring somewhat less frequently than all other subsequences), |
|
75 |
* because the values produced by those methods have fewer than 64 randomly chosen bits. |
|
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 |
* Instances {@link Xoshiro256StarStar} are <em>not</em> thread-safe. |
57388 | 78 |
* They are designed to be used so that each thread as its own instance. |
79 |
* The methods {@link #jump} and {@link #leap} and {@link #jumps} and {@link #leaps} |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
80 |
* can be used to construct new instances of {@link Xoshiro256StarStar} that traverse |
57388 | 81 |
* other parts of the state cycle. |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
82 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
83 |
* Instances of {@link Xoshiro256StarStar} are not cryptographically |
57388 | 84 |
* secure. Consider instead using {@link java.security.SecureRandom} |
85 |
* in security-sensitive applications. Additionally, |
|
86 |
* default-constructed instances do not use a cryptographically random |
|
87 |
* seed unless the {@linkplain System#getProperty system property} |
|
88 |
* {@code java.util.secureRandomSeed} is set to {@code true}. |
|
89 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
90 |
* @since 14 |
57388 | 91 |
*/ |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
92 |
public final class Xoshiro256StarStar implements LeapableRNG { |
57388 | 93 |
|
94 |
/* |
|
95 |
* Implementation Overview. |
|
96 |
* |
|
97 |
* This is an implementation of the xoroshiro128** algorithm written |
|
98 |
* in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org). |
|
99 |
* See http://xoshiro.di.unimi.it and these two papers: |
|
100 |
* |
|
101 |
* Sebastiano Vigna. 2016. An Experimental Exploration of Marsaglia's |
|
102 |
* xorshift Generators, Scrambled. ACM Transactions on Mathematical |
|
103 |
* Software 42, 4, Article 30 (June 2016), 23 pages. |
|
104 |
* https://doi.org/10.1145/2845077 |
|
105 |
* |
|
106 |
* David Blackman and Sebastiano Vigna. 2018. Scrambled Linear |
|
107 |
* Pseudorandom Number Generators. Computing Research Repository (CoRR). |
|
108 |
* http://arxiv.org/abs/1805.01407 |
|
109 |
* |
|
110 |
* The jump operation moves the current generator forward by 2*128 |
|
111 |
* steps; this has the same effect as calling nextLong() 2**128 |
|
112 |
* times, but is much faster. Similarly, the leap operation moves |
|
113 |
* the current generator forward by 2*192 steps; this has the same |
|
114 |
* effect as calling nextLong() 2**192 times, but is much faster. |
|
115 |
* The copy method may be used to make a copy of the current |
|
116 |
* generator. Thus one may repeatedly and cumulatively copy and |
|
117 |
* jump to produce a sequence of generators whose states are well |
|
118 |
* spaced apart along the overall state cycle (indeed, the jumps() |
|
119 |
* and leaps() methods each produce a stream of such generators). |
|
120 |
* The generators can then be parceled out to other threads. |
|
121 |
* |
|
122 |
* File organization: First static fields, then instance |
|
123 |
* fields, then constructors, then instance methods. |
|
124 |
*/ |
|
125 |
||
126 |
/* ---------------- static fields ---------------- */ |
|
127 |
||
128 |
/** |
|
129 |
* The seed generator for default constructors. |
|
130 |
*/ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
131 |
private static final AtomicLong DEFAULT_GEN = new AtomicLong(RNGSupport.initialSeed()); |
57388 | 132 |
|
133 |
/* |
|
134 |
* The period of this generator, which is 2**256 - 1. |
|
135 |
*/ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
136 |
private static final BigInteger PERIOD = |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
137 |
BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE); |
57388 | 138 |
|
139 |
/* ---------------- instance fields ---------------- */ |
|
140 |
||
141 |
/** |
|
142 |
* The per-instance state. |
|
143 |
* At least one of the four fields x0, x1, x2, and x3 must be nonzero. |
|
144 |
*/ |
|
145 |
private long x0, x1, x2, x3; |
|
146 |
||
147 |
/* ---------------- constructors ---------------- */ |
|
148 |
||
149 |
/** |
|
150 |
* Basic constructor that initializes all fields from parameters. |
|
151 |
* It then adjusts the field values if necessary to ensure that |
|
152 |
* all constraints on the values of fields are met. |
|
153 |
* |
|
154 |
* @param x0 first word of the initial state |
|
155 |
* @param x1 second word of the initial state |
|
156 |
* @param x2 third word of the initial state |
|
157 |
* @param x3 fourth word of the initial state |
|
158 |
*/ |
|
159 |
public Xoshiro256StarStar(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
|
160 |
this.x0 = x0; |
57388 | 161 |
this.x1 = x1; |
162 |
this.x2 = x2; |
|
163 |
this.x3 = x3; |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
164 |
// If x0, x1, x2, and x3 are all zero, we must choose nonzero values. |
57388 | 165 |
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
|
166 |
// At least three of the four values generated here will be nonzero. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
167 |
this.x0 = RNGSupport.mixStafford13(x0 += RNGSupport.GOLDEN_RATIO_64); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
168 |
this.x1 = (x0 += RNGSupport.GOLDEN_RATIO_64); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
169 |
this.x2 = (x0 += RNGSupport.GOLDEN_RATIO_64); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
170 |
this.x3 = (x0 += RNGSupport.GOLDEN_RATIO_64); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
171 |
} |
57388 | 172 |
} |
173 |
||
174 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
175 |
* Creates a new instance of {@link Xoshiro256StarStar} using the |
57388 | 176 |
* 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
|
177 |
* {@link Xoshiro256StarStar} created with the same seed in the same |
57388 | 178 |
* program generate identical sequences of values. |
179 |
* |
|
180 |
* @param seed the initial seed |
|
181 |
*/ |
|
182 |
public Xoshiro256StarStar(long seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
183 |
// 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
|
184 |
// 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
|
185 |
// 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
|
186 |
// |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
187 |
// The x values are then 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
|
188 |
// GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
189 |
this(RNGSupport.mixStafford13(seed ^= RNGSupport.SILVER_RATIO_64), |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
190 |
RNGSupport.mixStafford13(seed += RNGSupport.GOLDEN_RATIO_64), |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
191 |
RNGSupport.mixStafford13(seed += RNGSupport.GOLDEN_RATIO_64), |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
192 |
RNGSupport.mixStafford13(seed + RNGSupport.GOLDEN_RATIO_64)); |
57388 | 193 |
} |
194 |
||
195 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
196 |
* Creates a new instance of {@link Xoshiro256StarStar} that is likely to |
57388 | 197 |
* generate sequences of values that are statistically independent |
198 |
* of those of any other instances in the current program execution, |
|
199 |
* but may, and typically does, vary across program invocations. |
|
200 |
*/ |
|
201 |
public Xoshiro256StarStar() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
202 |
// Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
203 |
this(DEFAULT_GEN.getAndAdd(RNGSupport.GOLDEN_RATIO_64)); |
57388 | 204 |
} |
205 |
||
206 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
207 |
* Creates a new instance of {@link Xoshiro256StarStar} using the specified array of |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
208 |
* initial seed bytes. Instances of {@link Xoshiro256StarStar} created with the same |
57388 | 209 |
* seed array in the same program execution generate identical sequences of values. |
210 |
* |
|
211 |
* @param seed the initial seed |
|
212 |
*/ |
|
213 |
public Xoshiro256StarStar(byte[] seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
214 |
// Convert the seed to 4 long values, which are not all zero. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
215 |
long[] data = RNGSupport.convertSeedBytesToLongs(seed, 4, 4); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
216 |
long x0 = data[0], x1 = data[1], x2 = data[2], x3 = data[3]; |
57388 | 217 |
this.x0 = x0; |
218 |
this.x1 = x1; |
|
219 |
this.x2 = x2; |
|
220 |
this.x3 = x3; |
|
221 |
} |
|
222 |
||
223 |
/* ---------------- public methods ---------------- */ |
|
224 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
225 |
public Xoshiro256StarStar copy() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
226 |
return new Xoshiro256StarStar(x0, x1, x2, x3); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
227 |
} |
57388 | 228 |
|
229 |
/** |
|
230 |
* Returns a pseudorandom {@code long} value. |
|
231 |
* |
|
232 |
* @return a pseudorandom {@code long} value |
|
233 |
*/ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
234 |
public long nextLong() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
235 |
final long z = x0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
236 |
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
|
237 |
{ 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
|
238 |
x0 = q0; x1 = q1; x2 = q2; x3 = q3; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
239 |
return Long.rotateLeft(z * 5, 7) * 9; // "starstar" mixing function |
57388 | 240 |
} |
241 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
242 |
public BigInteger period() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
243 |
return PERIOD; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
244 |
} |
57388 | 245 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
246 |
public double defaultJumpDistance() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
247 |
return 0x1.0p64; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
248 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
249 |
|
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
250 |
public double defaultLeapDistance() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
251 |
return 0x1.0p96; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
252 |
} |
57388 | 253 |
|
254 |
private static final long[] JUMP_TABLE = { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
255 |
0x180ec6d33cfd0abaL, 0xd5a61266f0c9392cL, 0xa9582618e03fc9aaL, 0x39abdc4529b1661cL }; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
256 |
|
57388 | 257 |
private static final long[] LEAP_TABLE = { |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
258 |
0x76e15d3efefdcbbfL, 0xc5004e441c522fb3L, 0x77710069854ee241L, 0x39109bb02acbe635L }; |
57388 | 259 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
260 |
/** |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
261 |
* This is the jump function for the generator. It is equivalent to 2**128 calls to next(); it |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
262 |
* can be used to generate 2**128 non-overlapping subsequences for parallel computations. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
263 |
*/ |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
264 |
public void jump() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
265 |
jumpAlgorithm(JUMP_TABLE); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
266 |
} |
57388 | 267 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
268 |
/** |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
269 |
* This is the long-jump function for the generator. It is equivalent to 2**192 calls to next(); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
270 |
* it can be used to generate 2**64 starting points, from each of which jump() will generate |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
271 |
* 2**64 non-overlapping subsequences for parallel distributed computations. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
272 |
*/ |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
273 |
public void leap() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
274 |
jumpAlgorithm(LEAP_TABLE); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
275 |
} |
57388 | 276 |
|
277 |
private void jumpAlgorithm(long[] table) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
278 |
long s0 = 0, s1 = 0, s2 = 0, s3 = 0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
279 |
for (int i = 0; i < table.length; i++) { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
280 |
for (int b = 0; b < 64; b++) { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
281 |
if ((table[i] & (1L << b)) != 0) { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
282 |
s0 ^= x0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
283 |
s1 ^= x1; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
284 |
s2 ^= x2; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
285 |
s3 ^= x3; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
286 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
287 |
nextLong(); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
288 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
289 |
x0 = s0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
290 |
x1 = s1; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
291 |
x2 = s2; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
292 |
x3 = s3; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
293 |
} |
57388 | 294 |
} |
295 |
||
296 |
} |