forked from Maxluli/RustyPropagation
fixing the roll function
This commit is contained in:
+24
-7
@@ -151,12 +151,13 @@ impl Population {
|
|||||||
//now we can start to check if people would be infected or not
|
//now we can start to check if people would be infected or not
|
||||||
//let idx = human_idx(pos.x as i32, pos.y as i32, self.width as i32);
|
//let idx = human_idx(pos.x as i32, pos.y as i32, self.width as i32);
|
||||||
if pos.x > 0 && pos.x < self.width - 1 && pos.y > 0 && pos.y < self.height - 1 {
|
if pos.x > 0 && pos.x < self.width - 1 && pos.y > 0 && pos.y < self.height - 1 {
|
||||||
if self.roll(self.plague.curing_rate) {
|
if Population::roll(self.plague.curing_rate) {
|
||||||
//checks if the man dies
|
//checks if the man recovers
|
||||||
people_to_cure.push(Point { x: pos.x, y: pos.y });
|
people_to_cure.push(Point { x: pos.x, y: pos.y });
|
||||||
} else {
|
} else {
|
||||||
if self.roll(self.plague.death_rate) {
|
if Population::roll(self.plague.death_rate) {
|
||||||
//cheks if the man dies
|
//cheks if the man dies
|
||||||
|
println!("someone should die");
|
||||||
people_to_kill.push(Point { x: pos.x, y: pos.y });
|
people_to_kill.push(Point { x: pos.x, y: pos.y });
|
||||||
} else {
|
} else {
|
||||||
let mut possible_infections: Vec<Point> = Vec::with_capacity(8);
|
let mut possible_infections: Vec<Point> = Vec::with_capacity(8);
|
||||||
@@ -203,7 +204,7 @@ impl Population {
|
|||||||
let inf_idx =
|
let inf_idx =
|
||||||
human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
|
human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
|
||||||
if self.humans[inf_idx].present_state == State::Normal {
|
if self.humans[inf_idx].present_state == State::Normal {
|
||||||
if self.roll(self.plague.infection_rate) {
|
if Population::roll(self.plague.infection_rate) {
|
||||||
people_to_infect.push(Point {
|
people_to_infect.push(Point {
|
||||||
x: poss_infected_pos.x,
|
x: poss_infected_pos.x,
|
||||||
y: poss_infected_pos.y,
|
y: poss_infected_pos.y,
|
||||||
@@ -219,7 +220,7 @@ impl Population {
|
|||||||
|
|
||||||
//REMOVE WHEN IMPLEMENTED
|
//REMOVE WHEN IMPLEMENTED
|
||||||
//It is here to prevent an infected in the borders to keep the programm running as he thinks there are still people to simulate
|
//It is here to prevent an infected in the borders to keep the programm running as he thinks there are still people to simulate
|
||||||
people_to_kill.push(Point { x: pos.x, y: pos.y });
|
//people_to_kill.push(Point { x: pos.x, y: pos.y });
|
||||||
|
|
||||||
// @##
|
// @##
|
||||||
// ###
|
// ###
|
||||||
@@ -292,7 +293,7 @@ impl Population {
|
|||||||
}
|
}
|
||||||
stats
|
stats
|
||||||
}
|
}
|
||||||
pub fn roll(&self, probability: u32) -> bool {
|
pub fn roll(probability: u32) -> bool {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32
|
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32
|
||||||
}
|
}
|
||||||
@@ -425,6 +426,18 @@ mod tests {
|
|||||||
assert_eq!(stats.dead, width * height);
|
assert_eq!(stats.dead, width * height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[parameterized(rate = {0, 100}, expected = {false, true})]
|
||||||
|
fn roll_test(rate: u32, expected: bool) {
|
||||||
|
let tries = 1000;
|
||||||
|
let mut result = 0;
|
||||||
|
for _x in 0..1000 {
|
||||||
|
if Population::roll(rate) == expected {
|
||||||
|
result += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_eq!(result, tries);
|
||||||
|
}
|
||||||
|
|
||||||
#[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 1})]
|
#[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 1})]
|
||||||
fn propage_test(
|
fn propage_test(
|
||||||
infection_rate: u32,
|
infection_rate: u32,
|
||||||
@@ -455,7 +468,11 @@ mod tests {
|
|||||||
|
|
||||||
let infected_at_start = stats.infected;
|
let infected_at_start = stats.infected;
|
||||||
|
|
||||||
population.propagate();
|
let propa_stats: [i32; 4] = population.propagate();
|
||||||
|
if death_rate == 0 {
|
||||||
|
assert_eq!(propa_stats[3], 0, "no human should have died");
|
||||||
|
}
|
||||||
|
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("Population after propagate: {:?}", stats);
|
println!("Population after propagate: {:?}", stats);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user