DEV Community

Seed Map
Seed Map

Posted on

Predicting Minecraft Slime Chunks from a Seed in Python

Slime farms in Minecraft Java Edition depend on slime chunks: about one chunk in ten where slimes can spawn underground regardless of biome. Which chunks qualify is not random at runtime. It is a pure function of the world seed and the chunk coordinates, built on Java's java.util.Random. That makes it a nice small exercise in reimplementing a JVM random number generator in Python.

The rule

The check that circulates widely in the community, and that seed map tools reproduce, looks like this in Java:

Random rnd = new Random(
    seed +
    (int) (x * x * 0x4c1906) +
    (int) (x * 0x5ac0db) +
    (int) (z * z) * 0x4307a7L +
    (int) (z * 0x5f24f) ^ 0x3ad8025fL
);
boolean isSlimeChunk = rnd.nextInt(10) == 0;
Enter fullscreen mode Exit fullscreen mode

Here x and z are chunk coordinates (block coordinate divided by 16, rounded down), not block coordinates. This applies to Java Edition; Bedrock uses a different method.

The tricky parts

Two Java behaviours have to be reproduced exactly:

  1. 32-bit int overflow. (int)(x * x * 0x4c1906) is computed in 32-bit arithmetic and wraps around. Python integers never overflow, so we have to wrap manually.
  2. java.util.Random. It is a 48-bit linear congruential generator with a specific scrambling of the seed and a specific way of producing nextInt(bound).

The Python port

MASK48 = (1 << 48) - 1

def i32(v):
    v &= 0xFFFFFFFF
    return v - (1 << 32) if v >= 1 << 31 else v

def i64(v):
    v &= (1 << 64) - 1
    return v - (1 << 64) if v >= 1 << 63 else v

class JavaRandom:
    def __init__(self, seed):
        self.s = (seed ^ 0x5DEECE66D) & MASK48

    def next(self, bits):
        self.s = (self.s * 0x5DEECE66D + 0xB) & MASK48
        return i32(self.s >> (48 - bits))

    def next_int(self, bound):
        if bound & -bound == bound:  # power of two
            return i32((bound * self.next(31)) >> 31)
        while True:
            bits = self.next(31)
            val = bits % bound
            if bits - val + (bound - 1) < (1 << 31):
                return val

def is_slime_chunk(seed, cx, cz):
    s = i64(
        seed
        + i32(cx * cx * 0x4c1906)
        + i32(cx * 0x5ac0db)
        + i64(i32(cz * cz) * 0x4307a7)
        + i32(cz * 0x5f24f)
    ) ^ 0x3ad8025f
    return JavaRandom(s).next_int(10) == 0
Enter fullscreen mode Exit fullscreen mode

Note the operator precedence: in Java, + binds tighter than ^, so the XOR applies to the whole sum. That is easy to get wrong when porting.

Scanning an area

def slime_chunks(seed, radius=8):
    for cx in range(-radius, radius + 1):
        for cz in range(-radius, radius + 1):
            if is_slime_chunk(seed, cx, cz):
                yield cx * 16, cz * 16  # block coords of the chunk corner

for x, z in slime_chunks(12345):
    print(x, z)
Enter fullscreen mode Exit fullscreen mode

Around 10 percent of chunks should come back, which is a quick sanity check on the port.

Verifying against a map

Before you build a farm on the result, check a few chunks against an existing tool. Pick a seed, compare your list with a visual map, and confirm in-game with F3 chunk borders. For finding seeds that put slime chunks, villages and other features close to spawn, I use a seed finder and then run scripts like this one to plan builds in detail.

Reimplementing java.util.Random is a small project, but it unlocks a lot: the same generator sits behind many other seed-based checks in older versions of the game.

Top comments (0)