From 3931fba140bfbc85b4c66d2298d8935839925f03 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Thu, 7 Dec 2017 20:35:58 +0100 Subject: [PATCH] Use a sign test for bools --- src/distributions/uniform.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/distributions/uniform.rs b/src/distributions/uniform.rs index 5dc9fc54cfb..fce3b6c9746 100644 --- a/src/distributions/uniform.rs +++ b/src/distributions/uniform.rs @@ -184,7 +184,11 @@ impl Distribution for Uniform { impl Distribution for Uniform { #[inline] fn sample(&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 } }