# [Tutor] Percolation model in python

Kooser, Ara S askoose at sandia.gov
Sun Feb 6 18:27:18 CET 2005

```Hello,

I have been working with some high school students to create a model of small pox transmission.
I am somewhat new to python (my programming experience is in f77) so I have borrowed parts of Danny's code that he posted for the Game of Life. I have included the code that we are using below.
I have two questions. Once a MxN world is generated how would you search for nearest neighbors (to see who is connected) and then color the '*' so it's easier to see who is connected and who isn't.

For a definition of percolation theory-  http://en.wikipedia.org/wiki/Percolation_theory
or for the wolfram fans http://mathworld.wolfram.com/PercolationTheory.html

Thanks,
Ara

CODE STARTS HERE:

print """
1) Percolation model for Small Pox
2)
3) Instructions
4) Exit
"""

option = raw_input("Which option[1,2,3,4]? ")

if option == '1':

import random

perc = raw_input("Please enter a thresold between 0-1.   ")
perc = float(perc)

###
PERSON, EMPTY = '*', '.'
###

###

def percolation(perc):
randval = random.random()
if randval > perc:
return EMPTY
else:
return PERSON

def make_random_world(M, N):
"""Constructs a new random game world of size MxN."""
world = {}
for j in range(N):
for i in range(M):
world[i, j] = percolation(perc)
world['dimensions'] = (M, N)
return world

def print_world(world):
"""Prints out a string representation of a world."""
M, N = world['dimensions']
for j in range(N):
for i in range(M):
print world[i, j],
print

n = int(raw_input("Please enter a n dimension.   "))
m = int(raw_input("Please enter a m dimension.   "))