001package squidpony.squidmath; 002 003import squidpony.StringKit; 004 005import java.io.Serializable; 006 007/** 008 * A very-high-quality StatefulRandomness that is the fastest 64-bit generator in this library that passes statistical 009 * tests and is one-dimensionally equidistributed across all 64-bit outputs. Has 64 bits of state and natively outputs 010 * 64 bits at a time, changing the state with an "XLCG" or xor linear congruential generator (XLCGs are very similar to 011 * normal LCGs but have slightly better random qualities on the high bits; the code for this XLCG is 012 * {@code state = (state ^ 7822362180758744021) * -4126379630918251389}, and the only requirements for an XLCG are that 013 * the constant used with XOR, when treated as unsigned and modulo 8, equals 5, while the multiplier, again treated as 014 * unsigned and modulo 8, equals 3). Starting with that XLCG's output, it bitwise-left-rotates by 27, multiplies by a 015 * very large negative long (see next), then returns a right-xorshift by 25. The large negative long is 016 * -2643881736870682267, which when treated as unsigned is 2 to the 64 divided by an irrational number that generalizes 017 * the golden ratio. This specific irrational number is the solution to {@code x}<sup>{@code 5}</sup>{@code = x + 1}. 018 * Other multipliers also seem to work well as long as they have enough set bits (fairly-small multipliers fail tests). 019 * For whatever reason, the output of this simple function passes all 32TB of PractRand with one anomaly ("unusual" 020 * at 256GB), meaning its statistical quality is excellent. {@link ThrustAltRNG} is slightly faster, but isn't 021 * equidistributed; unlike ThrustAltRNG, this can produce all long values as output. ThrustAltRNG bunches some outputs 022 * and makes producing them more likely, while others can't be produced at all. Notably, this generator is faster than 023 * {@link LinnormRNG}, which it is based on, while improving its quality, is faster than {@link LightRNG} while keeping 024 * the same or higher quality, and is also faster than {@link XoRoRNG} while passing tests that XoRoRNG always or 025 * frequently fails, such as binary matrix rank tests. 026 * <br> 027 * This generator is a StatefulRandomness but not a SkippingRandomness, so it can't (efficiently) have the skip() method 028 * that LightRNG has. A method could be written to run the generator's state backwards, though, as well as to get the 029 * state from an output of {@link #nextLong()}. 030 * <br> 031 * The static determine() methods in this class are a completely different algorithm from the {@link #nextLong()} and 032 * similar instance methods here; they're a little faster than {@link LinnormRNG#determine(long)} and its family while 033 * actually having much better stability in case an increment is a poor fit for the internals of the generator. Like 034 * {@link #nextLong()}, {@link #determine(long)} can produce all possible long outputs and can take any long input; 035 * among determine() methods in this library that satisfy that constraint on input and output, this class' appears to be 036 * the fastest. 037 * <br> 038 * The name comes in a roundabout way from Xmulzencab, Maya mythology's bee god who is also called the Diving God, 039 * because the state transition is built around Xor and MUL. I was also listening to a Dio song, Holy Diver, at the 040 * time, and Diver is much more reasonable to pronounce than Xmulzencab. 041 * <br> 042 * Written December 14, 2018 by Tommy Ettinger. Thanks to M.E. O'Neill for her insights into the family of generators 043 * both this and her PCG-Random fall into, and to the team that worked on SplitMix64 for SplittableRandom in JDK 8. 044 * Chris Doty-Humphrey's work on PractRand has been invaluable, and I wouldn't know about XLCGs without his findings. 045 * Martin Roberts showed the technique for generalizing the golden ratio that produced the high-quality multiplier this 046 * uses in one place. Other constants were found empirically or via searching for probable primes with desirable values 047 * for use in an XLCG. 048 * @author Tommy Ettinger 049 */ 050public final class DiverRNG implements StatefulRandomness, Serializable { 051 052 private static final long serialVersionUID = 153186732328748834L; 053 054 private long state; /* The state can be seeded with any value. */ 055 056 /** 057 * Creates a new generator seeded using Math.random. 058 */ 059 public DiverRNG() { 060 this((long) ((Math.random() - 0.5) * 0x10000000000000L) 061 ^ (long) (((Math.random() - 0.5) * 2.0) * 0x8000000000000000L)); 062 } 063 064 public DiverRNG(final long seed) { 065 state = seed; 066 } 067 068 public DiverRNG(final String seed) { 069 state = CrossHash.hash64(seed); 070 } 071 072 @Override 073 public final int next(int bits) 074 { 075 long z = (state = (state ^ 0x6C8E9CF570932BD5L) * 0xC6BC279692B5CC83L); 076 z = (z << 27 | z >>> 37) * 0xDB4F0B9175AE2165L; 077 return (int)(z ^ z >>> 25) >>> (32 - bits); 078 } 079 080 /** 081 * Can return any long, positive or negative, of any size permissible in a 64-bit signed integer. 082 * 083 * @return any long, all 64 bits are random 084 */ 085 @Override 086 public final long nextLong() { 087 long z = (state = (state ^ 0x6C8E9CF570932BD5L) * 0xC6BC279692B5CC83L); 088 z = (z << 27 | z >>> 37) * 0xDB4F0B9175AE2165L; 089 return (z ^ z >>> 25); 090 } 091 092 /** 093 * Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the 094 * copy, both will generate the same sequence of random numbers from the point copy() was called. This just need to 095 * copy the state so it isn't shared, usually, and produce a new value with the same exact state. 096 * 097 * @return a copy of this RandomnessSource 098 */ 099 @Override 100 public DiverRNG copy() { 101 return new DiverRNG(state); 102 } 103 104 /** 105 * Can return any int, positive or negative, of any size permissible in a 32-bit signed integer. 106 * 107 * @return any int, all 32 bits are random 108 */ 109 public final int nextInt() { 110 long z = (state = (state ^ 0x6C8E9CF570932BD5L) * 0xC6BC279692B5CC83L); 111 z = (z << 27 | z >>> 37) * 0xDB4F0B9175AE2165L; 112 return (int)(z ^ z >>> 25); 113 } 114 115 /** 116 * Exclusive on the outer bound. The inner bound is 0. 117 * The bound can be negative, which makes this produce either a negative int or 0. 118 * 119 * @param bound the upper bound; should be positive 120 * @return a random int between 0 (inclusive) and bound (exclusive) 121 */ 122 public final int nextInt(final int bound) { 123 long z = (state = (state ^ 0x6C8E9CF570932BD5L) * 0xC6BC279692B5CC83L); 124 z = (z << 27 | z >>> 37) * 0xDB4F0B9175AE2165L; 125 return (int)((bound * ((z ^ z >>> 25) & 0xFFFFFFFFL)) >> 32); 126 } 127 128 /** 129 * Inclusive inner, exclusive outer. 130 * 131 * @param inner the inner bound, inclusive, can be positive or negative 132 * @param outer the outer bound, exclusive, can be positive or negative, usually greater than inner 133 * @return a random int between inner (inclusive) and outer (exclusive) 134 */ 135 public final int nextInt(final int inner, final int outer) { 136 return inner + nextInt(outer - inner); 137 } 138 139 /** 140 * Exclusive on bound (which may be positive or negative), with an inner bound of 0. 141 * If bound is negative this returns a negative long; if bound is positive this returns a positive long. The bound 142 * can even be 0, which will cause this to return 0L every time. 143 * <br> 144 * Credit for this method goes to <a href="https://oroboro.com/large-random-in-range/">Rafael Baptista's blog</a> 145 * for the original idea, and the JDK10 Math class' usage of Hacker's Delight code for the current algorithm. 146 * This method is drastically faster than the previous implementation when the bound varies often (roughly 4x 147 * faster, possibly more). It also always gets exactly one random long, so by default it advances the state as much 148 * as {@link #nextLong()}. 149 * 150 * @param bound the outer exclusive bound; can be positive or negative 151 * @return a random long between 0 (inclusive) and bound (exclusive) 152 */ 153 public long nextLong(long bound) { 154 long rand = (state = (state ^ 0x6C8E9CF570932BD5L) * 0xC6BC279692B5CC83L); 155 rand = (rand << 27 | rand >>> 37) * 0xDB4F0B9175AE2165L; 156 rand ^= rand >>> 25; 157 final long randLow = rand & 0xFFFFFFFFL; 158 final long boundLow = bound & 0xFFFFFFFFL; 159 rand >>>= 32; 160 bound >>= 32; 161 final long t = rand * boundLow + (randLow * boundLow >>> 32); 162 return rand * bound + (t >> 32) + (randLow * bound + (t & 0xFFFFFFFFL) >> 32); 163 } 164 /** 165 * Inclusive inner, exclusive outer; lower and upper can be positive or negative and there's no requirement for one 166 * to be greater than or less than the other. 167 * 168 * @param lower the lower bound, inclusive, can be positive or negative 169 * @param upper the upper bound, exclusive, can be positive or negative 170 * @return a random long that may be equal to lower and will otherwise be between lower and upper 171 */ 172 public final long nextLong(final long lower, final long upper) { 173 return lower + nextLong(upper - lower); 174 } 175 176 /** 177 * Gets a uniform random double in the range [0.0,1.0) 178 * 179 * @return a random double at least equal to 0.0 and less than 1.0 180 */ 181 public final double nextDouble() { 182 long z = (state = (state ^ 0x6C8E9CF570932BD5L) * 0xC6BC279692B5CC83L); 183 z = (z << 27 | z >>> 37) * 0xDB4F0B9175AE2165L; 184 return ((z ^ z >>> 25) & 0x1FFFFFFFFFFFFFL) * 0x1p-53; 185 186 } 187 188 /** 189 * Gets a uniform random double in the range [0.0,outer) given a positive parameter outer. If outer 190 * is negative, it will be the (exclusive) lower bound and 0.0 will be the (inclusive) upper bound. 191 * 192 * @param outer the exclusive outer bound, can be negative 193 * @return a random double between 0.0 (inclusive) and outer (exclusive) 194 */ 195 public final double nextDouble(final double outer) { 196 long z = (state = (state ^ 0x6C8E9CF570932BD5L) * 0xC6BC279692B5CC83L); 197 z = (z << 27 | z >>> 37) * 0xDB4F0B9175AE2165L; 198 return ((z ^ z >>> 25) & 0x1FFFFFFFFFFFFFL) * 0x1p-53 * outer; 199 } 200 201 /** 202 * Gets a uniform random float in the range [0.0,1.0) 203 * 204 * @return a random float at least equal to 0.0 and less than 1.0 205 */ 206 public final float nextFloat() { 207 final long z = (state = (state ^ 0x6C8E9CF570932BD5L) * 0xC6BC279692B5CC83L); 208 return ((z << 27 | z >>> 37) * 0xDB4F0B9175AE2165L >>> 40) * 0x1p-24f; 209 } 210 211 /** 212 * Gets a random value, true or false. 213 * Calls nextLong() once. 214 * 215 * @return a random true or false value. 216 */ 217 public final boolean nextBoolean() { 218 final long z = (state = (state ^ 0x6C8E9CF570932BD5L) * 0xC6BC279692B5CC83L); 219 return ((z << 27 | z >>> 37) * 0xDB4F0B9175AE2165L) < 0; 220 } 221 222 /** 223 * Given a byte array as a parameter, this will fill the array with random bytes (modifying it 224 * in-place). Calls nextLong() {@code Math.ceil(bytes.length / 8.0)} times. 225 * 226 * @param bytes a byte array that will have its contents overwritten with random bytes. 227 */ 228 public final void nextBytes(final byte[] bytes) { 229 int i = bytes.length, n; 230 while (i != 0) { 231 n = Math.min(i, 8); 232 for (long bits = nextLong(); n-- != 0; bits >>>= 8) bytes[--i] = (byte) bits; 233 } 234 } 235 236 /** 237 * Sets the seed (also the current state) of this generator. 238 * 239 * @param seed the seed to use for this LightRNG, as if it was constructed with this seed. 240 */ 241 @Override 242 public final void setState(final long seed) { 243 state = seed; 244 } 245 246 /** 247 * Gets the current state of this generator. 248 * 249 * @return the current seed of this LightRNG, changed once per call to nextLong() 250 */ 251 @Override 252 public final long getState() { 253 return state; 254 } 255 256 @Override 257 public String toString() { 258 return "DiverRNG with state 0x" + StringKit.hex(state) + 'L'; 259 } 260 261 @Override 262 public boolean equals(Object o) { 263 if (this == o) return true; 264 if (o == null || getClass() != o.getClass()) return false; 265 return state == ((DiverRNG) o).state; 266 } 267 268 @Override 269 public int hashCode() { 270 return (int) (state ^ (state >>> 32)); 271 } 272 273 /** 274 * Fast static randomizing method that takes its state as a parameter; state is expected to change between calls to 275 * this. It is recommended that you use {@code DiverRNG.determine(++state)} or {@code DiverRNG.determine(--state)} 276 * to produce a sequence of different numbers, and you may have slightly worse quality with increments or decrements 277 * other than 1. All longs are accepted by this method, and all longs can be produced; unlike several other classes' 278 * determine() methods, passing 0 here does not return 0. 279 * <br> 280 * You have a choice between determine() and randomize() in this class. {@code determine()} is the same as 281 * {@link LinnormRNG#determine(long)} and will behave well when the inputs are sequential, while {@code randomize()} 282 * is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with 283 * <a href="http://mostlymangling.blogspot.com/2019/01/better-stronger-mixer-and-test-procedure.html">the same 284 * testing requirements Evensen used for rrxmrrxmsx_0</a>; it will have excellent quality regardless of patterns in 285 * input but will be about 30% slower than {@code determine()}. Each method will produce all long outputs if given 286 * all possible longs as input. 287 * @param state any long; subsequent calls should change by an odd number, such as with {@code ++state} 288 * @return any long 289 */ 290 public static long determine(long state) 291 { 292 return (state = ((state = (((state * 0x632BE59BD9B4E019L) ^ 0x9E3779B97F4A7C15L) * 0xC6BC279692B5CC83L)) ^ state >>> 27) * 0xAEF17502108EF2D9L) ^ state >>> 25; 293 } 294 295 /** 296 * High-quality static randomizing method that takes its state as a parameter; state is expected to change between 297 * calls to this. It is suggested that you use {@code DiverRNG.randomize(++state)} or 298 * {@code DiverRNG.randomize(--state)} to produce a sequence of different numbers, but any increments are allowed 299 * (even-number increments won't be able to produce all outputs, but their quality will be fine for the numbers they 300 * can produce). All longs are accepted by this method, and all longs can be produced; unlike several other classes' 301 * determine() methods, passing 0 here does not return 0. 302 * <br> 303 * You have a choice between determine() and randomize() in this class. {@code determine()} is the same as 304 * {@link LinnormRNG#determine(long)} and will behave well when the inputs are sequential, while {@code randomize()} 305 * is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with 306 * <a href="http://mostlymangling.blogspot.com/2019/01/better-stronger-mixer-and-test-procedure.html">the same 307 * testing requirements Evensen used for rrxmrrxmsx_0</a>; it will have excellent quality regardless of patterns in 308 * input but will be about 30% slower than {@code determine()}. Each method will produce all long outputs if given 309 * all possible longs as input. 310 * @param state any long; subsequent calls should change by an odd number, such as with {@code ++state} 311 * @return any long 312 */ 313 public static long randomize(long state) 314 { 315 return (state = ((state = (state ^ (state << 41 | state >>> 23) ^ (state << 17 | state >>> 47) ^ 0xD1B54A32D192ED03L) * 0xAEF17502108EF2D9L) ^ state >>> 43 ^ state >>> 31 ^ state >>> 23) * 0xDB4F0B9175AE2165L) ^ state >>> 28; 316 } 317 318 /** 319 * Fast static randomizing method that takes its state as a parameter and limits output to an int between 0 320 * (inclusive) and bound (exclusive); state is expected to change between calls to this. It is recommended that you 321 * use {@code DiverRNG.determineBounded(++state, bound)} or {@code DiverRNG.determineBounded(--state, bound)} to 322 * produce a sequence of different numbers. All longs are accepted 323 * by this method, but not all ints between 0 and bound are guaranteed to be produced with equal likelihood (for any 324 * odd-number values for bound, this isn't possible for most generators). The bound can be negative. 325 * <br> 326 * You have a choice between determine() and randomize() in this class. {@code determine()} is the same as 327 * {@link LinnormRNG#determine(long)} and will behave well when the inputs are sequential, while {@code randomize()} 328 * is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with 329 * <a href="http://mostlymangling.blogspot.com/2019/01/better-stronger-mixer-and-test-procedure.html">the same 330 * testing requirements Evensen used for rrxmrrxmsx_0</a>; it will have excellent quality regardless of patterns in 331 * input but will be about 30% slower than {@code determine()}. Each method will produce all long outputs if given 332 * all possible longs as input. 333 * @param state any long; subsequent calls should change by an odd number, such as with {@code ++state} 334 * @param bound the outer exclusive bound, as an int 335 * @return an int between 0 (inclusive) and bound (exclusive) 336 */ 337 public static int determineBounded(long state, final int bound) 338 { 339 return (int)((bound * (((state = ((state = (((state * 0x632BE59BD9B4E019L) ^ 0x9E3779B97F4A7C15L) * 0xC6BC279692B5CC83L)) ^ state >>> 27) * 0xAEF17502108EF2D9L) ^ state >>> 25) & 0xFFFFFFFFL)) >> 32); 340 } 341 /** 342 * High-quality static randomizing method that takes its state as a parameter and limits output to an int between 0 343 * (inclusive) and bound (exclusive); state is expected to change between calls to this. It is suggested that you 344 * use {@code DiverRNG.randomizeBounded(++state)} or {@code DiverRNG.randomize(--state)} to produce a sequence of 345 * different numbers, but any increments are allowed (even-number increments won't be able to produce all outputs, 346 * but their quality will be fine for the numbers they can produce). All longs are accepted by this method, but not 347 * all ints between 0 and bound are guaranteed to be produced with equal likelihood (for any odd-number values for 348 * bound, this isn't possible for most generators). The bound can be negative. 349 * <br> 350 * You have a choice between determine() and randomize() in this class. {@code determine()} is the same as 351 * {@link LinnormRNG#determine(long)} and will behave well when the inputs are sequential, while {@code randomize()} 352 * is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with 353 * <a href="http://mostlymangling.blogspot.com/2019/01/better-stronger-mixer-and-test-procedure.html">the same 354 * testing requirements Evensen used for rrxmrrxmsx_0</a>; it will have excellent quality regardless of patterns in 355 * input but will be about 30% slower than {@code determine()}. Each method will produce all long outputs if given 356 * all possible longs as input. 357 * @param state any long; subsequent calls should change by an odd number, such as with {@code ++state} 358 * @param bound the outer exclusive bound, as an int 359 * @return an int between 0 (inclusive) and bound (exclusive) 360 */ 361 362 public static int randomizeBounded(long state, final int bound) 363 { 364 return (int)((bound * (((state = ((state = (state ^ (state << 41 | state >>> 23) ^ (state << 17 | state >>> 47) ^ 0xD1B54A32D192ED03L) * 0xAEF17502108EF2D9L) ^ state >>> 43 ^ state >>> 31 ^ state >>> 23) * 0xDB4F0B9175AE2165L) ^ state >>> 28) & 0xFFFFFFFFL)) >> 32); 365 } 366 367 /** 368 * Returns a random float that is deterministic based on state; if state is the same on two calls to this, this will 369 * return the same float. This is expected to be called with a changing variable, e.g. 370 * {@code determineFloat(++state)}, where the increment for state should generally be 1. The period is 2 to the 64 371 * if you increment or decrement by 1, but there are only 2 to the 30 possible floats between 0 and 1. 372 * <br> 373 * You have a choice between determine() and randomize() in this class. {@code determine()} is the same as 374 * {@link LinnormRNG#determine(long)} and will behave well when the inputs are sequential, while {@code randomize()} 375 * is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with 376 * <a href="http://mostlymangling.blogspot.com/2019/01/better-stronger-mixer-and-test-procedure.html">the same 377 * testing requirements Evensen used for rrxmrrxmsx_0</a>; it will have excellent quality regardless of patterns in 378 * input but will be about 30% slower than {@code determine()}. Each method will produce all long outputs if given 379 * all possible longs as input. 380 * @param state a variable that should be different every time you want a different random result; 381 * using {@code determineFloat(++state)} is recommended to go forwards or 382 * {@code determineFloat(--state)} to generate numbers in reverse order 383 * @return a pseudo-random float between 0f (inclusive) and 1f (exclusive), determined by {@code state} 384 */ 385 public static float determineFloat(long state) { 386 return ((((state = (((state * 0x632BE59BD9B4E019L) ^ 0x9E3779B97F4A7C15L) * 0xC6BC279692B5CC83L)) ^ state >>> 27) * 0xAEF17502108EF2D9L) >>> 40) * 0x1p-24f; 387 } 388 389 /** 390 * Returns a random float that is deterministic based on state; if state is the same on two calls to this, this will 391 * return the same float. This is expected to be called with a changing variable, e.g. 392 * {@code randomizeFloat(++state)}, where the increment for state can be any value and should usually be odd 393 * (even-number increments reduce the period). The period is 2 to the 64 if you increment or decrement by any odd 394 * number, but there are only 2 to the 30 possible floats between 0 and 1. 395 * <br> 396 * You have a choice between determine() and randomize() in this class. {@code determine()} is the same as 397 * {@link LinnormRNG#determine(long)} and will behave well when the inputs are sequential, while {@code randomize()} 398 * is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with 399 * <a href="http://mostlymangling.blogspot.com/2019/01/better-stronger-mixer-and-test-procedure.html">the same 400 * testing requirements Evensen used for rrxmrrxmsx_0</a>; it will have excellent quality regardless of patterns in 401 * input but will be about 30% slower than {@code determine()}. Each method will produce all long outputs if given 402 * all possible longs as input. 403 * @param state a variable that should be different every time you want a different random result; 404 * using {@code randomizeFloat(++state)} is recommended to go forwards or 405 * {@code randomizeFloat(--state)} to generate numbers in reverse order 406 * @return a pseudo-random float between 0f (inclusive) and 1f (exclusive), determined by {@code state} 407 */ 408 public static float randomizeFloat(long state) { 409 return (((state = (state ^ (state << 41 | state >>> 23) ^ (state << 17 | state >>> 47) ^ 0xD1B54A32D192ED03L) * 0xAEF17502108EF2D9L) ^ state >>> 43 ^ state >>> 31 ^ state >>> 23) * 0xDB4F0B9175AE2165L >>> 40) * 0x1p-24f; 410 411 } 412 413 /** 414 * Returns a random double that is deterministic based on state; if state is the same on two calls to this, this 415 * will return the same float. This is expected to be called with a changing variable, e.g. 416 * {@code determineDouble(++state)}, where the increment for state should generally be 1. The period is 2 to the 64 417 * if you increment or decrement by 1, but there are only 2 to the 62 possible doubles between 0 and 1. 418 * <br> 419 * You have a choice between determine() and randomize() in this class. {@code determine()} is the same as 420 * {@link LinnormRNG#determine(long)} and will behave well when the inputs are sequential, while {@code randomize()} 421 * is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with 422 * <a href="http://mostlymangling.blogspot.com/2019/01/better-stronger-mixer-and-test-procedure.html">the same 423 * testing requirements Evensen used for rrxmrrxmsx_0</a>; it will have excellent quality regardless of patterns in 424 * input but will be about 30% slower than {@code determine()}. Each method will produce all long outputs if given 425 * all possible longs as input. 426 * @param state a variable that should be different every time you want a different random result; 427 * using {@code determineDouble(++state)} is recommended to go forwards or 428 * {@code determineDouble(--state)} to generate numbers in reverse order 429 * @return a pseudo-random double between 0.0 (inclusive) and 1.0 (exclusive), determined by {@code state} 430 */ 431 public static double determineDouble(long state) { 432 return (((state = ((state = (((state * 0x632BE59BD9B4E019L) ^ 0x9E3779B97F4A7C15L) * 0xC6BC279692B5CC83L)) ^ state >>> 27) * 0xAEF17502108EF2D9L) ^ state >>> 25) & 0x1FFFFFFFFFFFFFL) * 0x1p-53; 433 } 434 435 /** 436 * Returns a random double that is deterministic based on state; if state is the same on two calls to this, this 437 * will return the same float. This is expected to be called with a changing variable, e.g. 438 * {@code randomizeDouble(++state)}, where the increment for state can be any number but should usually be odd 439 * (even-number increments reduce the period). The period is 2 to the 64 if you increment or decrement by 1, but 440 * there are only 2 to the 62 possible doubles between 0 and 1. 441 * <br> 442 * You have a choice between determine() and randomize() in this class. {@code determine()} is the same as 443 * {@link LinnormRNG#determine(long)} and will behave well when the inputs are sequential, while {@code randomize()} 444 * is a completely different algorithm based on Pelle Evensen's rrxmrrxmsx_0 and evaluated with 445 * <a href="http://mostlymangling.blogspot.com/2019/01/better-stronger-mixer-and-test-procedure.html">the same 446 * testing requirements Evensen used for rrxmrrxmsx_0</a>; it will have excellent quality regardless of patterns in 447 * input but will be about 30% slower than {@code determine()}. Each method will produce all long outputs if given 448 * all possible longs as input. 449 * @param state a variable that should be different every time you want a different random result; 450 * using {@code randomizeDouble(++state)} is recommended to go forwards or 451 * {@code randomizeDouble(--state)} to generate numbers in reverse order 452 * @return a pseudo-random double between 0.0 (inclusive) and 1.0 (exclusive), determined by {@code state} 453 */ 454 public static double randomizeDouble(long state) { 455 return (((state = ((state = (((state * 0x632BE59BD9B4E019L) ^ 0x9E3779B97F4A7C15L) * 0xC6BC279692B5CC83L)) ^ state >>> 27) * 0xAEF17502108EF2D9L) ^ state >>> 25) & 0x1FFFFFFFFFFFFFL) * 0x1p-53; 456 } 457 458}