We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 584e0e8 commit 0f3d818Copy full SHA for 0f3d818
src/distributions/other.rs
@@ -33,7 +33,11 @@ impl Distribution<char> for Uniform {
33
impl Distribution<bool> for Uniform {
34
#[inline]
35
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> bool {
36
- rng.gen::<u8>() & 1 == 1
+ // 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
41
}
42
43
0 commit comments