thedes_gen/
random.rs

1use std::convert::Infallible;
2
3use rand::{Rng, SeedableRng};
4use rand_distr::Distribution;
5
6pub type PickedReproducibleRng = rand_chacha::ChaCha8Rng;
7
8pub type ProabilityWeight = u32;
9
10pub type Seed = u32;
11
12pub fn create_reproducible_rng(seed: Seed) -> PickedReproducibleRng {
13    let mut full_seed = <PickedReproducibleRng as SeedableRng>::Seed::default();
14    for (i, chunk) in full_seed.chunks_exact_mut(4).enumerate() {
15        let i = i as Seed;
16        let bits = seed.wrapping_sub(i) ^ (i << 14);
17        chunk.copy_from_slice(&bits.to_le_bytes());
18    }
19    PickedReproducibleRng::from_seed(full_seed)
20}
21
22pub trait MutableDistribution<T> {
23    type Error: std::error::Error;
24
25    fn sample_mut<R>(&mut self, rng: &mut R) -> Result<T, Self::Error>
26    where
27        R: Rng + ?Sized;
28}
29
30impl<T, D> MutableDistribution<T> for D
31where
32    D: Distribution<T>,
33{
34    type Error = Infallible;
35
36    fn sample_mut<R>(&mut self, rng: &mut R) -> Result<T, Self::Error>
37    where
38        R: Rng + ?Sized,
39    {
40        Ok(self.sample(rng))
41    }
42}