diff options
author | pommicket <pommicket@gmail.com> | 2022-12-14 13:08:08 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-14 13:08:08 -0500 |
commit | 5a20cffba66caa71b495736f75031f69d09ba40b (patch) | |
tree | 92834986e8f8c7d1ab8947da52e649c90caac556 /gen_random/src | |
parent | 3265cb676c5c87fd624f59aaf3ca89d947df8cda (diff) |
more genrandom
Diffstat (limited to 'gen_random/src')
-rw-r--r-- | gen_random/src/lib.rs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/gen_random/src/lib.rs b/gen_random/src/lib.rs index f091ec3..b9cdb4f 100644 --- a/gen_random/src/lib.rs +++ b/gen_random/src/lib.rs @@ -1,5 +1,8 @@ extern crate rand; use rand::Rng; +use std::rc::Rc; +use std::sync::Arc; +use std::cell::{Cell, RefCell}; pub trait GenRandom: Sized { fn gen_random(rng: &mut impl Rng) -> Self; @@ -9,6 +12,14 @@ pub trait GenRandom: Sized { } } +pub fn gen_random_vec<T: GenRandom>(rng: &mut impl Rng, len: usize) -> Vec<T> { + (0..len).map(|_| T::gen_random(rng)).collect() +} + +pub fn gen_thread_random_vec<T: GenRandom>(len: usize) -> Vec<T> { + gen_random_vec(&mut rand::thread_rng(), len) +} + impl GenRandom for f32 { fn gen_random(rng: &mut impl Rng) -> Self { rng.gen_range(0.0..1.0) @@ -26,3 +37,80 @@ impl<T: GenRandom> GenRandom for Box<T> { Box::new(T::gen_random(rng)) } } + +impl<T: GenRandom> GenRandom for [T; 1] { + fn gen_random(rng: &mut impl Rng) -> Self { + [T::gen_random(rng)] + } +} + +impl<T: GenRandom> GenRandom for [T; 2] { + fn gen_random(rng: &mut impl Rng) -> Self { + [T::gen_random(rng), T::gen_random(rng)] + } +} + +impl<T: GenRandom> GenRandom for [T; 3] { + fn gen_random(rng: &mut impl Rng) -> Self { + [T::gen_random(rng), T::gen_random(rng), T::gen_random(rng)] + } +} + +impl<T: GenRandom> GenRandom for [T; 4] { + fn gen_random(rng: &mut impl Rng) -> Self { + [T::gen_random(rng), T::gen_random(rng), T::gen_random(rng), T::gen_random(rng)] + } +} + +impl<T: GenRandom> GenRandom for (T, T) { + fn gen_random(rng: &mut impl Rng) -> Self { + (T::gen_random(rng), T::gen_random(rng)) + } +} + +impl<T: GenRandom> GenRandom for (T, T, T) { + fn gen_random(rng: &mut impl Rng) -> Self { + (T::gen_random(rng), T::gen_random(rng), T::gen_random(rng)) + } +} + +impl<T: GenRandom> GenRandom for (T, T, T, T) { + fn gen_random(rng: &mut impl Rng) -> Self { + (T::gen_random(rng), T::gen_random(rng), T::gen_random(rng), T::gen_random(rng)) + } +} + +impl<T: GenRandom> GenRandom for Rc<T> { + fn gen_random(rng: &mut impl Rng) -> Self { + Self::new(T::gen_random(rng)) + } +} + +impl<T: GenRandom> GenRandom for Arc<T> { + fn gen_random(rng: &mut impl Rng) -> Self { + Self::new(T::gen_random(rng)) + } +} + +impl<T: GenRandom> GenRandom for Cell<T> { + fn gen_random(rng: &mut impl Rng) -> Self { + Self::new(T::gen_random(rng)) + } +} + +impl<T: GenRandom> GenRandom for RefCell<T> { + fn gen_random(rng: &mut impl Rng) -> Self { + Self::new(T::gen_random(rng)) + } +} + +impl<T: GenRandom> GenRandom for Option<T> { + fn gen_random(rng: &mut impl Rng) -> Self { + if rng.gen_range(0..2) == 0 { + None + } else { + Some(T::gen_random(rng)) + } + } +} + |