Spielerando

The Game Freezes When Loot Drops

When the game stops responding at the moment a chest opens or an enemy drops loot, look at the loop that rolls the loot. A fair roll draws a random number and draws again whenever the number falls in the uneven rest at the top; if no number can ever fit, that loop never ends. Z3 found the kind of table that did this to our own loot table, and we have since made the roll refuse it.

What you see

Why does the roll never end?

Our loot table rolls with a random generator that gives 4,294,967,296 different numbers (2 to the power of 32). To keep every item's chance exact, it throws away a number that falls past the last whole multiple of the total weight and draws again. If the total weight is larger than all the numbers the generator has, every number falls past that point, and the redraw goes on for ever.

The check that refuses a broken table when it loads did not look at the total. Z3 asked whether any table the rules allow keeps no number at all, and answered with one whose weights add up to more than 4,294,967,296. While we wrote this page we found a second gap: the roll itself trusted that check, so a game that skipped it still froze. The roll now refuses such a table too.

How to check your game for it

A test cannot wait for ever, so it uses a stand-in generator that gives up after 1000 draws and says so. The first test hands the table check a table whose weights add up past the generator's numbers. The second asks for a roll on such a table and expects a clear error, not an endless loop. The third makes sure a table at the limit still works and its roll still draws again when it has to.

The test: fix.test.ts

TypeScript for Node's own test runner. The tests named below as the catch are the ones that fail on the bug.

import assert from "node:assert/strict";
import test from "node:test";
import { rollBelow, SPAN, tableProblems } from "./fix.ts";

/** A stand-in generator that gives the same number every time, and gives up after 1000 draws, so
    a roll that would never end fails here instead of freezing the test. */
function stuckGenerator() {
  let draws = 0;
  return () => {
    draws++;
    if (draws > 1000) throw new Error("1000 numbers drawn and none kept: in the game this roll would never end");
    return 123456789;
  };
}

test("a table whose weights add up past the generator's numbers is refused when it loads", () => {
  const problems = tableProblems([{ id: "sword", weight: SPAN }, { id: "shield", weight: 1 }]);
  assert.equal(problems.length, 1, "the table was accepted");
});

test("a roll on such a table stops with an error instead of freezing", () => {
  let error: unknown = null;
  try {
    rollBelow(SPAN + 1, stuckGenerator());
  } catch (e) {
    error = e;
  }
  assert.ok(error instanceof RangeError, `the roll ended with: ${error instanceof Error ? error.message : "a result"}`);
});

test("a table up to the generator's numbers is fine, and its roll is fair and ends", () => {
  assert.deepEqual(tableProblems([{ id: "sword", weight: SPAN - 1 }, { id: "shield", weight: 1 }]), []);
  // 2^32 is 1 more than a multiple of 3, so the last number is drawn again
  const numbers = [SPAN - 1, 7];
  assert.equal(rollBelow(3, () => numbers.shift()!), 1);
});

The catch:

What it said on the code with the bug put back

✖ a table whose weights add up past the generator's numbers is refused when it loads
    the table was accepted
    got 0, expected 1
✖ a roll on such a table stops with an error instead of freezing
    the roll ended with: 1000 numbers drawn and none kept: in the game this roll would never end
✔ a table up to the generator's numbers is fine, and its roll is fair and ends
3 tests: 1 passed, 2 failed

What it said on the fixed code

✔ a table whose weights add up past the generator's numbers is refused when it loads
✔ a roll on such a table stops with an error instead of freezing
✔ a table up to the generator's numbers is fine, and its roll is fair and ends
3 tests: 3 passed, 0 failed

Run by pnpm fixes:check on 2026-11-26 with Node v24.15.0. The text above is its output.

The fix

Check every table when the game loads it: whole weights of 0 or more, a total above 0, and a total no larger than the numbers your generator gives. And make the roll refuse what it cannot serve with an error, instead of trusting that every table went through the check. An error names the table; a freeze names nothing.

What changed

