mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Give stages WorldRandom, a SplitMix64 generator whose every method IrisApi defines
java.util.Random fixes only some of its methods; the ranged RandomGenerator defaults module authors reach for are left to the JDK, so an update could change existing worlds. WorldRandom does not implement RandomGenerator and exposes a curated set (ints, longs, ranges, booleans, floats, doubles, gaussian, chance) with Lemire bounds and the polar gaussian on StrictMath. StageContext, ObjectGenerationContext and BlockPalette now use it. StageRandom becomes StageStreams; the seed derivation is unchanged. Every method is pinned to values from an independent Python reference in src/test/reference.
This commit is contained in:
1 parent
d5aad57ad9
commit
1ce0a19abb
17 files changed
+653
-48
No files matched your search
@@ -0,0 +1,204 @@
|
||||
package com.volmit.random;
|
||||
|
||||
/** A random stream whose every value is defined here, so the same seed gives the same draws on
|
||||
* every machine and every JDK. World generation draws from it: stages through
|
||||
* {@link com.volmit.stage.StageContext#random()}, object generators through
|
||||
* {@link com.volmit.object.ObjectGenerationContext#random()}.
|
||||
*
|
||||
* <p>It deliberately does not implement {@link java.util.random.RandomGenerator}: that interface's
|
||||
* default methods, and any a later JDK adds, use algorithms the JDK may change, which would change
|
||||
* existing worlds. Only the methods below exist, and each is specified exactly.
|
||||
*
|
||||
* <p><b>Algorithm.</b> The state is one {@code long}, initially the seed. {@link #nextLong()} is
|
||||
* SplitMix64 (Steele, Lea and Flood, 2014): add {@code 0x9e3779b97f4a7c15} to the state, then return
|
||||
* {@code mix(state)}, where {@code mix} is the finalizer
|
||||
* {@code z = (z ^ (z >>> 30)) * 0xbf58476d1ce4e5b9; z = (z ^ (z >>> 27)) * 0x94d049bb133111eb; z ^ (z >>> 31)}.
|
||||
* Every other method is defined on {@code nextLong()} values:
|
||||
* <ul>
|
||||
* <li>{@code nextInt()}: the high 32 bits of one value;</li>
|
||||
* <li>{@code nextBoolean()}: whether one value is negative (its highest bit);</li>
|
||||
* <li>{@code nextFloat()}: the high 24 bits of one value times 2<sup>-24</sup>;</li>
|
||||
* <li>{@code nextDouble()}: the high 53 bits of one value times 2<sup>-53</sup>;</li>
|
||||
* <li>bounded integers: Lemire's multiply-and-reject method, which is unbiased. For a range of
|
||||
* {@code n} values, {@code ints} take the high 32 bits {@code x} of one value and compute the
|
||||
* 64-bit product {@code x * n}, {@code longs} take one value {@code x} and compute the
|
||||
* 128-bit product; when the product's low half is below 2<sup>32</sup> (or 2<sup>64</sup>)
|
||||
* {@code mod n}, they draw again; the result is the product's high half plus the origin;</li>
|
||||
* <li>bounded doubles: {@code origin + nextDouble() * (bound - origin)}, a result that rounds up
|
||||
* to {@code bound} replaced by the largest double below it (see {@link #nextDouble(double, double)}
|
||||
* for ranges wider than the largest double);</li>
|
||||
* <li>{@code nextGaussian()}: Marsaglia's polar method on pairs of {@code nextDouble()} values,
|
||||
* using {@link StrictMath}, whose results are fixed;</li>
|
||||
* <li>{@code chance(p)}: {@code nextDouble() < p}.</li>
|
||||
* </ul>
|
||||
* Methods without rejection draw exactly one value. {@code nextInt(0, n)} equals {@code nextInt(n)}
|
||||
* and {@code nextLong(0, n)} equals {@code nextLong(n)}.
|
||||
*
|
||||
* <p>Not thread-safe. One stage uses its stream within one invocation; do not share or retain it.
|
||||
*/
|
||||
public final class WorldRandom {
|
||||
private static final long GOLDEN_GAMMA = 0x9e3779b97f4a7c15L;
|
||||
private static final int INT_BITS = 32;
|
||||
private static final long UNSIGNED_INT_MASK = 0xffff_ffffL;
|
||||
private static final long INT_RANGE = 1L << INT_BITS;
|
||||
private static final int FLOAT_SHIFT = Long.SIZE - 24;
|
||||
private static final float FLOAT_UNIT = 0x1.0p-24f;
|
||||
private static final int DOUBLE_SHIFT = Long.SIZE - 53;
|
||||
private static final double DOUBLE_UNIT = 0x1.0p-53;
|
||||
|
||||
private long state;
|
||||
|
||||
public WorldRandom(long seed) {
|
||||
state = seed;
|
||||
}
|
||||
|
||||
/** A uniformly distributed {@code long}: the next SplitMix64 output. */
|
||||
public long nextLong() {
|
||||
state += GOLDEN_GAMMA;
|
||||
return mix(state);
|
||||
}
|
||||
|
||||
/** A uniformly distributed {@code long} in {@code [0, bound)}. Throws if {@code bound <= 0}. */
|
||||
public long nextLong(long bound) {
|
||||
if (bound <= 0) throw new IllegalArgumentException("bound must be positive: " + bound);
|
||||
return boundedLong(bound);
|
||||
}
|
||||
|
||||
/** A uniformly distributed {@code long} in {@code [origin, bound)}, for any {@code origin < bound},
|
||||
* including ranges wider than {@code Long.MAX_VALUE}. Throws if {@code origin >= bound}.
|
||||
*/
|
||||
public long nextLong(long origin, long bound) {
|
||||
if (origin >= bound) throw invalidRange(origin, bound);
|
||||
return origin + boundedLong(bound - origin);
|
||||
}
|
||||
|
||||
/** A uniformly distributed {@code int}: the high 32 bits of {@link #nextLong()}. */
|
||||
public int nextInt() {
|
||||
return (int) (nextLong() >>> INT_BITS);
|
||||
}
|
||||
|
||||
/** A uniformly distributed {@code int} in {@code [0, bound)}. Throws if {@code bound <= 0}. */
|
||||
public int nextInt(int bound) {
|
||||
if (bound <= 0) throw new IllegalArgumentException("bound must be positive: " + bound);
|
||||
return (int) boundedInt(bound);
|
||||
}
|
||||
|
||||
/** A uniformly distributed {@code int} in {@code [origin, bound)}, for any {@code origin < bound},
|
||||
* including ranges wider than {@code Integer.MAX_VALUE}. Throws if {@code origin >= bound}.
|
||||
*/
|
||||
public int nextInt(int origin, int bound) {
|
||||
if (origin >= bound) throw invalidRange(origin, bound);
|
||||
return (int) (origin + boundedInt((long) bound - origin));
|
||||
}
|
||||
|
||||
/** {@code true} or {@code false} with equal probability: whether {@link #nextLong()} is negative. */
|
||||
public boolean nextBoolean() {
|
||||
return nextLong() < 0;
|
||||
}
|
||||
|
||||
/** A uniformly distributed {@code float} in {@code [0, 1)}, a multiple of 2<sup>-24</sup>. */
|
||||
public float nextFloat() {
|
||||
return (nextLong() >>> FLOAT_SHIFT) * FLOAT_UNIT;
|
||||
}
|
||||
|
||||
/** A uniformly distributed {@code double} in {@code [0, 1)}, a multiple of 2<sup>-53</sup>. */
|
||||
public double nextDouble() {
|
||||
return (nextLong() >>> DOUBLE_SHIFT) * DOUBLE_UNIT;
|
||||
}
|
||||
|
||||
/** A {@code double} in {@code [0, bound)}: {@code nextDouble() * bound}, never {@code bound} itself.
|
||||
* Throws unless {@code bound} is positive and finite.
|
||||
*/
|
||||
public double nextDouble(double bound) {
|
||||
if (!(bound > 0 && bound < Double.POSITIVE_INFINITY)) {
|
||||
throw new IllegalArgumentException("bound must be positive and finite: " + bound);
|
||||
}
|
||||
return belowBound(nextDouble() * bound, bound);
|
||||
}
|
||||
|
||||
/** A {@code double} in {@code [origin, bound)}, never {@code bound} itself:
|
||||
* {@code origin + nextDouble() * (bound - origin)}. When {@code bound - origin} exceeds the largest
|
||||
* double, the range is halved first: {@code 2 * (origin / 2 + nextDouble() * (bound / 2 - origin / 2))}.
|
||||
* Throws unless both are finite and {@code origin < bound}.
|
||||
*/
|
||||
public double nextDouble(double origin, double bound) {
|
||||
if (!(origin < bound && Double.isFinite(origin) && Double.isFinite(bound))) {
|
||||
throw new IllegalArgumentException("origin and bound must be finite with origin < bound: [" + origin + ", " + bound + ")");
|
||||
}
|
||||
double unit = nextDouble();
|
||||
double width = bound - origin;
|
||||
double value = Double.isFinite(width) ? origin + unit * width : 2 * (origin / 2 + unit * (bound / 2 - origin / 2));
|
||||
return belowBound(value, bound);
|
||||
}
|
||||
|
||||
/** A normally distributed {@code double} with mean 0 and standard deviation 1, by Marsaglia's polar
|
||||
* method: draw {@code u = 2 * nextDouble() - 1} and {@code v = 2 * nextDouble() - 1} until
|
||||
* {@code s = u * u + v * v} is in {@code (0, 1)}, then return
|
||||
* {@code u * StrictMath.sqrt(-2 * StrictMath.log(s) / s)}. The second value of the pair is discarded.
|
||||
*/
|
||||
public double nextGaussian() {
|
||||
double u;
|
||||
double s;
|
||||
do {
|
||||
u = 2 * nextDouble() - 1;
|
||||
double v = 2 * nextDouble() - 1;
|
||||
s = u * u + v * v;
|
||||
} while (s >= 1 || s == 0);
|
||||
return u * StrictMath.sqrt(-2 * StrictMath.log(s) / s);
|
||||
}
|
||||
|
||||
/** Whether an event of the given probability happens: {@code nextDouble() < probability}. Always
|
||||
* draws one value, so {@code chance(0)} is never and {@code chance(1)} always true. Throws unless
|
||||
* {@code probability} is in {@code [0, 1]}.
|
||||
*/
|
||||
public boolean chance(double probability) {
|
||||
if (!(probability >= 0 && probability <= 1)) {
|
||||
throw new IllegalArgumentException("probability must be in [0, 1]: " + probability);
|
||||
}
|
||||
return nextDouble() < probability;
|
||||
}
|
||||
|
||||
/** Lemire's method for {@code range} in {@code [1, 2^32]}: high half of a 32-by-32-bit product. */
|
||||
private long boundedInt(long range) {
|
||||
long product = (nextLong() >>> INT_BITS) * range;
|
||||
long low = product & UNSIGNED_INT_MASK;
|
||||
if (low < range) {
|
||||
long threshold = (INT_RANGE - range) % range;
|
||||
while (low < threshold) {
|
||||
product = (nextLong() >>> INT_BITS) * range;
|
||||
low = product & UNSIGNED_INT_MASK;
|
||||
}
|
||||
}
|
||||
return product >>> INT_BITS;
|
||||
}
|
||||
|
||||
/** Lemire's method for {@code range} in {@code [1, 2^64)}, read as unsigned: high half of a
|
||||
* 64-by-64-bit product.
|
||||
*/
|
||||
private long boundedLong(long range) {
|
||||
long value = nextLong();
|
||||
long low = value * range;
|
||||
if (Long.compareUnsigned(low, range) < 0) {
|
||||
long threshold = Long.remainderUnsigned(-range, range);
|
||||
while (Long.compareUnsigned(low, threshold) < 0) {
|
||||
value = nextLong();
|
||||
low = value * range;
|
||||
}
|
||||
}
|
||||
return Math.unsignedMultiplyHigh(value, range);
|
||||
}
|
||||
|
||||
private static double belowBound(double value, double bound) {
|
||||
return value < bound ? value : Math.nextDown(bound);
|
||||
}
|
||||
|
||||
private static IllegalArgumentException invalidRange(long origin, long bound) {
|
||||
return new IllegalArgumentException("origin must be below bound: [" + origin + ", " + bound + ")");
|
||||
}
|
||||
|
||||
private static long mix(long value) {
|
||||
value = (value ^ (value >>> 30)) * 0xbf58476d1ce4e5b9L;
|
||||
value = (value ^ (value >>> 27)) * 0x94d049bb133111ebL;
|
||||
return value ^ (value >>> 31);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user