From a881a25eeb9593348f5bc943a969bd8d44b1fab2 Mon Sep 17 00:00:00 2001 From: TheIronBorn Date: Wed, 8 Nov 2017 05:42:41 +0000 Subject: [PATCH 1/2] reroll if `Rand::rand` gives a zero seed Currently there's a chance a seed of all zeros could be produced by the other RNG. A zero seed will never produce anything but zero because of the way the RNG works and notes that "The state must be seeded so that it is not everywhere zero." --- src/xoroshiro128.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/xoroshiro128.rs b/src/xoroshiro128.rs index f5b5232..8693db7 100644 --- a/src/xoroshiro128.rs +++ b/src/xoroshiro128.rs @@ -96,8 +96,10 @@ impl<'a> SeedableRng<&'a [u64]> for Xoroshiro128 { impl Rand for Xoroshiro128 { fn rand(other: &mut R) -> Xoroshiro128 { let mut key: [u64; STATE_SIZE] = [0; STATE_SIZE]; - for word in &mut key { - *word = other.gen(); + while key.iter().all(|&x| x == 0) { + for word in &mut key { + *word = other.gen(); + } } SeedableRng::from_seed(&key[..]) } From 5b80a8fd5d0bc06dbf781c4377d92eeaabe47998 Mon Sep 17 00:00:00 2001 From: TheIronBorn Date: Wed, 8 Nov 2017 05:55:19 +0000 Subject: [PATCH 2/2] cleanup --- src/xoroshiro128.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/xoroshiro128.rs b/src/xoroshiro128.rs index 8693db7..1f891f3 100644 --- a/src/xoroshiro128.rs +++ b/src/xoroshiro128.rs @@ -97,9 +97,7 @@ impl Rand for Xoroshiro128 { fn rand(other: &mut R) -> Xoroshiro128 { let mut key: [u64; STATE_SIZE] = [0; STATE_SIZE]; while key.iter().all(|&x| x == 0) { - for word in &mut key { - *word = other.gen(); - } + key = other.gen(); } SeedableRng::from_seed(&key[..]) }