pub use crate::rng::{ProvablyFairConfig, ProvablyFairRNG};
use std::fmt;
#[derive(Debug)]
pub struct SimulationResult {
pub squares: Vec<u8>,
}
impl fmt::Display for SimulationResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Squares: {:?}", self.squares)
}
}
pub fn simulate(config: ProvablyFairConfig) -> SimulationResult {
let mut rng: ProvablyFairRNG<f64> = ProvablyFairRNG::from_config(config);
let mut remaining_squares: Vec<u8> = (1..41).collect();
let picked_squares: Vec<_> = (0..10)
.map(|_| {
let f = rng.next().unwrap();
let idx = (f * remaining_squares.len() as f64) as usize;
let n = *remaining_squares.get(idx).unwrap();
remaining_squares.remove(idx);
n
})
.collect();
SimulationResult {
squares: picked_squares,
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn simulate_plinko_test() {
let config = ProvablyFairConfig::new("client seed", "server seed", 1);
assert_eq!(
simulate(config).squares,
vec![30, 26, 10, 37, 22, 35, 25, 24, 39, 4]
);
let config = ProvablyFairConfig::new("client seed", "server seed", 2);
assert_eq!(
simulate(config).squares,
vec![22, 26, 8, 4, 3, 19, 9, 2, 34, 10]
);
}
}