Skip to content

Commit

Permalink
Use a sign test for bools
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Dec 7, 2017
1 parent cb0f3f8 commit 3931fba
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/distributions/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ impl Distribution<u128> for Uniform {
impl Distribution<bool> for Uniform {
#[inline]
fn sample<R: Rng+?Sized>(&self, rng: &mut R) -> bool {
rng.next_u32() & 1 == 1
// We can compare against an arbitrary bit of an u32 to get a bool.
// Because the least significant bits of a lower quality RNG can have
// simple patterns, we compare against the most significant bit. This is
// easiest done using a sign test.
(rng.next_u32() as i32) < 0
}
}

Expand Down

0 comments on commit 3931fba

Please sign in to comment.