[GH-ISSUE #2943] [Issue]: {{random:X:Y}} with a reversed range and {{roll:Xd0}} produce wrong numbers #1519

Closed
opened 2026-10-21 05:37:52 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2943

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

{{random:X:Y}} and {{roll:XdY}} produce wrong numbers for a couple of common mistakes, silently, instead of handling them sensibly:

  • If the range is backwards, e.g. {{random:5:1}}, you get a number outside the range you asked for (somewhere around 2–5), not a number between 1 and 5.
  • {{roll:Xd0}} (zero-sided dice) just returns the dice count instead of 0 or an error — e.g. {{roll:2d0}} → 2.

These are small, but for a dice/random feature they give quietly incorrect results.

How to reproduce (try it yourself)

Put these in a message or character field and look at the output:

  • {{random:5:1}} → You'll see a number in roughly 2–5. Expected: a number between 1 and 5 (order shouldn't matter).
  • {{roll:2d0}} → You'll see 2. Expected: 0 (or treat 0 sides as invalid).

Where it is

packages/shared/src/utils/macro-engine.ts — {{random:X:Y}} at line 919 computes Math.floor(Math.random() * (hi - lo + 1)) + lo with no check that hi >= lo; {{roll}} at line 926 computes Math.floor(Math.random() * s) + 1 per die, which is 1 when s is 0.

What's happening

The handlers don't normalize/validate their inputs: a reversed range makes (hi - lo + 1) negative, and s = 0 makes each die a guaranteed 1.

Suggested fix

In {{random:X:Y}}, swap the bounds if hi < lo (use min/max). In {{roll}}, treat sides < 1 as invalid (return 0 or leave the macro unresolved). (Clamping the upper bounds is also covered by the separate DoS issue.)


Found by a code review of v2.0.6; please verify the reproductions before fixing.

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2943 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** `{{random:X:Y}}` and `{{roll:XdY}}` produce wrong numbers for a couple of common mistakes, silently, instead of handling them sensibly: - If the range is backwards, e.g. `{{random:5:1}}`, you get a number outside the range you asked for (somewhere around 2–5), not a number between 1 and 5. - `{{roll:Xd0}}` (zero-sided dice) just returns the dice count instead of 0 or an error — e.g. `{{roll:2d0}}` → `2`. These are small, but for a dice/random feature they give quietly incorrect results. **How to reproduce (try it yourself)** Put these in a message or character field and look at the output: - `{{random:5:1}}` → **You'll see** a number in roughly 2–5. **Expected:** a number between 1 and 5 (order shouldn't matter). - `{{roll:2d0}}` → **You'll see** `2`. **Expected:** `0` (or treat 0 sides as invalid). **Where it is** `packages/shared/src/utils/macro-engine.ts` — `{{random:X:Y}}` at line 919 computes `Math.floor(Math.random() * (hi - lo + 1)) + lo` with no check that `hi >= lo`; `{{roll}}` at line 926 computes `Math.floor(Math.random() * s) + 1` per die, which is `1` when `s` is 0. **What's happening** The handlers don't normalize/validate their inputs: a reversed range makes `(hi - lo + 1)` negative, and `s = 0` makes each die a guaranteed `1`. **Suggested fix** In `{{random:X:Y}}`, swap the bounds if `hi < lo` (use `min`/`max`). In `{{roll}}`, treat `sides < 1` as invalid (return `0` or leave the macro unresolved). (Clamping the upper bounds is also covered by the separate DoS issue.) --- *Found by a code review of v2.0.6; please verify the reproductions before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 28, 2026):

Addressed in the staging fixes: {{random:X:Y}} now normalizes reversed bounds before rolling, {{roll:Xd0}} returns 0 instead of adding one per die, and dice count/sides are capped to avoid the obvious runaway loop case. Regression coverage was added for the reversed range and zero-sided dice behavior.

<!-- gh-comment-id:4827517852 --> @SpicyMarinara commented on GitHub (Jun 28, 2026): Addressed in the staging fixes: {{random:X:Y}} now normalizes reversed bounds before rolling, {{roll:Xd0}} returns 0 instead of adding one per die, and dice count/sides are capped to avoid the obvious runaway loop case. Regression coverage was added for the reversed range and zero-sided dice behavior.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1519
No description provided.