Skip to content

Commit 0f3d818

Browse files
committed
Generate bool using sign test
1 parent 584e0e8 commit 0f3d818

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/distributions/other.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ impl Distribution<char> for Uniform {
3333
impl Distribution<bool> for Uniform {
3434
#[inline]
3535
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> bool {
36-
rng.gen::<u8>() & 1 == 1
36+
// We can compare against an arbitrary bit of an u32 to get a bool.
37+
// Because the least significant bits of a lower quality RNG can have
38+
// simple patterns, we compare against the most significant bit. This is
39+
// easiest done using a sign test.
40+
(rng.next_u32() as i32) < 0
3741
}
3842
}
3943

0 commit comments

Comments
 (0)