diff --git a/src/lib.rs b/src/lib.rs index c74ae86..ce1ed00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -449,6 +449,14 @@ impl Rng { } } + /// Return a byte array of the given size. + #[inline] + pub fn bytes(&self) -> [u8; N] { + let mut bytes = [0u8; N]; + self.fill(&mut bytes); + bytes + } + rng_integer!( u8, u8, diff --git a/tests/smoke.rs b/tests/smoke.rs index 07e5c8e..a4abdd2 100644 --- a/tests/smoke.rs +++ b/tests/smoke.rs @@ -81,6 +81,29 @@ fn u128() { } } +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn fill() { + let r = fastrand::Rng::new(); + let mut a = [0u8; 64]; + let mut b = [0u8; 64]; + + r.fill(&mut a); + r.fill(&mut b); + + assert_ne!(a, b); +} + +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn bytes() { + let r = fastrand::Rng::new(); + let a: [u8; 32] = r.bytes(); + let b: [u8; 32] = r.bytes(); + + assert_ne!(a, b); +} + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn rng() {