Skip to content

Fix modulo bias in device Random_int rejection sampling#5545

Merged
WeiqunZhang merged 1 commit into
AMReX-Codes:developmentfrom
WeiqunZhang:random_int_bias
Jul 8, 2026
Merged

Fix modulo bias in device Random_int rejection sampling#5545
WeiqunZhang merged 1 commit into
AMReX-Codes:developmentfrom
WeiqunZhang:random_int_bias

Conversation

@WeiqunZhang

Copy link
Copy Markdown
Member

The CUDA/HIP device path of Random_int used rand > (RAND_M - RAND_M % n) as its rejection bound, which accepts L+1 values where L is the largest multiple of n ≤ RAND_M. The extra value L maps to residue 0, biasing the output toward 0 — up to ~2× for large n (e.g., n = 2^31).

Change > to >= so the accepted set is exactly {0, …, L-1}, a perfect multiple of n, giving a uniform distribution.

The CUDA/HIP device path of Random_int used `rand > (RAND_M - RAND_M % n)`
as its rejection bound, which accepts L+1 values where L is the largest
multiple of n ≤ RAND_M.  The extra value L maps to residue 0, biasing the
output toward 0 — up to ~2× for large n (e.g., n = 2^31).

Change `>` to `>=` so the accepted set is exactly {0, …, L-1}, a perfect
multiple of n, giving a uniform distribution.
@WeiqunZhang WeiqunZhang requested a review from atmyers July 8, 2026 01:35
@WeiqunZhang WeiqunZhang merged commit 8baee78 into AMReX-Codes:development Jul 8, 2026
73 checks passed
@WeiqunZhang WeiqunZhang deleted the random_int_bias branch July 8, 2026 19:30
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.

2 participants