diff --git a/src/rand_impls.rs b/src/rand_impls.rs index 5a7e3de0b03..e2fc4027775 100644 --- a/src/rand_impls.rs +++ b/src/rand_impls.rs @@ -155,7 +155,13 @@ impl Rand for char { impl Rand for bool { #[inline] fn rand(rng: &mut R) -> bool { - rng.gen::() & 1 == 1 + // Generate a 32-bit value and test the sign. + // Many PRNGs exhibit non-uniform random quality across their bits. The + // low order bit of LCGs, for example, flips 0/1. Even some high + // quality PRNGs, like Xoroshiro128+, have strange low order bit + // behavior. Testing the sign offers protection against these common + // pathologies. + rng.gen::() < 0 } }