diff --git a/Cargo.toml b/Cargo.toml index ebb78f05a..70302389e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rand_mt" -version = "4.1.0" # remember to set `html_root_url` in `src/lib.rs`. +version = "4.1.1" # remember to set `html_root_url` in `src/lib.rs`. authors = ["David Creswick ", "Ryan Lopopolo "] license = "MIT OR Apache-2.0" edition = "2018" diff --git a/src/lib.rs b/src/lib.rs index 09daa744a..282bc9d04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,7 +84,7 @@ //! [`rand_core`]: https://crates.io/crates/rand_core //! [`std::error::error`]: https://doc.rust-lang.org/std/error/trait.Error.html -#![doc(html_root_url = "https://docs.rs/rand_mt/4.1.0")] +#![doc(html_root_url = "https://docs.rs/rand_mt/4.1.1")] #![no_std] // Ensure code blocks in README.md compile diff --git a/src/mt.rs b/src/mt.rs index 909cc34be..e7ab28b92 100644 --- a/src/mt.rs +++ b/src/mt.rs @@ -310,8 +310,11 @@ impl Mt19937GenRand32 { next.copy_from_slice(&chunk); } - dest_chunks - .into_remainder() + let remainder = dest_chunks.into_remainder(); + if remainder.is_empty() { + return; + } + remainder .iter_mut() .zip(self.next_u32().to_le_bytes().iter()) .for_each(|(cell, &byte)| { diff --git a/src/mt64.rs b/src/mt64.rs index 08b402de9..8bd35e6c0 100644 --- a/src/mt64.rs +++ b/src/mt64.rs @@ -293,8 +293,11 @@ impl Mt19937GenRand64 { next.copy_from_slice(&chunk); } - dest_chunks - .into_remainder() + let remainder = dest_chunks.into_remainder(); + if remainder.is_empty() { + return; + } + remainder .iter_mut() .zip(self.next_u64().to_le_bytes().iter()) .for_each(|(cell, &byte)| { diff --git a/tests/ruby_reproducibility.rs b/tests/ruby_reproducibility.rs new file mode 100644 index 000000000..2b3a79485 --- /dev/null +++ b/tests/ruby_reproducibility.rs @@ -0,0 +1,32 @@ +use rand_mt::Mt; + +// ```ruby +// # Should double check this is official spec +// it "returns the same numeric output for a given seed across all implementations and platforms" do +// rnd = Random.new(33) +// rnd.bytes(2).should == "\x14\\" +// rnd.bytes(1000) # skip some +// rnd.bytes(2).should == "\xA1p" +// end +// +// it "returns the same numeric output for a given huge seed across all implementations and platforms" do +// rnd = Random.new(bignum_value ** 4) +// rnd.bytes(2).should == "_\x91" +// rnd.bytes(1000) # skip some +// rnd.bytes(2).should == "\x17\x12" +// end +// ``` +#[test] +fn spec_bytes() { + let mut rng = Mt::new(33); + let mut buf = [0; 2]; + rng.fill_bytes(&mut buf); + assert_eq!(buf[..], b"\x14\\"[..]); + + let mut skip = [0; 1000]; + rng.fill_bytes(&mut skip); + + let mut buf = [0; 2]; + rng.fill_bytes(&mut buf); + assert_eq!(buf[..], b"\xA1p"[..]); +}