Skip to content

Instantly share code, notes, and snippets.

@libswan
Created May 12, 2013 09:01
Show Gist options
  • Save libswan/5562896 to your computer and use it in GitHub Desktop.
Save libswan/5562896 to your computer and use it in GitHub Desktop.
Correct code from papashangu
def rabbitGrowth():
global CURRENTRABBITPOP
global MAXRABBITPOP
num = CURRENTRABBITPOP
for i in xrange(num):
if random.random() < (1 - (float(CURRENTRABBITPOP) / MAXRABBITPOP)):
CURRENTRABBITPOP += 1
def foxGrowth():
global CURRENTRABBITPOP
global CURRENTFOXPOP
global MAXRABBITPOP
num = CURRENTFOXPOP
for i in xrange(num):
if (random.random() < (float(CURRENTRABBITPOP) / MAXRABBITPOP)) and (CURRENTRABBITPOP > 10):
CURRENTRABBITPOP -= 1
if random.random() < (float(1) / 3):
CURRENTFOXPOP += 1
else:
if (random.random() < 0.1) and (CURRENTFOXPOP > 10):
CURRENTFOXPOP -= 1
def runSimulation(numSteps):
global CURRENTRABBITPOP
global CURRENTFOXPOP
rabbit_populations = []
fox_populations = []
for i in xrange(numSteps):
rabbitGrowth()
foxGrowth()
rabbit_populations.append(CURRENTRABBITPOP)
fox_populations.append(CURRENTFOXPOP)
return (rabbit_populations, fox_populations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment