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:
piexel committed 2026-10-06 11:36:13 +02:00
1 parent d5aad57ad9
commit 1ce0a19abb
17 files changed
+653 -48

No files matched your search

@@ -4,12 +4,12 @@ import com.volmit.config.FeatureSettings;
import com.volmit.generation.GenerationContext;
import com.volmit.hunk.Hunk;
import com.volmit.mantle.Mantle;
import com.volmit.random.WorldRandom;
import com.volmit.resource.PackResources;
import com.volmit.stage.ChunkStageContext;
import com.volmit.stage.Stage;
import com.volmit.world.TerrainSampler;
import java.util.Objects;
import java.util.random.RandomGenerator;
/** The context every stage of one chunk invocation receives. */
final class ChunkInvocation implements ChunkStageContext {
@@ -76,7 +76,7 @@ final class ChunkInvocation implements ChunkStageContext {
}
@Override
public RandomGenerator random() {
public WorldRandom random() {
return stages.random();
}
}
@@ -19,7 +19,7 @@ public final class ChunkRunner {
}
/** Runs one chunk. The host derives {@code invocationSeed} from the world seed and the chunk
* position; each stage draws from its own stream of that seed ({@link StageRandom}).
* position; each stage draws from its own stream of that seed ({@link StageStreams}).
*/
public void generate(GenerationContext inputs, long invocationSeed, ConfiguredSettings settings) {
var chunk = new ChunkInvocation(new StageInvocation(graph, invocationSeed, settings), inputs);
@@ -5,7 +5,7 @@ import com.volmit.stage.Stage;
/** Execution data of one resolved stage. Indices are positions in the resolved order.
* readableStages/readablePositions are the transitive requirements whose outputs it may read.
* settingsSlot indexes the graph's distinct settings types, or is NO_SETTINGS. idHash keys the
* stage's random stream ({@link StageRandom}), computed once per pack load.
* stage's random stream ({@link StageStreams}), computed once per pack load.
*/
record ExecutableStage(
Stage<?> stage,
@@ -225,7 +225,7 @@ final class GraphResolver {
.toArray(Stage<?>[]::new),
readable[index].stream().toArray(),
settingsSlot(declaration, settingsTypes),
StageRandom.idHash(declaration.stage().id()));
StageStreams.idHash(declaration.stage().id()));
stages.add(new ResolvedStage(declaration.stage(), declaration.module(), declaration.settings(),
incomingEdges(node, position), absentSoftTargets.get(node)));
}
@@ -6,13 +6,13 @@ import com.volmit.object.ObjectTransformer;
import com.volmit.placement.PlacementAccess;
import com.volmit.placement.PlacementContext;
import com.volmit.placement.PlacementRequest;
import com.volmit.random.WorldRandom;
import com.volmit.resource.PackResources;
import com.volmit.stage.BlockStageContext;
import com.volmit.stage.Stage;
import com.volmit.world.BlockView;
import com.volmit.world.TerrainSampler;
import java.util.Objects;
import java.util.random.RandomGenerator;
/** The context every stage of one placement attempt receives. Block accessors work only while
* a block pass is running.
@@ -117,7 +117,7 @@ final class PlacementInvocation implements BlockStageContext {
}
@Override
public RandomGenerator random() {
public WorldRandom random() {
return stages.random();
}
@@ -24,7 +24,7 @@ public final class PlacementRunner {
/** Takes ownership of a fresh edit and closes it on success, rejection or exception.
* The host prepares the edit area, the read-only inputs and the settings configured for this
* placement, and derives {@code invocationSeed} from the world seed and the placement's stable
* identity. Each stage draws from its own stream of that seed ({@link StageRandom}). Stages never
* identity. Each stage draws from its own stream of that seed ({@link StageStreams}). Stages never
* receive commit/close rights.
*/
public PlacementOutcome place(
@@ -1,11 +1,11 @@
package com.volmit.host;
import com.volmit.IrisFeature;
import com.volmit.random.WorldRandom;
import com.volmit.stage.BlockStageContext;
import com.volmit.stage.Stage;
import java.util.Arrays;
import java.util.Objects;
import java.util.random.RandomGenerator;
import java.util.stream.IntStream;
/** Pipeline-independent state of one invocation: activation, settings, stage outputs and stage
@@ -24,7 +24,7 @@ final class StageInvocation {
private final boolean[] active;
private final Object[] outputs;
private final long invocationSeed;
private final RandomGenerator[] streams;
private final WorldRandom[] streams;
private int current = NO_STAGE;
StageInvocation(ResolvedGraph graph, long invocationSeed, ConfiguredSettings settings) {
@@ -34,7 +34,7 @@ final class StageInvocation {
outputs = new Object[graph.size()];
Arrays.fill(outputs, PENDING);
this.invocationSeed = invocationSeed;
streams = new RandomGenerator[graph.size()];
streams = new WorldRandom[graph.size()];
}
boolean isActive(int position) {
@@ -97,12 +97,12 @@ final class StageInvocation {
}
/** The running stage's own stream. A block stage keeps one stream across every block of its pass. */
RandomGenerator random() {
WorldRandom random() {
if (current == NO_STAGE) {
throw new IllegalStateException("A stage's random stream is only available while the stage runs");
}
if (streams[current] == null) {
streams[current] = StageRandom.stream(invocationSeed, graph.executable(current).idHash());
streams[current] = StageStreams.stream(invocationSeed, graph.executable(current).idHash());
}
return streams[current];
}
@@ -1,9 +1,8 @@
package com.volmit.host;
import com.volmit.random.WorldRandom;
import com.volmit.resource.ResourceId;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.random.RandomGenerator;
/** Derives the random stream of one stage in one invocation from the invocation seed and the
* stage id alone. A stage's draws therefore never depend on which other modules or stages are
@@ -14,33 +13,29 @@ import java.util.random.RandomGenerator;
* <li>{@code idHash} = 64-bit FNV-1a of the UTF-8 bytes of the stage id ({@code namespace:path});</li>
* <li>{@code streamSeed} = {@code mix(mix(invocationSeed) ^ idHash)}, where {@code mix} is the
* SplitMix64 finalizer (Stafford variant 13);</li>
* <li>the stream is {@code new java.util.Random(streamSeed)}.</li>
* <li>the stream is {@code new WorldRandom(streamSeed)}, whose every method IrisApi defines.</li>
* </ol>
* {@code mix} is a bijection, so one stage gets a different stream for every invocation seed, and
* within one invocation stages with different id hashes get different streams.
*
* <p>{@link java.util.Random} is the only JDK generator whose algorithm the Java SE specification
* fixes for {@code nextInt()}, {@code nextInt(bound)}, {@code nextLong()}, {@code nextBoolean()},
* {@code nextFloat()}, {@code nextDouble()}, {@code nextGaussian()} and {@code nextBytes}.
* {@code SplittableRandom} and the named {@code RandomGeneratorFactory} algorithms promise identical
* sequences only within one program. The ranged methods {@code Random} inherits from
* {@link RandomGenerator}, such as {@code nextInt(origin, bound)} or {@code nextDouble(bound)}, and
* {@code nextExponential()} are not fixed by the specification.
*/
public final class StageRandom {
public final class StageStreams {
private static final long FNV_OFFSET_BASIS = 0xcbf29ce484222325L;
private static final long FNV_PRIME = 0x100000001b3L;
private static final int BYTE_MASK = 0xff;
private StageRandom() {}
private StageStreams() {}
/** The stream {@code stageId} draws from in the invocation with {@code invocationSeed}. */
public static RandomGenerator stream(long invocationSeed, ResourceId stageId) {
public static WorldRandom stream(long invocationSeed, ResourceId stageId) {
return stream(invocationSeed, idHash(stageId));
}
static RandomGenerator stream(long invocationSeed, long idHash) {
return new Random(mix(mix(invocationSeed) ^ idHash));
static WorldRandom stream(long invocationSeed, long idHash) {
return new WorldRandom(streamSeed(invocationSeed, idHash));
}
static long streamSeed(long invocationSeed, long idHash) {
return mix(mix(invocationSeed) ^ idHash);
}
/** 64-bit FNV-1a over the UTF-8 bytes of the id. */