time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

commit cbca6dbb027b760036495c73b9df7bcfd1302d4f
parent 52d0fba1c9a881ece67f8b4e7d040ce317c6e923
Author: NunoSempere <nuno.semperelh@protonmail.com>
Date:   Sat,  6 Apr 2024 14:00:55 -0400

rust: continue creating model

Diffstat:
Mrust/src/main.rs | 31++++++++++++++++++++++---------
1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/rust/src/main.rs b/rust/src/main.rs @@ -1,15 +1,30 @@ use rand_core::SeedableRng; +use rand_distr::{Distribution, Normal, Uniform}; use rand_pcg::Pcg64Mcg; -use rand_distr::{Normal, Distribution}; // use rand::thread_rng; // use rand::prelude::* fn model(mut rng: &mut Pcg64Mcg) -> f64 { - let x = 1.0; - let normal = Normal::new(2.0, 3.0).unwrap(); - let v = normal.sample(&mut rng); - v * x + let p_a = 0.8; + let p_b = 0.5; + let p_c = p_a * p_b; + + let ws = [1.0 - p_c, p_c / 2.0, p_c / 4.0, p_c / 4.0]; + + let uniform = Uniform::new(0.0, 1.0); + let p = uniform.sample(&mut rng); + if p < ws[0] { + return 0.0; + } else if p < (ws[0] + ws[1]) { + return 1.0; + } else if p < (ws[0] + ws[1] + ws[2]) { + let normal = Normal::new(2.0, 3.0).unwrap(); + let v = normal.sample(&mut rng); + return v; + } else { + return 3.0; + } } fn main() { @@ -18,9 +33,7 @@ fn main() { let mut rng = Pcg64Mcg::seed_from_u64(1); let a = model(&mut rng); - println!("{} is from a N(2, 9) distribution", a); + println!("Sample is {}", a); let b = model(&mut rng); - println!("{} is from a N(2, 9) distribution", b); + println!("Sample is {}", b); } - -