diff options
author | pommicket <pommicket@gmail.com> | 2022-12-14 12:45:48 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-14 12:45:48 -0500 |
commit | 3265cb676c5c87fd624f59aaf3ca89d947df8cda (patch) | |
tree | 0bc94cbdddb9cecfc6205f834428e6efafcdb69a /gen_random/src | |
parent | 3f839f2ec08ffbb75bee391202e7e0d7432d97e0 (diff) |
GenRandom
Diffstat (limited to 'gen_random/src')
-rw-r--r-- | gen_random/src/lib.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/gen_random/src/lib.rs b/gen_random/src/lib.rs new file mode 100644 index 0000000..f091ec3 --- /dev/null +++ b/gen_random/src/lib.rs @@ -0,0 +1,28 @@ +extern crate rand; +use rand::Rng; + +pub trait GenRandom: Sized { + fn gen_random(rng: &mut impl Rng) -> Self; + fn gen_thread_random() -> Self { + let mut thread_rng = rand::thread_rng(); + Self::gen_random(&mut thread_rng) + } +} + +impl GenRandom for f32 { + fn gen_random(rng: &mut impl Rng) -> Self { + rng.gen_range(0.0..1.0) + } +} + +impl GenRandom for f64 { + fn gen_random(rng: &mut impl Rng) -> Self { + rng.gen_range(0.0..1.0) + } +} + +impl<T: GenRandom> GenRandom for Box<T> { + fn gen_random(rng: &mut impl Rng) -> Self { + Box::new(T::gen_random(rng)) + } +} |