Skip to content

Commit

Permalink
Merge pull request rust-random#791 from vks/zero-seed
Browse files Browse the repository at this point in the history
Use SplitMix to seed 64-bit generators
  • Loading branch information
dhardy committed May 7, 2019
2 parents 65b8198 + faf4fec commit 01343e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
11 changes: 7 additions & 4 deletions rand_xoshiro/src/xoroshiro64star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use byteorder::{ByteOrder, LittleEndian};
use rand_core;
use rand_core::le::read_u32_into;
use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32};
Expand Down Expand Up @@ -71,9 +70,7 @@ impl SeedableRng for Xoroshiro64Star {

/// Seed a `Xoroshiro64Star` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoroshiro64Star {
let mut s = [0; 8];
LittleEndian::write_u64(&mut s, seed);
Xoroshiro64Star::from_seed(s)
from_splitmix!(seed)
}
}

Expand All @@ -94,4 +91,10 @@ mod tests {
assert_eq!(rng.next_u32(), e);
}
}

#[test]
fn zero_seed() {
let mut rng = Xoroshiro64Star::seed_from_u64(0);
assert_ne!(rng.next_u64(), 0);
}
}
13 changes: 8 additions & 5 deletions rand_xoshiro/src/xoroshiro64starstar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use byteorder::{ByteOrder, LittleEndian};
use rand_core;
use rand_core::le::read_u32_into;
use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32};
Expand Down Expand Up @@ -68,11 +67,9 @@ impl SeedableRng for Xoroshiro64StarStar {
}
}

/// Seed a `Xoroshiro64StarStar` from a `u64`.
/// Seed a `Xoroshiro64StarStar` from a `u64` using `SplitMix64`.
fn seed_from_u64(seed: u64) -> Xoroshiro64StarStar {
let mut s = [0; 8];
LittleEndian::write_u64(&mut s, seed);
Xoroshiro64StarStar::from_seed(s)
from_splitmix!(seed)
}
}

Expand All @@ -93,4 +90,10 @@ mod tests {
assert_eq!(rng.next_u32(), e);
}
}

#[test]
fn zero_seed() {
let mut rng = Xoroshiro64StarStar::seed_from_u64(0);
assert_ne!(rng.next_u64(), 0);
}
}

0 comments on commit 01343e1

Please sign in to comment.