summaryrefslogtreecommitdiff
path: root/gen_random/src/lib.rs
blob: f091ec338ee9ad1516d0294d2fb16b1b749051fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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))
	}
}