author | jlaskey |
Thu, 14 Nov 2019 08:54:56 -0400 | |
branch | JDK-8193209-branch |
changeset 59080 | 1b314be4feb2 |
parent 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; |
|
57547 | 30 |
import java.util.random.RandomGenerator.LeapableGenerator; |
57388 | 31 |
|
32 |
/** |
|
33 |
* A generator of uniform pseudorandom values applicable for use in |
|
34 |
* (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
|
35 |
* generate subtasks. Class {@link Xoroshiro128StarStar} implements |
57547 | 36 |
* interfaces {@link RandomGenerator} and {@link LeapableGenerator}, |
57388 | 37 |
* and therefore supports methods for producing pseudorandomly chosen |
38 |
* 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
|
39 |
* as well as creating new {@link Xoroshiro128StarStar} objects |
57388 | 40 |
* by "jumping" or "leaping". |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
41 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
42 |
* Series of generated values pass the TestU01 BigCrush and PractRand test suites |
57388 | 43 |
* that measure independence and uniformity properties of random number generators. |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
44 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
45 |
* The class {@link Xoroshiro128StarStar} uses the {@code xoroshiro128} algorithm, |
57388 | 46 |
* version 1.0 (parameters 24, 16, 37), with the "**" scrambler (a mixing function). |
47 |
* Its state consists of two {@code long} fields {@code x0} and {@code x1}, |
|
48 |
* which can take on any values provided that they are not both zero. |
|
49 |
* The period of this generator 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
|
50 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
51 |
* The 64-bit values produced by the {@code nextLong()} method are equidistributed. |
57388 | 52 |
* To be precise, over the course of the cycle of length 2<sup>128</sup>-1, |
53 |
* each nonzero {@code long} value is generated 2<sup>64</sup> times, |
|
54 |
* but the value 0 is generated only 2<sup>64</sup>-1 times. |
|
55 |
* The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()} |
|
56 |
* methods are likewise equidistributed. |
|
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 |
* In fact, the 64-bit values produced by the {@code nextLong()} method are 2-equidistributed. |
57388 | 59 |
* To be precise: consider the (overlapping) length-2 subsequences of the cycle of 64-bit |
60 |
* values produced by {@code nextLong()} (assuming no other methods are called that would |
|
61 |
* affect the state). There are 2<sup>128</sup>-1 such subsequences, and each subsequence, |
|
62 |
* which consists of 2 64-bit values, can have one of 2<sup>128</sup> values. Of those |
|
63 |
* 2<sup>128</sup> subsequence values, each one is generated exactly once over the course |
|
64 |
* of the entire cycle, except that the subsequence (0, 0) never appears. |
|
65 |
* The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()} |
|
66 |
* methods are likewise 2-equidistributed, but note that that the subsequence (0, 0) |
|
67 |
* can also appear (but occurring somewhat less frequently than all other subsequences), |
|
68 |
* 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
|
69 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
70 |
* Instances {@link Xoroshiro128StarStar} are <em>not</em> thread-safe. |
57388 | 71 |
* They are designed to be used so that each thread as its own instance. |
72 |
* 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
|
73 |
* can be used to construct new instances of {@link Xoroshiro128StarStar} that traverse |
57388 | 74 |
* 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
|
75 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
76 |
* Instances of {@link Xoroshiro128StarStar} are not cryptographically |
57388 | 77 |
* secure. Consider instead using {@link java.security.SecureRandom} |
78 |
* in security-sensitive applications. Additionally, |
|
79 |
* default-constructed instances do not use a cryptographically random |
|
80 |
* seed unless the {@linkplain System#getProperty system property} |
|
81 |
* {@code java.util.secureRandomSeed} is set to {@code true}. |
|
82 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
83 |
* @since 14 |
57388 | 84 |
*/ |
57547 | 85 |
public final class Xoroshiro128StarStar implements LeapableGenerator { |
57388 | 86 |
|
87 |
/* |
|
88 |
* Implementation Overview. |
|
89 |
* |
|
90 |
* This is an implementation of the xoroshiro128** algorithm written |
|
91 |
* in 2016 by David Blackman and Sebastiano Vigna (vigna@acm.org), |
|
92 |
* and updated with improved parameters in 2018. |
|
93 |
* See http://xoshiro.di.unimi.it and these two papers: |
|
94 |
* |
|
95 |
* Sebastiano Vigna. 2016. An Experimental Exploration of Marsaglia's |
|
96 |
* xorshift Generators, Scrambled. ACM Transactions on Mathematical |
|
97 |
* Software 42, 4, Article 30 (June 2016), 23 pages. |
|
98 |
* https://doi.org/10.1145/2845077 |
|
99 |
* |
|
100 |
* David Blackman and Sebastiano Vigna. 2018. Scrambled Linear |
|
101 |
* Pseudorandom Number Generators. Computing Research Repository (CoRR). |
|
102 |
* http://arxiv.org/abs/1805.01407 |
|
103 |
* |
|
104 |
* The jump operation moves the current generator forward by 2*64 |
|
105 |
* steps; this has the same effect as calling nextLong() 2**64 |
|
106 |
* times, but is much faster. Similarly, the leap operation moves |
|
107 |
* the current generator forward by 2*96 steps; this has the same |
|
108 |
* effect as calling nextLong() 2**96 times, but is much faster. |
|
109 |
* The copy method may be used to make a copy of the current |
|
110 |
* generator. Thus one may repeatedly and cumulatively copy and |
|
111 |
* jump to produce a sequence of generators whose states are well |
|
112 |
* spaced apart along the overall state cycle (indeed, the jumps() |
|
113 |
* and leaps() methods each produce a stream of such generators). |
|
114 |
* The generators can then be parceled out to other threads. |
|
115 |
* |
|
116 |
* File organization: First the non-public methods that constitute the |
|
117 |
* main algorithm, then the public methods. Note that many methods are |
|
57547 | 118 |
* defined by classes {@link AbstractJumpableGenerator} and {@link AbstractGenerator}. |
57388 | 119 |
*/ |
120 |
||
121 |
/* ---------------- static fields ---------------- */ |
|
122 |
||
123 |
/** |
|
124 |
* The seed generator for default constructors. |
|
125 |
*/ |
|
57547 | 126 |
private static final AtomicLong DEFAULT_GEN = new AtomicLong(RandomSupport.initialSeed()); |
57388 | 127 |
|
128 |
/* |
|
129 |
* The period of this generator, which is 2**128 - 1. |
|
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 BigInteger PERIOD = |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
132 |
BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE); |
57388 | 133 |
|
134 |
/* ---------------- instance fields ---------------- */ |
|
135 |
||
136 |
/** |
|
137 |
* The per-instance state. |
|
138 |
* At least one of the two fields x0 and x1 must be nonzero. |
|
139 |
*/ |
|
140 |
private long x0, x1; |
|
141 |
||
142 |
/* ---------------- constructors ---------------- */ |
|
143 |
||
144 |
/** |
|
145 |
* Basic constructor that initializes all fields from parameters. |
|
146 |
* It then adjusts the field values if necessary to ensure that |
|
147 |
* all constraints on the values of fields are met. |
|
148 |
* |
|
149 |
* @param x0 first word of the initial state |
|
150 |
* @param x1 second word of the initial state |
|
151 |
*/ |
|
152 |
public Xoroshiro128StarStar(long x0, long x1) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
153 |
this.x0 = x0; |
57388 | 154 |
this.x1 = x1; |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
155 |
// If x0 and x1 are both zero, we must choose nonzero values. |
57388 | 156 |
if ((x0 | x1) == 0) { |
59080 | 157 |
this.x0 = RandomSupport.GOLDEN_RATIO_64; |
158 |
this.x1 = RandomSupport.SILVER_RATIO_64; |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
159 |
} |
57388 | 160 |
} |
161 |
||
162 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
163 |
* Creates a new instance of {@link Xoroshiro128StarStar} using the |
57388 | 164 |
* 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
|
165 |
* {@link Xoroshiro128StarStar} created with the same seed in the same |
57388 | 166 |
* program generate identical sequences of values. |
167 |
* |
|
168 |
* @param seed the initial seed |
|
169 |
*/ |
|
170 |
public Xoroshiro128StarStar(long seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
171 |
// 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
|
172 |
// 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
|
173 |
// 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
|
174 |
// |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
175 |
// 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
|
176 |
// GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer. |
57547 | 177 |
this(RandomSupport.mixStafford13(seed ^= RandomSupport.SILVER_RATIO_64), |
178 |
RandomSupport.mixStafford13(seed + RandomSupport.GOLDEN_RATIO_64)); |
|
57388 | 179 |
} |
180 |
||
181 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
182 |
* Creates a new instance of {@link Xoroshiro128StarStar} that is likely to |
57388 | 183 |
* generate sequences of values that are statistically independent |
184 |
* of those of any other instances in the current program execution, |
|
185 |
* but may, and typically does, vary across program invocations. |
|
186 |
*/ |
|
187 |
public Xoroshiro128StarStar() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
188 |
// Using GOLDEN_RATIO_64 here gives us a good Weyl sequence of values. |
57547 | 189 |
this(DEFAULT_GEN.getAndAdd(RandomSupport.GOLDEN_RATIO_64)); |
57388 | 190 |
} |
191 |
||
192 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
193 |
* Creates a new instance of {@link Xoroshiro128StarStar} using the specified array of |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
194 |
* initial seed bytes. Instances of {@link Xoroshiro128StarStar} created with the same |
57388 | 195 |
* seed array in the same program execution generate identical sequences of values. |
196 |
* |
|
197 |
* @param seed the initial seed |
|
198 |
*/ |
|
199 |
public Xoroshiro128StarStar(byte[] seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
200 |
// Convert the seed to 2 long values, which are not both zero. |
57547 | 201 |
long[] data = RandomSupport.convertSeedBytesToLongs(seed, 2, 2); |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
202 |
long x0 = data[0], x1 = data[1]; |
57388 | 203 |
this.x0 = x0; |
204 |
this.x1 = x1; |
|
205 |
} |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
206 |
|
57388 | 207 |
/* ---------------- public methods ---------------- */ |
208 |
||
209 |
public Xoroshiro128StarStar copy() { return new Xoroshiro128StarStar(x0, x1); } |
|
210 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
211 |
/* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
212 |
* To the extent possible under law, the author has dedicated all copyright and related and |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
213 |
* neighboring rights to this software to the public domain worldwide. This software is |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
214 |
* distributed without any warranty. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
215 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
216 |
* See <http://creativecommons.org/publicdomain/zero/1.0/>. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
217 |
*/ |
57388 | 218 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
219 |
/* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
220 |
* This is the successor to xorshift128+. It is the fastest full-period generator passing |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
221 |
* BigCrush without systematic failures, but due to the relatively short period it is acceptable |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
222 |
* only for applications with a mild amount of parallelism; otherwise, use a xorshift1024* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
223 |
* generator. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
224 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
225 |
* Beside passing BigCrush, this generator passes the PractRand test suite up to (and included) |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
226 |
* 16TB, with the exception of binary rank tests, which fail due to the lowest bit being an |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
227 |
* LFSR; all other bits pass all tests. We suggest to use a sign test to extract a random |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
228 |
* Boolean value. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
229 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
230 |
* Note that the generator uses a simulated rotate operation, which most C compilers will turn |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
231 |
* into a single instruction. In Java, you can use Long.rotateLeft(). In languages that do not |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
232 |
* make low-level rotation instructions accessible xorshift128+ could be faster. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
233 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
234 |
* The state must be seeded so that it is not everywhere zero. If you have a 64-bit seed, we |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
235 |
* suggest to seed a splitmix64 generator and use its output to fill s. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
236 |
*/ |
57388 | 237 |
|
238 |
/** |
|
239 |
* Returns a pseudorandom {@code long} value. |
|
240 |
* |
|
241 |
* @return a pseudorandom {@code long} value |
|
242 |
*/ |
|
243 |
public long nextLong() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
244 |
final long s0 = x0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
245 |
long s1 = x1; |
59080 | 246 |
// Compute the result based on current state information |
247 |
// (this allows the computation to be overlapped with state update). |
|
248 |
final long result = Long.rotateLeft(s0 * 5, 7) * 9; // "starstar" mixing function |
|
57388 | 249 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
250 |
s1 ^= s0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
251 |
x0 = Long.rotateLeft(s0, 24) ^ s1 ^ (s1 << 16); // a, b |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
252 |
x1 = Long.rotateLeft(s1, 37); // c |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
253 |
|
59080 | 254 |
return result; |
57388 | 255 |
} |
256 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
257 |
public BigInteger period() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
258 |
return PERIOD; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
259 |
} |
57388 | 260 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
261 |
public double defaultJumpDistance() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
262 |
return 0x1.0p64; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
263 |
} |
57388 | 264 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
265 |
public double defaultLeapDistance() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
266 |
return 0x1.0p96; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
267 |
} |
57388 | 268 |
|
269 |
private static final long[] JUMP_TABLE = { 0xdf900294d8f554a5L, 0x170865df4b3201fcL }; |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
270 |
|
57388 | 271 |
private static final long[] LEAP_TABLE = { 0xd2a98b26625eee7bL, 0xdddf9b1090aa7ac1L }; |
272 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
273 |
/** |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
274 |
* This is the jump function for the generator. It is equivalent to 2**64 calls to nextLong(); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
275 |
* it can be used to generate 2**64 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
|
276 |
*/ |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
277 |
public void jump() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
278 |
jumpAlgorithm(JUMP_TABLE); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
279 |
} |
57388 | 280 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
281 |
/** |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
282 |
* This is the long-jump function for the generator. It is equivalent to 2**96 calls to next(); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
283 |
* it can be used to generate 2**32 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
|
284 |
* 2**32 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
|
285 |
*/ |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
286 |
public void leap() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
287 |
jumpAlgorithm(LEAP_TABLE); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
288 |
} |
57388 | 289 |
|
290 |
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
|
291 |
long s0 = 0, s1 = 0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
292 |
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
|
293 |
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
|
294 |
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
|
295 |
s0 ^= x0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
296 |
s1 ^= x1; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
297 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
298 |
nextLong(); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
299 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
300 |
x0 = s0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
301 |
x1 = s1; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
302 |
} |
57388 | 303 |
} |
304 |
} |