Lines with - are the code with the bug, lines with + the fix.

    const total = entries.reduce((sum, e) => sum + e.weight, 0);
    if (total <= 0) problems.push("the table has no weight");
+   if (total > SPAN) problems.push(`the weights add up to ${total}, more than the generator's ${SPAN} numbers`);
    return problems;
  }
…
      the last whole multiple of `limit` are drawn again, so no result comes up more often. */
  export function rollBelow(limit: number, next: () => number): number {
+   // With a limit past the generator's numbers no draw is ever kept: refuse instead of freezing.
+   if (!(limit >= 1 && limit <= SPAN)) throw new RangeError(`no fair roll below ${limit} from ${SPAN} numbers`);
    const usable = SPAN - (SPAN % limit);
    for (;;) {

The fixed code: fix.ts

/* A fair loot roll draws a random whole number and, when it falls past the last whole multiple of
   the table's total weight, draws again, so no item is favoured. That redraw has to be able to end:
   a 32-bit generator gives 4,294,967,296 numbers, and a table whose weights add up to more keeps
   none of them. From the loot table blueprint:
   https://spielerando.com/en/mechanics/loot-table */

export interface Entry {
  id: string;
  /** A whole number. Zero means the entry never drops. */
  weight: number;
}

/** How many numbers the generator gives: 0 up to 2^32 - 1. */
export const SPAN = 2 ** 32;

/** What makes a table unusable; empty when it is fine. Run it when the game loads its tables. */
export function tableProblems(entries: Entry[]): string[] {
  const problems: string[] = [];
  for (const e of entries) if (!Number.isInteger(e.weight) || e.weight < 0) problems.push(`"${e.id}" needs a whole weight of 0 or more`);
  const total = entries.reduce((sum, e) => sum + e.weight, 0);
  if (total <= 0) problems.push("the table has no weight");
  if (total > SPAN) problems.push(`the weights add up to ${total}, more than the generator's ${SPAN} numbers`);
  return problems;
}

/** A fair whole number from 0 up to `limit`, from the 32-bit numbers `next` draws. Numbers past
    the last whole multiple of `limit` are drawn again, so no result comes up more often. */
export function rollBelow(limit: number, next: () => number): number {
  // With a limit past the generator's numbers no draw is ever kept: refuse instead of freezing.
  if (!(limit >= 1 && limit <= SPAN)) throw new RangeError(`no fair roll below ${limit} from ${SPAN} numbers`);
  const usable = SPAN - (SPAN % limit);
  for (;;) {
    const drawn = next();
    if (drawn < usable) return drawn % limit;
  }
}

Paste this to your AI

My game freezes at the moment loot is rolled (a chest opens, an enemy dies, a reward appears). I think the loop that draws the random number can never end for one of my tables.

1. Before you change anything, find every loop in my game that repeats until a random number fits: loot rolls, spawn points, shuffles. For each, write down when it ends, and which tables or settings could stop it from ever ending: a total weight of 0, weights that are not whole numbers, or a total larger than the numbers my random generator gives.
2. Write tests like the ones below for my code. Give them a stand-in generator that gives up after 1000 draws, so a loop that would never end fails the test instead of freezing it. Run them and paste me the real output.
3. Fix what fails the way the code below does: check every table when the game loads it, and make the roll refuse a table it cannot serve instead of looping. Run the tests again and paste the output.
4. Take the check on the total out again for a moment, show me that the test fails instead of hanging, and put it back.
5. Run the check on every loot table in my game's data and list the ones it refuses.

Do not tell me it is fixed unless the test output shows it.

The button copies this text together with the fixed code and the test above, so your AI has everything.

Where we found it

The blueprint the bug was in, and the test that guards it there:

How we found them: the bugs our proofs found in our own code

Was this page helpful?

One click, no account. It shows us which pages help.

Next step

Fix by symptom

The Quest Is Done Before the Player Did Anything

A quest that counts as done the moment it starts usually has an empty list of goals: "every goal is met" says yes to an empty list. The test and the fix.

Last updated: 2026-11-26