summaryrefslogtreecommitdiff
path: root/gen_random/src/lib.rs
blob: b9cdb4f0f68abcfab924ac9487beb4f4dd685ede (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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;
	fn gen_thread_random() -> Self {
		let mut thread_rng = rand::thread_rng();
		Self::gen_random(&mut thread_rng)
	}
}

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)
	}
}

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))
	}
}

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))
		}
	}
}