Add uniformity tests and change spawn

This commit is contained in:
Joshua Coles 2023-03-03 11:39:17 +00:00
parent f5853580f4
commit 51ad848046
3 changed files with 46 additions and 3 deletions

View File

@ -6,7 +6,7 @@ pub mod model;
pub const DIM: u32 = 2; pub const DIM: u32 = 2;
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Position(pub i32, pub i32); pub struct Position(pub i32, pub i32);
impl Position { impl Position {

View File

@ -89,7 +89,7 @@ impl<R: Rng, S: Storage, W: Walker<R>> DLASystem<R, S, W> {
for direction in 0..DIM { for direction in 0..DIM {
for sign in [-1, 1] { for sign in [-1, 1] {
let neighbour = position.clone() + Position::in_direction(direction, sign); 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; return true;
} }
} }
@ -101,7 +101,7 @@ impl<R: Rng, S: Storage, W: Walker<R>> DLASystem<R, S, W> {
fn spawn_particle(&mut self) { fn spawn_particle(&mut self) {
let theta = self.rng.gen_range(0f32..1.0); let theta = self.rng.gen_range(0f32..1.0);
let (x, y) = (self.add_circle * theta.cos(), self.add_circle * theta.sin()); 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) { if !self.storage.at(&position) {
self.active_particle = Some(position); self.active_particle = Some(position);

View File

@ -17,3 +17,46 @@ impl<R: Rng> Walker<R> for LocalRandomWalker {
position.clone() + offset 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<Position> = 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);
}
}