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
//! Random primitives for Lustre.

use rand::{rngs::ThreadRng, Rng};

use chandeliers_sem::traits::{Embed, Step};
use chandeliers_sem::{implicit_clock, ty};

/// Lustre node that returns a random `int` uniformly between
/// `i64::MIN` and `i64::MAX`.
#[derive(Debug, Default, Clone)]
pub struct random_int {
    /// Internal random number generator.
    rng: ThreadRng,
}
impl Step for random_int {
    type Input = ();
    type Output = i64;
    fn step(&mut self, __inputs: ty!()) -> ty!(int) {
        implicit_clock!(__inputs);
        self.rng.gen::<i64>().embed()
    }
}

/// Lustre node that returns a random `float` uniformly between
/// `f64::MIN` and `f64::MAX`.
#[derive(Debug, Default, Clone)]
pub struct random_float {
    /// Internal random number generator.
    rng: ThreadRng,
}
impl Step for random_float {
    type Input = ();
    type Output = f64;
    fn step(&mut self, __inputs: ty!()) -> ty!(float) {
        implicit_clock!(__inputs);
        self.rng.gen::<f64>().embed()
    }
}

/// Lustre node that returns a random `bool` uniformly.
#[derive(Debug, Default, Clone)]
pub struct random_bool {
    /// Internal random number generator.
    rng: ThreadRng,
}
impl Step for random_bool {
    type Input = ();
    type Output = bool;
    fn step(&mut self, __inputs: ty!()) -> ty!(bool) {
        implicit_clock!(__inputs);
        self.rng.gen::<bool>().embed()
    }
}