Every Java Edition world has a list of "slime chunks": 16x16 areas where slimes can spawn underground at any light level. Players build slime farms inside them, and seed tools mark them on maps. The check is a small, well-known formula built on java.util.Random, and it makes a nice exercise in reproducing Java's integer behaviour in another language. Here it is in Python and JavaScript.
The formula
For a world seed seed and chunk coordinates (x, z), Java Edition does roughly this:
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;
Two details matter:
- Some products are cast to
int(32-bit, wrapping on overflow) before being added to along. - The final value seeds Java's linear congruential generator, and we only need its first
nextInt(10).
Reimplementing Java's Random
MASK48 = (1 << 48) - 1
MULT = 0x5DEECE66D
def to_int32(v):
v &= 0xFFFFFFFF
return v - (1 << 32) if v >= 1 << 31 else v
def to_int64(v):
v &= (1 << 64) - 1
return v - (1 << 64) if v >= 1 << 63 else v
class JavaRandom:
def __init__(self, seed):
self.seed = (seed ^ MULT) & MASK48
def next(self, bits):
self.seed = (self.seed * MULT + 0xB) & MASK48
return to_int32(self.seed >> (48 - bits))
def next_int(self, bound):
if bound & -bound == bound: # power of two
return (bound * self.next(31)) >> 31
while True:
bits = self.next(31)
val = bits % bound
if bits - val + (bound - 1) < (1 << 31):
return val
The slime chunk check
def is_slime_chunk(seed, x, z):
s = (seed
+ to_int32(x * x * 0x4c1906)
+ to_int32(x * 0x5ac0db)
+ to_int32(z * z) * 0x4307a7
+ to_int32(z * 0x5f24f)) ^ 0x3ad8025f
return JavaRandom(to_int64(s)).next_int(10) == 0
# chunks around the origin
for z in range(-2, 3):
print("".join("#" if is_slime_chunk(12345, x, z) else "." for x in range(-8, 9)))
Note that (int)(z * z) * 0x4307a7L multiplies the 32-bit result by a long, so the second product is not wrapped to 32 bits. Getting that one cast wrong is the most common reason ports disagree with the game.
JavaScript version
JavaScript numbers are doubles, so use BigInt for the 48-bit and 64-bit parts:
const MASK48 = (1n << 48n) - 1n, MULT = 0x5DEECE66Dn;
const i32 = (v) => BigInt.asIntN(32, BigInt(v));
const i64 = (v) => BigInt.asIntN(64, v);
function isSlimeChunk(seed, x, z) {
x = BigInt(x); z = BigInt(z);
let s = BigInt(seed) + i32(x * x * 0x4c1906n) + i32(x * 0x5ac0dbn)
+ i32(z * z) * 0x4307a7n + i32(z * 0x5f24fn);
s = i64(s ^ 0x3ad8025fn);
let state = (s ^ MULT) & MASK48;
const next = (bits) => {
state = (state * MULT + 0xBn) & MASK48;
return Number(BigInt.asIntN(32, state >> BigInt(48 - bits)));
};
// bound 10 is not a power of two
while (true) {
const bits = next(31), val = bits % 10;
if (bits - val + 9 < 2 ** 31) return val === 0;
}
}
Checking your result
Compare a few chunks with the game: stand in a chunk (F3 shows chunk coordinates), and test whether slimes spawn below Y=40 in a dark, flat room. Or compare with an existing tool; a Minecraft seed map for Java Edition lets you load the same seed and see whether your output lines up with what the map shows for that area.
Caveats
- This applies to Java Edition. Bedrock uses a different method.
- Slime chunks never apply in mushroom fields, and swamps have their own surface spawning rules.
- The formula has been stable for many versions, but always verify after a major update.
Top comments (0)