Python: Zombie Contagion Simulation

Zombie Contagion Simulation

Before going to bed last night, this thought crossed my mind… to write a little script in Python that would simulate a case where you had an infectious disease that could spread to humans, who add to the contagion… like a Zombie outbreak…

Assumptions
Zombies are highly contagious
Once infected there is a 100% chance of transitioning to a zombie state
Humans can kill or cure a zombie, but only 1 at a time
A Zombie can only convert one human at a time

My initial method of running this calculation was to write this code:

import random
from datetime import datetime 
def zombie_attack(zombies, humans, zom_mod = 1, human_mod = 1):
    start_time = datetime.now()
    while humans > 0 and zombies > 0:
        zombie_roll = random.randint(1,100) + zom_mod
        human_roll = random.randint(1,100) + human_mod
        if zombie_roll > human_roll:
            print("A human has converted into a zombie")
            zombies += 1
            humans -= 1
        else:
            print("A zombie has been killed")
            zombies -= 1
    if zombies == 0:
        print("The humans won.")
        print("There are "+ str(humans)+ " humans remaining.")
    elif humans == 0:
        print("The humans have lost.")
        print("There are "+str(zombies)+" zombies and 0 humans.")
    end_time = datetime.now()
    time_length = end_time - start_time
    print("It took " + str(time_length) + " to finish this simulation."

The zombie_attack method takes 2 required parameters and 2 optional.  Required we have the amount of zombies (infected and contagious individuals) and the amount of humans (uninfected.)   Optional, we have a modifier for both human and zombie.  This modifier is a percentage taking a value from 1 to 100.  For a zombie it would increase the odds of converting someone via contagion.  For a human, it would be the odds of removing a zombie from the population, either by cure of destruction.

It all happens within a while loop. While the amount of human and zombies are greater then 0, we roll the dice… a random number is calculated for both the zombie and the human… If the zombie roll is greater than the human, the zombie gets to convert one human and the human population looses one member.  Otherwise, a human has either cured or killed a zombie.

Once we hit a conclusion the loop breaks and we check if the zombies won or the humans… who is at 0 population?

With a run like so:

zombie_attack(2, 2000000, 20)

which gives a 20% edge to the zombies, it’s interesting that many times the zombies win even though it seems incredible that 2 individuals could overtake a population of 200,000 people.

The humans have lost.
There are 101231 zombies and 0 humans.

Even more interesting was taking 2 zombies against a population of 2 Million humans. I gave the zombies a 20% chance of infection rate, and the humans a 10% chance to kill/cure a zombie.

zombie_attack(2, 2000000, 20, 10)

Surprisingly the zombies win most of the time:
There are 614363 zombies and 0 humans.
It took 0:00:27.840368 to finish this simulation.

Leave a Reply

Your email address will not be published. Required fields are marked *