mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +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
@@ -10,7 +10,7 @@ import com.volmit.host.LoadedModules;
|
||||
import com.volmit.host.PlacementRunner;
|
||||
import com.volmit.host.ResolvedGraph;
|
||||
import com.volmit.host.StageGraphBuilder;
|
||||
import com.volmit.host.StageRandom;
|
||||
import com.volmit.host.StageStreams;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
@@ -120,7 +120,7 @@ class PlacementLifecyclePortTest {
|
||||
* is the first draw of the transforms stage's own stream.
|
||||
*/
|
||||
private static BlockPosition placed(int x, int y, int z) {
|
||||
var transforms = new Transforms(StageRandom.stream(TestContexts.SEED, PrepareModule.TRANSFORMS.id()).nextInt(4));
|
||||
var transforms = new Transforms(StageStreams.stream(TestContexts.SEED, PrepareModule.TRANSFORMS.id()).nextInt(4));
|
||||
var rotated = transforms.rotate(new BlockPosition(x, y, z), new BlockPosition(0, 0, 0));
|
||||
return rotated.add(SURFACE_ANCHOR);
|
||||
}
|
||||
|
||||
+12
-10
@@ -22,11 +22,10 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.random.RandomGenerator;
|
||||
import java.util.stream.IntStream;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StageRandomTest {
|
||||
class StageStreamsTest {
|
||||
private static final BlockState STONE = new BlockState("minecraft:stone");
|
||||
private static final int BLOCKS = 3;
|
||||
private static final int BOUND = 1_000_000;
|
||||
@@ -79,7 +78,7 @@ class StageRandomTest {
|
||||
}
|
||||
|
||||
private static List<Long> firstDraws(long invocationSeed, String stageId, int count) {
|
||||
var stream = StageRandom.stream(invocationSeed, ResourceId.parse(stageId));
|
||||
var stream = StageStreams.stream(invocationSeed, ResourceId.parse(stageId));
|
||||
return IntStream.range(0, count).mapToObj(ignored -> (long) stream.nextInt(BOUND)).toList();
|
||||
}
|
||||
|
||||
@@ -123,7 +122,7 @@ class StageRandomTest {
|
||||
var drawn = place(graph, TestContexts.SEED);
|
||||
|
||||
assertNotEquals(drawn.get("a:mod/pick"), drawn.get("c:mod/pick"));
|
||||
assertNotEquals(StageRandom.idHash(ResourceId.parse("a:mod/pick")), StageRandom.idHash(ResourceId.parse("c:mod/pick")));
|
||||
assertNotEquals(StageStreams.idHash(ResourceId.parse("a:mod/pick")), StageStreams.idHash(ResourceId.parse("c:mod/pick")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,16 +146,19 @@ class StageRandomTest {
|
||||
}
|
||||
|
||||
/** Pins the documented derivation with values computed outside Java from its specification, so a
|
||||
* change to the derivation or to the JDK's Random fails here instead of silently changing worlds.
|
||||
* change to the derivation fails here instead of silently changing worlds. {@code WorldRandomTest}
|
||||
* pins the generator itself.
|
||||
*/
|
||||
@Test
|
||||
void derivationMatchesItsSpecification() {
|
||||
var stage = ResourceId.parse("iris:stilt/fill");
|
||||
assertEquals(0xd11cde74d6a9508aL, StageRandom.idHash(stage), "64-bit FNV-1a of iris:stilt/fill");
|
||||
assertEquals(0xd11cde74d6a9508aL, StageStreams.idHash(stage), "64-bit FNV-1a of iris:stilt/fill");
|
||||
|
||||
RandomGenerator stream = StageRandom.stream(TestContexts.SEED, stage);
|
||||
assertEquals(4203081479670090427L, stream.nextLong());
|
||||
assertEquals(-978353470, stream.nextInt());
|
||||
assertEquals(943449, stream.nextInt(BOUND));
|
||||
assertEquals(-3593301659158072667L, StageStreams.streamSeed(TestContexts.SEED, StageStreams.idHash(stage)));
|
||||
|
||||
var stream = StageStreams.stream(TestContexts.SEED, stage);
|
||||
assertEquals(2676194909693269196L, stream.nextLong());
|
||||
assertEquals(1649978757, stream.nextInt());
|
||||
assertEquals(868252, stream.nextInt(BOUND));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package com.volmit.random;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.random.RandomGenerator;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** Pins every method to values computed outside Java from the specification in {@link WorldRandom}'s
|
||||
* documentation ({@code src/test/reference/world_random.py}), so any change to an algorithm fails here
|
||||
* instead of silently changing existing worlds.
|
||||
*/
|
||||
class WorldRandomTest {
|
||||
private static final long SEED = 42;
|
||||
private static final int REPEATS = 1_000;
|
||||
|
||||
private static <T> List<T> draws(int count, Function<WorldRandom, T> method) {
|
||||
var random = new WorldRandom(SEED);
|
||||
return Stream.generate(() -> method.apply(random)).limit(count).toList();
|
||||
}
|
||||
|
||||
@Test
|
||||
void longsAreSplitMix64Outputs() {
|
||||
assertEquals(List.of(-4767286540954276203L, 2949826092126892291L, 5139283748462763858L,
|
||||
6349198060258255764L, 701532786141963250L, -2430762948046562554L), draws(6, WorldRandom::nextLong));
|
||||
}
|
||||
|
||||
@Test
|
||||
void intsAreTheHighHalfOfALong() {
|
||||
assertEquals(List.of(-1109970394, 686809907, 1196582743, 1478287871, 163338330, -565956102),
|
||||
draws(6, WorldRandom::nextInt));
|
||||
}
|
||||
|
||||
@Test
|
||||
void boundedIntsMatchTheirSpecification() {
|
||||
assertEquals(List.of(7, 1, 2, 3, 0, 8), draws(6, random -> random.nextInt(10)));
|
||||
assertEquals(List.of(2, -4, -3, -2, -5, 3), draws(6, random -> random.nextInt(-5, 5)));
|
||||
assertEquals(List.of(1037513253, -1460673742, -950900906, -669195778, -1984145319, 1581527545),
|
||||
draws(6, random -> random.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE)));
|
||||
}
|
||||
|
||||
/** A range of 3 * 2^30 values rejects one draw in four; these twelve values take fourteen draws. */
|
||||
@Test
|
||||
void boundedIntsRejectDrawsThatWouldBias() {
|
||||
assertEquals(List.of(241264028, -1632376218, -1250046591, -1038767745, -2024979901, 649274747,
|
||||
431532146, -1052489127, -1487448649, -559454326, -493719002, -472403562),
|
||||
draws(12, random -> random.nextInt(Integer.MIN_VALUE, 1 << 30)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void boundedLongsMatchTheirSpecification() {
|
||||
assertEquals(List.of(741L, 159L, 278L, 344L, 38L, 868L), draws(6, random -> random.nextLong(1000)));
|
||||
assertEquals(List.of(483L, -681L, -443L, -312L, -924L, 736L), draws(6, random -> random.nextLong(-1000, 1000)));
|
||||
assertEquals(List.of(4456085495900499604L, -6273545944727883518L, -4084088288392011951L,
|
||||
-2874173976596520045L, -8521839250712812559L, 6792609088808213253L),
|
||||
draws(6, random -> random.nextLong(Long.MIN_VALUE, Long.MAX_VALUE)));
|
||||
}
|
||||
|
||||
/** A range of 3 * 2^62 values rejects one draw in four; these twelve values take fourteen draws. */
|
||||
@Test
|
||||
void boundedLongsRejectDrawsThatWouldBias() {
|
||||
assertEquals(List.of(1036221112711680751L, -7011002467759606590L, -5368909225507702915L,
|
||||
-8697222447248303371L, 2788613807392465988L, -6201723502271557115L,
|
||||
-4520406378645414305L, -666636742666774078L, -6388543298285055153L,
|
||||
-2402838030758921074L, -2120506962181442260L, -2028957847325109687L),
|
||||
draws(12, random -> random.nextLong(Long.MIN_VALUE, 1L << 62)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void booleansAreTheHighestBitOfALong() {
|
||||
assertEquals(List.of(true, false, false, false, false, true, false, true, false, true, false, false),
|
||||
draws(12, WorldRandom::nextBoolean));
|
||||
}
|
||||
|
||||
@Test
|
||||
void floatsAndDoublesAreTheHighBitsOfALong() {
|
||||
assertEquals(List.of(0.7415648698806763f, 0.1599103808403015f, 0.27860110998153687f,
|
||||
0.34419065713882446f, 0.038030147552490234f, 0.868228018283844f), draws(6, WorldRandom::nextFloat));
|
||||
assertEquals(List.of(0.7415648787718233, 0.1599103928769201, 0.27860113025513866,
|
||||
0.34419071652363753, 0.03803016854024621, 0.8682280765465323), draws(6, WorldRandom::nextDouble));
|
||||
}
|
||||
|
||||
@Test
|
||||
void boundedDoublesMatchTheirSpecification() {
|
||||
assertEquals(List.of(7.415648787718233, 1.599103928769201, 2.786011302551387, 3.4419071652363753,
|
||||
0.3803016854024621, 8.682280765465324), draws(6, random -> random.nextDouble(10.0)));
|
||||
assertEquals(List.of(4.915648787718233, -0.900896071230799, 0.28601130255138685, 0.9419071652363753,
|
||||
-2.119698314597538, 6.182280765465324), draws(6, random -> random.nextDouble(-2.5, 7.5)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doubleRangesWiderThanTheLargestDoubleAreHalvedFirst() {
|
||||
assertEquals(List.of(8.685190483839085e+307, -1.2227535039263658e+308, -7.960144564132267e+307,
|
||||
-5.601945585065466e+307, -1.6609599890574008e+308, 1.3239221705425129e+308),
|
||||
draws(6, random -> random.nextDouble(-Double.MAX_VALUE, Double.MAX_VALUE)));
|
||||
}
|
||||
|
||||
/** Most draws in a range one ulp wide round up to the bound and are replaced by the origin. */
|
||||
@Test
|
||||
void boundedDoublesNeverReturnTheBound() {
|
||||
double bound = Math.nextUp(1.0);
|
||||
assertEquals(List.of(1.0), draws(REPEATS, random -> random.nextDouble(1.0, bound)).stream().distinct().toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void gaussiansUseThePolarMethodWithStrictMath() {
|
||||
assertEquals(List.of(0.49295065581737485, -1.2810773478777024, -0.6018779810957331,
|
||||
-1.5423606818352653, -1.4515103855695575, 1.9375059271254913), draws(6, WorldRandom::nextGaussian));
|
||||
}
|
||||
|
||||
@Test
|
||||
void chanceIsADoubleBelowTheProbability() {
|
||||
assertEquals(List.of(false, true, false, false, true, false, true, false, false, false, true, false),
|
||||
draws(12, random -> random.chance(0.25)));
|
||||
assertEquals(List.of(false), draws(REPEATS, random -> random.chance(0)).stream().distinct().toList());
|
||||
assertEquals(List.of(true), draws(REPEATS, random -> random.chance(1)).stream().distinct().toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void zeroOriginRangesEqualTheSingleBoundMethods() {
|
||||
assertEquals(draws(REPEATS, random -> random.nextInt(37)), draws(REPEATS, random -> random.nextInt(0, 37)));
|
||||
assertEquals(draws(REPEATS, random -> random.nextLong(37)), draws(REPEATS, random -> random.nextLong(0, 37)));
|
||||
assertEquals(List.of(0), draws(REPEATS, random -> random.nextInt(1)).stream().distinct().toList());
|
||||
assertEquals(List.of(0L), draws(REPEATS, random -> random.nextLong(1)).stream().distinct().toList());
|
||||
}
|
||||
|
||||
/** How many values a method draws is part of the sequence: later draws shift if it changes. */
|
||||
@Test
|
||||
void methodsWithoutRejectionDrawExactlyOneValue() {
|
||||
List<Consumer<WorldRandom>> singleDraws = List.of(WorldRandom::nextInt, WorldRandom::nextBoolean,
|
||||
WorldRandom::nextFloat, WorldRandom::nextDouble, random -> random.nextDouble(3.0),
|
||||
random -> random.nextDouble(-1.0, 1.0), random -> random.chance(0.5));
|
||||
long second = draws(2, WorldRandom::nextLong).getLast();
|
||||
|
||||
for (var method : singleDraws) {
|
||||
var random = new WorldRandom(SEED);
|
||||
method.accept(random);
|
||||
assertEquals(second, random.nextLong());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidBoundsThrow() {
|
||||
var random = new WorldRandom(SEED);
|
||||
List<Runnable> invalid = List.of(
|
||||
() -> random.nextInt(0), () -> random.nextInt(-1), () -> random.nextInt(5, 5), () -> random.nextInt(5, 4),
|
||||
() -> random.nextLong(0), () -> random.nextLong(-1), () -> random.nextLong(5, 5), () -> random.nextLong(5, 4),
|
||||
() -> random.nextDouble(0), () -> random.nextDouble(-1), () -> random.nextDouble(Double.NaN),
|
||||
() -> random.nextDouble(Double.POSITIVE_INFINITY), () -> random.nextDouble(1, 1),
|
||||
() -> random.nextDouble(2, 1), () -> random.nextDouble(Double.NaN, 1),
|
||||
() -> random.nextDouble(0, Double.POSITIVE_INFINITY), () -> random.nextDouble(Double.NEGATIVE_INFINITY, 0),
|
||||
() -> random.chance(-0.1), () -> random.chance(1.1), () -> random.chance(Double.NaN));
|
||||
|
||||
for (var call : invalid) assertThrows(IllegalArgumentException.class, call::run);
|
||||
}
|
||||
|
||||
/** No method may fall through to an algorithm the JDK defines. A new public method needs pinned
|
||||
* values above before it joins this list.
|
||||
*/
|
||||
@Test
|
||||
void exposesOnlyTheMethodsPinnedHere() {
|
||||
assertFalse(RandomGenerator.class.isAssignableFrom(WorldRandom.class));
|
||||
assertTrue(Modifier.isFinal(WorldRandom.class.getModifiers()));
|
||||
var methods = Arrays.stream(WorldRandom.class.getDeclaredMethods())
|
||||
.filter(method -> Modifier.isPublic(method.getModifiers()))
|
||||
.map(WorldRandomTest::signature)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
assertEquals(Set.of("nextLong()", "nextLong(long)", "nextLong(long,long)", "nextInt()", "nextInt(int)",
|
||||
"nextInt(int,int)", "nextBoolean()", "nextFloat()", "nextDouble()", "nextDouble(double)",
|
||||
"nextDouble(double,double)", "nextGaussian()", "chance(double)"), methods);
|
||||
}
|
||||
|
||||
private static String signature(Method method) {
|
||||
return method.getName() + Arrays.stream(method.getParameterTypes()).map(Class::getName)
|
||||
.collect(Collectors.joining(",", "(", ")"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
"""Reference values for WorldRandomTest and StageStreamsTest, computed from the specification in
|
||||
WorldRandom's and StageStreams' documentation with Python integers and IEEE doubles, outside Java.
|
||||
|
||||
Run from IrisApi: python3 -B src/test/reference/world_random.py
|
||||
"""
|
||||
import math
|
||||
import struct
|
||||
|
||||
MASK64 = (1 << 64) - 1
|
||||
GOLDEN_GAMMA = 0x9E3779B97F4A7C15
|
||||
|
||||
|
||||
def signed64(value):
|
||||
value &= MASK64
|
||||
return value - (1 << 64) if value >= 1 << 63 else value
|
||||
|
||||
|
||||
def signed32(value):
|
||||
value &= (1 << 32) - 1
|
||||
return value - (1 << 32) if value >= 1 << 31 else value
|
||||
|
||||
|
||||
def mix(z):
|
||||
z = ((z ^ (z >> 30)) * 0xBF58476D1CE4E5B9) & MASK64
|
||||
z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & MASK64
|
||||
return z ^ (z >> 31)
|
||||
|
||||
|
||||
def fnv1a64(text):
|
||||
value = 0xCBF29CE484222325
|
||||
for byte in text.encode("utf-8"):
|
||||
value = ((value ^ byte) * 0x100000001B3) & MASK64
|
||||
return value
|
||||
|
||||
|
||||
def stream_seed(invocation_seed, stage_id):
|
||||
return mix(mix(invocation_seed & MASK64) ^ fnv1a64(stage_id))
|
||||
|
||||
|
||||
# fdlibm __ieee754_log (e_log.c), the algorithm StrictMath.log specifies.
|
||||
LN2_HI = 6.93147180369123816490e-01
|
||||
LN2_LO = 1.90821492927058770002e-10
|
||||
TWO54 = 1.80143985094819840000e+16
|
||||
LG = [6.666666666666735130e-01, 3.999999999940941908e-01, 2.857142874366239149e-01,
|
||||
2.222219843214978396e-01, 1.818357216161805012e-01, 1.531383769920937332e-01,
|
||||
1.479819860511658591e-01]
|
||||
|
||||
|
||||
def high_word(x):
|
||||
return signed32(struct.unpack("<Q", struct.pack("<d", x))[0] >> 32)
|
||||
|
||||
|
||||
def low_word(x):
|
||||
return struct.unpack("<Q", struct.pack("<d", x))[0] & 0xFFFFFFFF
|
||||
|
||||
|
||||
def with_high_word(x, high):
|
||||
return struct.unpack("<d", struct.pack("<Q", ((high & 0xFFFFFFFF) << 32) | low_word(x)))[0]
|
||||
|
||||
|
||||
def fdlibm_log(x):
|
||||
hx, lx = high_word(x), low_word(x)
|
||||
k = 0
|
||||
if hx < 0x00100000:
|
||||
if ((hx & 0x7FFFFFFF) | lx) == 0:
|
||||
return float("-inf")
|
||||
if hx < 0:
|
||||
return float("nan")
|
||||
k -= 54
|
||||
x *= TWO54
|
||||
hx = high_word(x)
|
||||
if hx >= 0x7FF00000:
|
||||
return x + x
|
||||
k += (hx >> 20) - 1023
|
||||
hx &= 0x000FFFFF
|
||||
i = (hx + 0x95F64) & 0x100000
|
||||
x = with_high_word(x, hx | (i ^ 0x3FF00000))
|
||||
k += i >> 20
|
||||
f = x - 1.0
|
||||
if (0x000FFFFF & (2 + hx)) < 3:
|
||||
if f == 0.0:
|
||||
if k == 0:
|
||||
return 0.0
|
||||
dk = float(k)
|
||||
return dk * LN2_HI + dk * LN2_LO
|
||||
r = f * f * (0.5 - 0.33333333333333333 * f)
|
||||
if k == 0:
|
||||
return f - r
|
||||
dk = float(k)
|
||||
return dk * LN2_HI - ((r - dk * LN2_LO) - f)
|
||||
s = f / (2.0 + f)
|
||||
dk = float(k)
|
||||
z = s * s
|
||||
i = hx - 0x6147A
|
||||
w = z * z
|
||||
j = 0x6B851 - hx
|
||||
t1 = w * (LG[1] + w * (LG[3] + w * LG[5]))
|
||||
t2 = z * (LG[0] + w * (LG[2] + w * (LG[4] + w * LG[6])))
|
||||
i |= j
|
||||
r = t2 + t1
|
||||
if i > 0:
|
||||
hfsq = 0.5 * f * f
|
||||
if k == 0:
|
||||
return f - (hfsq - s * (hfsq + r))
|
||||
return dk * LN2_HI - ((hfsq - (s * (hfsq + r) + dk * LN2_LO)) - f)
|
||||
if k == 0:
|
||||
return f - s * (f - r)
|
||||
return dk * LN2_HI - ((s * (f - r) - dk * LN2_LO) - f)
|
||||
|
||||
|
||||
class WorldRandom:
|
||||
def __init__(self, seed):
|
||||
self.state = seed & MASK64
|
||||
|
||||
def raw(self):
|
||||
"""The next output as an unsigned 64-bit integer."""
|
||||
self.state = (self.state + GOLDEN_GAMMA) & MASK64
|
||||
return mix(self.state)
|
||||
|
||||
def next_long(self):
|
||||
return signed64(self.raw())
|
||||
|
||||
def next_int(self):
|
||||
return signed32(self.raw() >> 32)
|
||||
|
||||
def bounded(self, width, bits):
|
||||
"""Lemire: high half of x * width, rejecting while the low half is below 2^bits mod width."""
|
||||
threshold = (1 << bits) % width
|
||||
while True:
|
||||
x = self.raw() >> (64 - bits)
|
||||
product = x * width
|
||||
if product % (1 << bits) >= threshold:
|
||||
return product >> bits
|
||||
|
||||
def next_int_bound(self, bound):
|
||||
return self.bounded(bound, 32)
|
||||
|
||||
def next_int_range(self, origin, bound):
|
||||
return origin + self.bounded(bound - origin, 32)
|
||||
|
||||
def next_long_bound(self, bound):
|
||||
return self.bounded(bound, 64)
|
||||
|
||||
def next_long_range(self, origin, bound):
|
||||
return origin + self.bounded(bound - origin, 64)
|
||||
|
||||
def next_boolean(self):
|
||||
return self.raw() >> 63 == 1
|
||||
|
||||
def next_float(self):
|
||||
return (self.raw() >> 40) * 2.0 ** -24
|
||||
|
||||
def next_double(self):
|
||||
return (self.raw() >> 11) * 2.0 ** -53
|
||||
|
||||
def next_double_bound(self, bound):
|
||||
value = self.next_double() * bound
|
||||
return value if value < bound else below(bound)
|
||||
|
||||
def next_double_range(self, origin, bound):
|
||||
unit = self.next_double()
|
||||
width = bound - origin
|
||||
if width != float("inf"):
|
||||
value = origin + unit * width
|
||||
else:
|
||||
value = 2 * (origin / 2 + unit * (bound / 2 - origin / 2))
|
||||
return value if value < bound else below(bound)
|
||||
|
||||
def next_gaussian(self):
|
||||
while True:
|
||||
u = 2 * self.next_double() - 1
|
||||
v = 2 * self.next_double() - 1
|
||||
s = u * u + v * v
|
||||
if 0 < s < 1:
|
||||
return u * math.sqrt(-2 * fdlibm_log(s) / s)
|
||||
|
||||
def chance(self, probability):
|
||||
return self.next_double() < probability
|
||||
|
||||
|
||||
def below(value):
|
||||
bits = struct.unpack("<q", struct.pack("<d", value))[0]
|
||||
return struct.unpack("<d", struct.pack("<q", bits - 1 if value > 0 else bits + 1))[0]
|
||||
|
||||
|
||||
def draws(label, method, count=6, seed=42):
|
||||
random = WorldRandom(seed)
|
||||
print(f"{label}: {[method(random) for _ in range(count)]}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"fnv1a64(iris:stilt/fill) = {signed64(fnv1a64('iris:stilt/fill'))}")
|
||||
print(f"streamSeed(42, iris:stilt/fill) = {signed64(stream_seed(42, 'iris:stilt/fill'))}")
|
||||
stage = WorldRandom(stream_seed(42, "iris:stilt/fill"))
|
||||
print(f"stage stream nextLong, nextInt, nextInt(1000000): "
|
||||
f"{stage.next_long()}, {stage.next_int()}, {stage.next_int_bound(1_000_000)}")
|
||||
|
||||
draws("nextLong()", WorldRandom.next_long)
|
||||
draws("nextInt()", WorldRandom.next_int)
|
||||
draws("nextInt(10)", lambda r: r.next_int_bound(10))
|
||||
draws("nextInt(-5, 5)", lambda r: r.next_int_range(-5, 5))
|
||||
# width 3 * 2^30: one draw in four is rejected.
|
||||
draws("nextInt(-2^31, 2^30)", lambda r: r.next_int_range(-(1 << 31), 1 << 30), count=12)
|
||||
draws("nextInt(MIN, MAX)", lambda r: r.next_int_range(-(1 << 31), (1 << 31) - 1))
|
||||
draws("nextLong(1000)", lambda r: r.next_long_bound(1000))
|
||||
draws("nextLong(-1000, 1000)", lambda r: r.next_long_range(-1000, 1000))
|
||||
# width 3 * 2^62: one draw in four is rejected.
|
||||
draws("nextLong(-2^63, 2^62)", lambda r: r.next_long_range(-(1 << 63), 1 << 62), count=12)
|
||||
draws("nextLong(MIN, MAX)", lambda r: r.next_long_range(-(1 << 63), (1 << 63) - 1))
|
||||
draws("nextBoolean()", WorldRandom.next_boolean, count=12)
|
||||
draws("nextFloat()", WorldRandom.next_float)
|
||||
draws("nextDouble()", WorldRandom.next_double)
|
||||
draws("nextDouble(10.0)", lambda r: r.next_double_bound(10.0))
|
||||
draws("nextDouble(-2.5, 7.5)", lambda r: r.next_double_range(-2.5, 7.5))
|
||||
max_double = 1.7976931348623157e308
|
||||
draws("nextDouble(-MAX, MAX)", lambda r: r.next_double_range(-max_double, max_double))
|
||||
draws("nextGaussian()", WorldRandom.next_gaussian)
|
||||
draws("chance(0.25)", lambda r: r.chance(0.25), count=12)
|
||||
Reference in new issue
Block a user