From aa4181c85d2930b981501b054f7bda78ad8a9df5 Mon Sep 17 00:00:00 2001 From: TheIronBorn Date: Tue, 11 Dec 2018 00:17:23 +0000 Subject: [PATCH] fix https://github.com/rust-random/rand/issues/661 --- src/distributions/uniform.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/distributions/uniform.rs b/src/distributions/uniform.rs index 5fb89e34053..074dafc88c5 100644 --- a/src/distributions/uniform.rs +++ b/src/distributions/uniform.rs @@ -452,8 +452,9 @@ macro_rules! uniform_int_impl { let ints_to_reject = (unsigned_max - range + 1) % range; unsigned_max - ints_to_reject } else { - // conservative but fast approximation - range << range.leading_zeros() + // conservative but fast approximation. `- 1` is necessary to allow the + // same comparison without bias. + (range << range.leading_zeros()).wrapping_sub(1) }; loop {