Skip to content

Fix randomInt rounding at the upper bound - #52

Open
Zhuyuyangyy wants to merge 1 commit into
d3:mainfrom
Zhuyuyangyy:fix/randomint-exclusive-upper-bound-20261031
Open

Zhuyuyangyy wants to merge 1 commit into
d3:mainfrom
Zhuyuyangyy:fix/randomint-exclusive-upper-bound-20261031

Conversation

@Zhuyuyangyy

Copy link
Copy Markdown

Fixes #51 by applying the floor-first ordering suggested in the report.

When a legal source returns the largest double below one, adding an integer offset before flooring can round the sum up to the excluded upper bound. For example:

randomInt.source(() => 1 - 2 ** -53)(-3, -2)(); // -2, but should be -3

The same problem occurs with positive intervals and large safe-integer offsets. The production change is one line: floor the sampled range offset first, then add the integer lower bound. Bounds are normalized as before, and the source is still called once per sample. This does not add parameter validation or attempt to solve Number precision limitations outside safe-integer bounds.

Six deterministic tests cover negative and positive intervals, a large offset, the single-argument form, fractional-bound normalization, and legal source values at zero, near one, and a binary midpoint. No versions, dependency declarations, lockfiles, or generated distributions are changed.

Local validation

On Node 24.19.0 with Mocha 8.4.0, ESLint 7.28.0, and d3-array 2.12.1 (within the declared ranges):

  • Unmodified baseline: 67 passing, ESLint passes.
  • The new tests against the old implementation: 4 failing, 69 passing; both control cases pass.
  • npm test: 73 passing, ESLint passes.
  • 2,040 deterministic controls retain their baseline outputs.
  • 40 near-one source boundary checks return the expected last integer and call the source exactly once; these include 28 cases where the baseline returned the excluded maximum.
  • git diff --check: passes.

These are local results, not a claim that upstream CI has passed. Other Node versions and real-browser runtimes were not tested.

AI assistance

This change and its tests were prepared with AI assistance on behalf of the account owner. It has not received an independent human review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

randomInt can return max

1 participant