Minecraft does not scatter structures completely at random. In Java Edition, most structures use a "random spread" placement: the world is divided into square regions, and each region gets at most one attempt at that structure. Once you understand this, you can predict where a structure can appear without generating any terrain. This post walks through the idea and a small JavaScript implementation.
Spacing, separation and salt
Each structure set has three numbers:
| Structure | Spacing (chunks) | Separation (chunks) | Salt |
|---|---|---|---|
| Village | 34 | 8 | 10387312 |
| Desert pyramid | 32 | 8 | 14357617 |
| Pillager outpost | 32 | 8 | 165745296 |
| Woodland mansion | 80 | 20 | 10387319 |
- Spacing is the size of a region, in chunks.
- Separation is the minimum gap. The attempt is placed somewhere in the first
spacing - separationchunks of the region, so two neighbouring attempts are never closer thanseparationchunks. - Salt makes each structure type use a different random sequence, so villages and pyramids do not always land on the same chunk.
These values come from the vanilla data files and can change between versions, so always check them against the version you target.
A Java-compatible random generator
The game uses java.util.Random, a 48-bit linear congruential generator. JavaScript numbers cannot hold 64-bit products exactly, so use BigInt:
const MULT = 0x5DEECE66Dn;
const MASK = (1n << 48n) - 1n;
class JavaRandom {
constructor(seed) { this.seed = (BigInt(seed) ^ MULT) & MASK; }
next(bits) {
this.seed = (this.seed * MULT + 0xBn) & MASK;
return Number(BigInt.asIntN(32, this.seed >> BigInt(48 - bits)));
}
nextInt(bound) {
if ((bound & -bound) === bound) {
return Number((BigInt(bound) * BigInt(this.next(31))) >> 31n);
}
let bits, val;
do {
bits = this.next(31);
val = bits % bound;
} while (((bits - val + (bound - 1)) | 0) < 0);
return val;
}
}
The | 0 in the loop reproduces Java's 32-bit integer overflow check, which rejects a small biased range of values.
Finding the candidate chunk for a region
function regionSeed(worldSeed, regionX, regionZ, salt) {
return BigInt.asIntN(64,
BigInt(regionX) * 341873128712n +
BigInt(regionZ) * 132897987541n +
BigInt(worldSeed) + BigInt(salt));
}
function candidateChunk(worldSeed, regionX, regionZ, { spacing, separation, salt, triangular = false }) {
const rng = new JavaRandom(regionSeed(worldSeed, regionX, regionZ, salt));
const range = spacing - separation;
const pick = () => triangular
? Math.floor((rng.nextInt(range) + rng.nextInt(range)) / 2)
: rng.nextInt(range);
const offX = pick();
const offZ = pick();
return { chunkX: regionX * spacing + offX, chunkZ: regionZ * spacing + offZ };
}
Woodland mansions use the triangular spread (two random numbers averaged), which pulls attempts towards the middle of each region. Most other structures use the linear version.
Turning a block position into nearby candidates
const floorDiv = (a, b) => Math.floor(a / b);
function nearbyCandidates(worldSeed, blockX, blockZ, cfg, radius = 1) {
const rx = floorDiv(floorDiv(blockX, 16), cfg.spacing);
const rz = floorDiv(floorDiv(blockZ, 16), cfg.spacing);
const out = [];
for (let dx = -radius; dx <= radius; dx++) {
for (let dz = -radius; dz <= radius; dz++) {
const c = candidateChunk(worldSeed, rx + dx, rz + dz, cfg);
out.push({ x: c.chunkX * 16 + 8, z: c.chunkZ * 16 + 8 });
}
}
return out.sort((a, b) =>
Math.hypot(a.x - blockX, a.z - blockZ) - Math.hypot(b.x - blockX, b.z - blockZ));
}
Use Math.floor rather than Math.trunc or bit shifts on the division: negative coordinates must round down, or every region west or north of the origin will be off by one.
The catch: candidates are not structures
A candidate chunk is only an attempt. The structure is generated only if the biome at that spot allows it (a mansion needs a dark forest, a desert pyramid needs a desert) and, for some structures, if other checks pass. Villages and outposts also interact: an outpost will not generate too close to a village. So a full finder has to run the biome generator too, and that is the expensive part.
That is why it is easier to check results on a finished tool. For example, you can locate a Woodland Mansion on the seed map and compare the markers with the candidate chunks your code produces for the same seed and version. If your candidates line up with real mansions in dark forests, your random generator and region maths are correct.
Where to go next
- Add a biome lookup (or a lookup against exported biome data) to filter candidates.
- Cache region results: the same region is queried over and over while a user pans a map.
- Handle concentric-ring placement separately. Strongholds do not use random spread at all; they are placed in rings around the origin.
Comments
Join the discussionNo comments yet. Be the first to comment.