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()}. * *

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. * *

Algorithm. 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: *

* Methods without rejection draw exactly one value. {@code nextInt(0, n)} equals {@code nextInt(n)} * and {@code nextLong(0, n)} equals {@code nextLong(n)}. * *

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-24. */ public float nextFloat() { return (nextLong() >>> FLOAT_SHIFT) * FLOAT_UNIT; } /** A uniformly distributed {@code double} in {@code [0, 1)}, a multiple of 2-53. */ 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); } }