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
#[cfg(feature = "chacha")]
pub use chacha::{ChaCha, ChaCha12, ChaCha20, ChaCha8};
#[cfg(feature = "pcg64")]
pub use pcg64::Pcg64;
#[cfg(feature = "wyrand")]
pub use wyrand::WyRand;
use crate::gen::{RandomGen, RandomRange};
use core::ops::RangeBounds;
#[cfg(feature = "wyrand")]
pub mod wyrand;
#[cfg(feature = "pcg64")]
pub mod pcg64;
#[cfg(feature = "chacha")]
pub mod chacha;
pub trait Rng: Clone {
type Output: AsRef<[u8]>;
fn rand(&mut self) -> Self::Output;
fn rand_with_seed(seed: &[u8]) -> Self::Output;
fn generate<R>(&mut self) -> R
where
R: RandomGen<Self>,
{
R::random(self)
}
fn fill<R, A>(&mut self, mut target: A)
where
R: RandomGen<Self>,
A: AsMut<[R]>,
{
let target = target.as_mut();
target.iter_mut().for_each(|entry| *entry = self.generate());
}
fn generate_range<R, B>(&mut self, range: B) -> R
where
R: RandomRange<Self>,
B: RangeBounds<R>,
{
R::random_range(self, range)
}
fn shuffle<I, S>(&mut self, mut target: S)
where
S: AsMut<[I]>,
{
let target = target.as_mut();
let target_len = target.len();
for idx in 0..target_len {
let random_idx = self.generate_range(0..target_len);
target.swap(idx, random_idx);
}
}
fn reseed(&mut self, new_seed: &[u8]);
}