This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* How to deterministically initalize a PRNG from (x, y) coordinates. The | |
* exact algorithm choice is arbitrary. `coordToLong` could be even simpler. | |
* The "hash" is a good scrambling function from 64-bit input to 64-bit | |
* output. Hashing might be redundant for certain PRNG implementations, but | |
* only if the implementation already mixes seeds very well. (Similar seeds | |
* can produce outputs with the same patterns.) If you use another RNG then | |
* you need to bundle its source with your project. C's "srand" should not | |
* be used because the same seed can produce different results on different | |
* platforms. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
default int nextInt_Biased(int upperBound) | |
{ | |
if (upperBound == 0) { return nextInt(); } | |
if (upperBound == 1) { return 0; } | |
final long unsignedLimit = upperBound & 0xFFFF_FFFFL; | |
final int log2Ceil = 64 - Long.numberOfLeadingZeros(unsignedLimit - 1); | |
final int bitsOfRandomness = 64 - log2Ceil; | |
final long rand = nextUnsignedLongBits(bitsOfRandomness); | |
return (int)(long)((rand * unsignedLimit) >>> bitsOfRandomness); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.futuresecurity.random.sampling; | |
import io.github.futuresecurity.random.UniformGenerator; | |
import java.util.SplittableRandom; | |
public class BoundedIntSampling | |
{ | |
public static int randomInt_TwoFactor32(int bound, UniformGenerator rng) | |
{ |