chandeliers_std/
rand.rs

1//! Random primitives for Lustre.
2
3use rand::{rngs::ThreadRng, Rng};
4
5use chandeliers_sem::traits::{Embed, Step};
6use chandeliers_sem::{implicit_clock, ty};
7
8/// Lustre node that returns a random `int` uniformly between
9/// `i64::MIN` and `i64::MAX`.
10#[derive(Debug, Default, Clone)]
11pub struct random_int {
12    /// Internal random number generator.
13    rng: ThreadRng,
14}
15impl Step for random_int {
16    type Input = ();
17    type Output = i64;
18    fn step(&mut self, __inputs: ty!()) -> ty!(int) {
19        implicit_clock!(__inputs);
20        self.rng.gen::<i64>().embed()
21    }
22}
23
24/// Lustre node that returns a random `float` uniformly between
25/// `f64::MIN` and `f64::MAX`.
26#[derive(Debug, Default, Clone)]
27pub struct random_float {
28    /// Internal random number generator.
29    rng: ThreadRng,
30}
31impl Step for random_float {
32    type Input = ();
33    type Output = f64;
34    fn step(&mut self, __inputs: ty!()) -> ty!(float) {
35        implicit_clock!(__inputs);
36        self.rng.gen::<f64>().embed()
37    }
38}
39
40/// Lustre node that returns a random `bool` uniformly.
41#[derive(Debug, Default, Clone)]
42pub struct random_bool {
43    /// Internal random number generator.
44    rng: ThreadRng,
45}
46impl Step for random_bool {
47    type Input = ();
48    type Output = bool;
49    fn step(&mut self, __inputs: ty!()) -> ty!(bool) {
50        implicit_clock!(__inputs);
51        self.rng.gen::<bool>().embed()
52    }
53}