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 Xoroshiro128Plus} 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 Xoroshiro128Plus} 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 |
* <em>except</em> that it does not pass the binary rank tests of PractRand, |
|
44 |
* which fail due to the lowest bit being an LFSR; all other bits pass all tests. |
|
45 |
* For this reason may be best for some purposes to use this generator to generate |
|
46 |
* pseudorandom {@code int}, {@code float}, and {@code double} values but not |
|
47 |
* {@code long} values. For the same reason, it may be best not to use the |
|
48 |
* method {@code nextGaussian} or {@code nextExponential} with this generator. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
49 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
50 |
* The class {@link Xoroshiro128Plus} uses the {@code xoroshiro128} algorithm, |
57388 | 51 |
* version 1.0 (parameters 24, 16, 37), with the "+" scrambler |
52 |
* (the returned value is the sum of the two state variables {@code x0} and {@code x1}). |
|
53 |
* Its state consists of two {@code long} fields {@code x0} and {@code x1}, |
|
54 |
* which can take on any values provided that they are not both zero. |
|
55 |
* 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
|
56 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
57 |
* The 64-bit values produced by the {@code nextLong()} method are equidistributed. |
57388 | 58 |
* To be precise, over the course of the cycle of length 2<sup>128</sup>-1, |
59 |
* each nonzero {@code long} value is generated 2<sup>64</sup> times, |
|
60 |
* but the value 0 is generated only 2<sup>64</sup>-1 times. |
|
61 |
* The values produced by the {@code nextInt()}, {@code nextFloat()}, and {@code nextDouble()} |
|
62 |
* methods are likewise equidistributed. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
63 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
64 |
* Instances {@link Xoroshiro128Plus} are <em>not</em> thread-safe. |
57388 | 65 |
* They are designed to be used so that each thread as its own instance. |
66 |
* 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
|
67 |
* can be used to construct new instances of {@link Xoroshiro128Plus} that traverse |
57388 | 68 |
* 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
|
69 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
70 |
* Instances of {@link Xoroshiro128Plus} are not cryptographically |
57388 | 71 |
* secure. Consider instead using {@link java.security.SecureRandom} |
72 |
* in security-sensitive applications. Additionally, |
|
73 |
* default-constructed instances do not use a cryptographically random |
|
74 |
* seed unless the {@linkplain System#getProperty system property} |
|
75 |
* {@code java.util.secureRandomSeed} is set to {@code true}. |
|
76 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
77 |
* @since 14 |
57388 | 78 |
*/ |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
79 |
public final class Xoroshiro128Plus implements LeapableRNG { |
57388 | 80 |
|
81 |
/* |
|
82 |
* Implementation Overview. |
|
83 |
* |
|
84 |
* This is an implementation of the xoroshiro128+ algorithm written |
|
85 |
* in 2016 by David Blackman and Sebastiano Vigna (vigna@acm.org), |
|
86 |
* and updated with improved parameters in 2018. |
|
87 |
* See http://xoshiro.di.unimi.it and these two papers: |
|
88 |
* |
|
89 |
* Sebastiano Vigna. 2016. An Experimental Exploration of Marsaglia's |
|
90 |
* xorshift Generators, Scrambled. ACM Transactions on Mathematical |
|
91 |
* Software 42, 4, Article 30 (June 2016), 23 pages. |
|
92 |
* https://doi.org/10.1145/2845077 |
|
93 |
* |
|
94 |
* David Blackman and Sebastiano Vigna. 2018. Scrambled Linear |
|
95 |
* Pseudorandom Number Generators. Computing Research Repository (CoRR). |
|
96 |
* http://arxiv.org/abs/1805.01407 |
|
97 |
* |
|
98 |
* The jump operation moves the current generator forward by 2*64 |
|
99 |
* steps; this has the same effect as calling nextLong() 2**64 |
|
100 |
* times, but is much faster. Similarly, the leap operation moves |
|
101 |
* the current generator forward by 2*96 steps; this has the same |
|
102 |
* effect as calling nextLong() 2**96 times, but is much faster. |
|
103 |
* The copy method may be used to make a copy of the current |
|
104 |
* generator. Thus one may repeatedly and cumulatively copy and |
|
105 |
* jump to produce a sequence of generators whose states are well |
|
106 |
* spaced apart along the overall state cycle (indeed, the jumps() |
|
107 |
* and leaps() methods each produce a stream of such generators). |
|
108 |
* The generators can then be parceled out to other threads. |
|
109 |
* |
|
110 |
* File organization: First the non-public methods that constitute the |
|
111 |
* main algorithm, then the public methods. Note that many methods are |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
112 |
* defined by classes {@link AbstractJumpableRNG} and {@link AbstractRNG}. |
57388 | 113 |
*/ |
114 |
||
115 |
/* ---------------- static fields ---------------- */ |
|
116 |
||
117 |
/** |
|
118 |
* The seed generator for default constructors. |
|
119 |
*/ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
120 |
private static final AtomicLong defaultGen = new AtomicLong(RNGSupport.initialSeed()); |
57388 | 121 |
|
122 |
/* |
|
123 |
* The period of this generator, which is 2**128 - 1. |
|
124 |
*/ |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
125 |
private static final BigInteger PERIOD = |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
126 |
BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE); |
57388 | 127 |
|
128 |
/* ---------------- instance fields ---------------- */ |
|
129 |
||
130 |
/** |
|
131 |
* The per-instance state. |
|
132 |
* At least one of the two fields x0 and x1 must be nonzero. |
|
133 |
*/ |
|
134 |
private long x0, x1; |
|
135 |
||
136 |
/* ---------------- constructors ---------------- */ |
|
137 |
||
138 |
/** |
|
139 |
* Basic constructor that initializes all fields from parameters. |
|
140 |
* It then adjusts the field values if necessary to ensure that |
|
141 |
* all constraints on the values of fields are met. |
|
142 |
* |
|
143 |
* @param x0 first word of the initial state |
|
144 |
* @param x1 second word of the initial state |
|
145 |
*/ |
|
146 |
public Xoroshiro128Plus(long x0, long x1) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
147 |
this.x0 = x0; |
57388 | 148 |
this.x1 = x1; |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
149 |
// If x0 and x1 are both zero, we must choose nonzero values. |
57388 | 150 |
if ((x0 | x1) == 0) { |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
151 |
// At least one of the two 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
|
152 |
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
|
153 |
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
|
154 |
} |
57388 | 155 |
} |
156 |
||
157 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
158 |
* Creates a new instance of {@link Xoroshiro128Plus} using the |
57388 | 159 |
* 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
|
160 |
* {@link Xoroshiro128Plus} created with the same seed in the same |
57388 | 161 |
* program generate identical sequences of values. |
162 |
* |
|
163 |
* @param seed the initial seed |
|
164 |
*/ |
|
165 |
public Xoroshiro128Plus(long seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
166 |
// 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
|
167 |
// 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
|
168 |
// 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
|
169 |
// |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
170 |
// 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
|
171 |
// 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
|
172 |
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
|
173 |
RNGSupport.mixStafford13(seed + RNGSupport.GOLDEN_RATIO_64)); |
57388 | 174 |
} |
175 |
||
176 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
177 |
* Creates a new instance of {@link Xoroshiro128Plus} that is likely to |
57388 | 178 |
* generate sequences of values that are statistically independent |
179 |
* of those of any other instances in the current program execution, |
|
180 |
* but may, and typically does, vary across program invocations. |
|
181 |
*/ |
|
182 |
public Xoroshiro128Plus() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
183 |
// 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
|
184 |
this(defaultGen.getAndAdd(RNGSupport.GOLDEN_RATIO_64)); |
57388 | 185 |
} |
186 |
||
187 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
188 |
* Creates a new instance of {@link Xoroshiro128Plus} using the specified array of |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
189 |
* initial seed bytes. Instances of {@link Xoroshiro128Plus} created with the same |
57388 | 190 |
* seed array in the same program execution generate identical sequences of values. |
191 |
* |
|
192 |
* @param seed the initial seed |
|
193 |
*/ |
|
194 |
public Xoroshiro128Plus(byte[] seed) { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
195 |
// Convert the seed to 2 long values, which are not both zero. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
196 |
long[] data = RNGSupport.convertSeedBytesToLongs(seed, 2, 2); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
197 |
long x0 = data[0], x1 = data[1]; |
57388 | 198 |
this.x0 = x0; |
199 |
this.x1 = x1; |
|
200 |
} |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
201 |
|
57388 | 202 |
/* ---------------- public methods ---------------- */ |
203 |
||
204 |
public Xoroshiro128Plus copy() { return new Xoroshiro128Plus(x0, x1); } |
|
205 |
||
206 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
207 |
/* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
208 |
* 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
|
209 |
* neighboring rights to this software to the public domain worldwide. This software is distributed |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
210 |
* without any warranty. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
211 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
212 |
* 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
|
213 |
*/ |
57388 | 214 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
215 |
/* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
216 |
* 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
|
217 |
* 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
|
218 |
* 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
|
219 |
* generator. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
220 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
221 |
* 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
|
222 |
* 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
|
223 |
* 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
|
224 |
* Boolean value. |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
225 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
226 |
* 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
|
227 |
* 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
|
228 |
* 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
|
229 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
230 |
* 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
|
231 |
* 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
|
232 |
*/ |
57388 | 233 |
|
234 |
/** |
|
235 |
* Returns a pseudorandom {@code long} value. |
|
236 |
* |
|
237 |
* @return a pseudorandom {@code long} value |
|
238 |
*/ |
|
239 |
public long nextLong() { |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
240 |
final long s0 = x0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
241 |
long s1 = x1; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
242 |
final long z = s0 + s1; |
57388 | 243 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
244 |
s1 ^= s0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
245 |
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
|
246 |
x1 = Long.rotateLeft(s1, 37); // c |
57388 | 247 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
248 |
return z; |
57388 | 249 |
} |
250 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
251 |
public BigInteger period() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
252 |
return PERIOD; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
253 |
} |
57388 | 254 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
255 |
public double defaultJumpDistance() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
256 |
return 0x1.0p64; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
257 |
} |
57388 | 258 |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
259 |
public double defaultLeapDistance() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
260 |
return 0x1.0p96; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
261 |
} |
57388 | 262 |
|
263 |
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
|
264 |
|
57388 | 265 |
private static final long[] LEAP_TABLE = { 0xd2a98b26625eee7bL, 0xdddf9b1090aa7ac1L }; |
266 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
267 |
/* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
268 |
* This is the jump function for the generator. It is equivalent |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
269 |
* to 2**64 calls to nextLong(); it can be used to generate 2**64 |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
270 |
* 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
|
271 |
*/ |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
272 |
public void jump() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
273 |
jumpAlgorithm(JUMP_TABLE); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
274 |
} |
57388 | 275 |
|
57437
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 |
* 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
|
278 |
* 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
|
279 |
* 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
|
280 |
*/ |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
281 |
public void leap() { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
282 |
jumpAlgorithm(LEAP_TABLE); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
283 |
} |
57388 | 284 |
|
285 |
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
|
286 |
long s0 = 0, s1 = 0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
287 |
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
|
288 |
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
|
289 |
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
|
290 |
s0 ^= x0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
291 |
s1 ^= x1; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
292 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
293 |
nextLong(); |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
294 |
} |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
295 |
x0 = s0; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
296 |
x1 = s1; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
297 |
} |
57388 | 298 |
} |
299 |
} |