Skip to content

Fix PCGRandom.integer() bias: use Lemire rejection sampling#6201

Open
Zelys-DFKH wants to merge 1 commit intoEffect-TS:mainfrom
Zelys-DFKH:fix/pcg-random-integer-bias
Open

Fix PCGRandom.integer() bias: use Lemire rejection sampling#6201
Zelys-DFKH wants to merge 1 commit intoEffect-TS:mainfrom
Zelys-DFKH:fix/pcg-random-integer-bias

Conversation

@Zelys-DFKH
Copy link
Copy Markdown

Summary

PCGRandom.integer() used Math.round(this.number() * Number.MAX_SAFE_INTEGER) % max, which baked two sources of statistical bias into one expression and consumed twice the PRNG state per call.

Closes #6184

ryanleecode's comment in the thread spotted and quantified the bias clearly. I wanted to make sure a fix landed.

Problem

Math.round boundary bias: Math.round maps [0, 1) to integers non-uniformly. Buckets at each end of the range receive half the probability mass of interior values.

Modulo bias: Number.MAX_SAFE_INTEGER is not evenly divisible by most values of max, so some output values have one extra preimage in the modulo cycle and are overrepresented.

Double state consumption: number() calls _next() twice to build a 53-bit float. Integer generation only needs one 32-bit draw.

The practical bias magnitude is tiny (ryanleecode's analysis put it at ~10^-16), but a PRNG that is correct-by-construction is worth having.

Fix

For max <= 2^32: Lemire (2018) rejection sampling on raw 32-bit output (arXiv:1805.10941). Same approach as OpenBSD's arc4random_uniform and the PCG reference implementation. Every value in [0, max) gets exactly floor((2^32 - threshold) / max) accepting preimages, provably uniform.

For max > 2^32 (e.g. nextInt passing Number.MAX_SAFE_INTEGER): falls back to Math.floor(this.number() * max). Math.floor removes the Math.round boundary bias; residual floating-point discretization bias at that scale is negligible.

Tests

  • Bounds test: nextIntBetween stays in [min, max) across several small ranges, where rejection sampling is most aggressively exercised.
  • Deterministic test against a known seed to catch regressions in output sequence.

@Zelys-DFKH Zelys-DFKH requested a review from mikearnaldi as a code owner April 28, 2026 23:55
@github-project-automation github-project-automation Bot moved this to Discussion Ongoing in PR Backlog Apr 28, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 28, 2026

🦋 Changeset detected

Latest commit: 53025fd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 36 packages
Name Type
effect Patch
@effect/cli Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/platform Patch
@effect/printer-ansi Patch
@effect/printer Patch
@effect/rpc Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-drizzle Patch
@effect/sql-kysely Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/sql Patch
@effect/typeclass Patch
@effect/vitest Patch
@effect/workflow Patch
@effect/ai Patch
@effect/ai-amazon-bedrock Patch
@effect/ai-anthropic Patch
@effect/ai-google Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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

Labels

None yet

Projects

Status: Discussion Ongoing

Development

Successfully merging this pull request may close these issues.

PCG Random.integer() uses biased Math.round + modulo instead of rejection sampling

1 participant