Skip to content

Fix modulo bias in lottery draws - #393

Open
shuvroroy wants to merge 1 commit into
laravel:0.xfrom
shuvroroy:fix/lottery-modulo-bias
Open

shuvroroy wants to merge 1 commit into
laravel:0.xfrom
shuvroroy:fix/lottery-modulo-bias

Conversation

@shuvroroy

Copy link
Copy Markdown
Contributor

For Lottery::odds(1_000_000_000, 3_000_000_000), the old draw counted both 0 through 999,999,999 and 3,000,000,000 through 3,999,999,999 as wins. Together they cover 46.57% of all u32 values, well above the requested one third.

Retry samples in the final, incomplete group of out_of values. Each remainder then occurs equally often among the accepted samples. The regression test supplies the first value in that incomplete group, followed by u32::MAX and the values on either side of the win boundary.

Verification

  • Confirmed the regression test fails with the original modulo calculation.
  • All 350 core unit tests and all 5 lottery integration tests passed.
  • Workspace formatting and Clippy for the core library passed.
  • A separate run of 100,000 draws through Lottery::draw produced 33,438 wins after the fix.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant