roll-parser - v3.4.0
    Preparing search index...

    Function createMockRng

    • Creates a mock RNG that hands out predefined values in order — the way to write dice tests with exact expected totals.

      Two deliberate strictnesses, both there to surface a miscounted sequence instead of hiding it:

      • It never wraps around. Running out throws MockRNGExhaustedError.
      • nextInt rejects a scripted value outside the requested [min, max] with a RangeError, so createMockRng([7]) cannot satisfy a d6.

      Both are the only failures that reach a caller of roll() without a roll-parser code, and only ever when a mock was injected — see MockRNGExhaustedError for why they stay outside the hierarchy.

      Draw order matters when the notation contains meta-expressions. Keep/drop counts (4d6kh(1d2)) are drawn before the pool; threshold expressions (4d6cs>(1d2)) are drawn after it. The README's Randomness section has the full tables.

      Parameters

      • values: number[]

        Values to return, in draw order (die faces for nextInt, floats in [0, 1) for next)

      Returns RNG

      An RNG that replays values

      import { createMockRng } from 'roll-parser/testing';

      const rng = createMockRng([4, 2, 6]);
      rng.nextInt(1, 6); // 4
      rng.nextInt(1, 6); // 2
      rng.nextInt(1, 6); // 6
      rng.nextInt(1, 6); // throws MockRNGExhaustedError
      import { roll } from 'roll-parser';
      import { createMockRng } from 'roll-parser/testing';

      const result = roll('4d6kh3', { rng: createMockRng([3, 6, 2, 5]) });
      result.total; // 14
      result.rendered; // '4d6[3, 6, ~~2~~, 5] = 14'