diff --git a/src/system/mod.rs b/src/system/mod.rs index ad7a5f9..b9c9b71 100644 --- a/src/system/mod.rs +++ b/src/system/mod.rs @@ -6,7 +6,7 @@ pub mod model; pub const DIM: u32 = 2; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct Position(pub i32, pub i32); impl Position { diff --git a/src/system/model.rs b/src/system/model.rs index 737b215..4300676 100644 --- a/src/system/model.rs +++ b/src/system/model.rs @@ -89,7 +89,7 @@ impl> DLASystem { for direction in 0..DIM { for sign in [-1, 1] { let neighbour = position.clone() + Position::in_direction(direction, sign); - if self.storage.at(&neighbour) && self.rng.gen_range(0.0f32..1.0) < self.stick_probability { + if self.storage.at(&neighbour) && self.rng.gen_range(0.0f32..=1.0) < self.stick_probability { return true; } } @@ -101,7 +101,7 @@ impl> DLASystem { fn spawn_particle(&mut self) { let theta = self.rng.gen_range(0f32..1.0); let (x, y) = (self.add_circle * theta.cos(), self.add_circle * theta.sin()); - let position = Position(x.round() as i32, y.round() as i32); + let position = Position(x as i32, y as i32); if !self.storage.at(&position) { self.active_particle = Some(position); diff --git a/src/system/walker.rs b/src/system/walker.rs index 6cde562..7fd54c7 100644 --- a/src/system/walker.rs +++ b/src/system/walker.rs @@ -17,3 +17,46 @@ impl Walker for LocalRandomWalker { position.clone() + offset } } + +mod test { + use rand::rngs::SmallRng; + use rand::{SeedableRng, thread_rng}; + use crate::system::Position; + use crate::system::walker::{LocalRandomWalker, Walker}; + + #[test] + fn uniformity() { + let walker = LocalRandomWalker; + let mut rng = SmallRng::from_rng(thread_rng()).unwrap(); + let mut results: Vec = vec![]; + + let origin = &Position(0, 0); + + let x: u32 = (1_000000_000); + for i in 0..x { + results.push(walker.walk(&mut rng, origin)); + } + + let a = results + .iter() + .filter(|a| **a == Position(0, 1)) + .count(); + + let b = results + .iter() + .filter(|a| **a == Position(0, -1)) + .count(); + + let c = results + .iter() + .filter(|a| **a == Position(1, 0)) + .count(); + + let d = results + .iter() + .filter(|a| **a == Position(-1, 0)) + .count(); + + println!("{} {} {} {}", a as f32 / x as f32, b as f32 / x as f32, c as f32 / x as f32, d as f32 / x as f32); + } +}