1pub use crate::rng::{ProvablyFairConfig, ProvablyFairRNG};
25use std::fmt;
26
27#[derive(Debug)]
28pub struct SimulationResult {
29 pub squares: Vec<u8>,
30}
31
32impl fmt::Display for SimulationResult {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 write!(f, "Squares: {:?}", self.squares)
35 }
36}
37
38pub fn simulate(config: ProvablyFairConfig) -> SimulationResult {
39 let mut rng: ProvablyFairRNG<f64> = ProvablyFairRNG::from_config(config);
40
41 let mut remaining_squares: Vec<u8> = (1..41).collect();
42
43 let picked_squares: Vec<_> = (0..10)
44 .map(|_| {
45 let f = rng.next().unwrap();
46 let idx = (f * remaining_squares.len() as f64) as usize;
47 let n = *remaining_squares.get(idx).unwrap();
49 remaining_squares.remove(idx);
50 n
51 })
52 .collect();
53
54 SimulationResult {
55 squares: picked_squares,
56 }
57}
58
59#[cfg(test)]
60mod test {
61 use super::*;
62
63 #[test]
64 fn simulate_plinko_test() {
65 let config = ProvablyFairConfig::new("client seed", "server seed", 1);
66 assert_eq!(
67 simulate(config).squares,
68 vec![30, 26, 10, 37, 22, 35, 25, 24, 39, 4]
69 );
70 let config = ProvablyFairConfig::new("client seed", "server seed", 2);
71 assert_eq!(
72 simulate(config).squares,
73 vec![22, 26, 8, 4, 3, 19, 9, 2, 34, 10]
74 );
75 }
76}