Skip to content

Instantly share code, notes, and snippets.

@libswan
Last active December 17, 2015 05:39
Show Gist options
  • Save libswan/5559919 to your computer and use it in GitHub Desktop.
Save libswan/5559919 to your computer and use it in GitHub Desktop.
My code. This didn't work.
def rabbitGrowth():
"""
rabbitGrowth is called once at the beginning of each time step.
It makes use of the global variables: CURRENTRABBITPOP and MAXRABBITPOP.
The global variable CURRENTRABBITPOP is modified by this procedure.
For each rabbit, based on the probabilities in the problem set write-up,
a new rabbit may be born.
Nothing is returned.
"""
#print 'rabbitGrowth Loop'
global CURRENTRABBITPOP
#print CURRENTRABBITPOP
global MAXRABBITPOP
#print MAXRABBITPOP
cRP = float(CURRENTRABBITPOP)
mRP = float(MAXRABBITPOP)
currentOverMax = cRP/mRP
probRR = 1.0 - currentOverMax
#print 'probRR ' + str(probRR)
rabitRandom = random.random()
#print 'rabitRandom ' + str(rabitRandom)
if rabitRandom < probRR:
cRP += 1
CURRENTRABBITPOP = int(cRP)
#print 'CRP @ rabbitGrowth End ' + str(CURRENTRABBITPOP)
def foxGrowth():
"""
foxGrowth is called once at the end of each time step.
It makes use of the global variables: CURRENTFOXPOP and CURRENTRABBITPOP,
and both may be modified by this procedure.
Each fox, based on the probabilities in the problem statement, may eat
one rabbit (but only if there are more than 10 rabbits).
If it eats a rabbit, then with a 1/3 prob it gives birth to a new fox.
If it does not eat a rabbit, then with a 1/10 prob it dies.
Nothing is returned.
"""
#print 'foxGrowth Loop'
global CURRENTRABBITPOP
#print CURRENTRABBITPOP
global MAXRABBITPOP
#print MAXRABBITPOP
global CURRENTFOXPOP
#print CURRENTFOXPOP
one = float(1)
three = float(3)
foxGivesBirth = one/three
foxDies = 0.1
cRP = float(CURRENTRABBITPOP)
mRP = float(MAXRABBITPOP)
currentOverMax = cRP/mRP
probFER = currentOverMax
#print 'probFER ' + str(probFER)
foxEats = False
#Will the fox eat?
foxEatsRandom = random.random()
#print 'foxEatsRandom ' + str(foxEatsRandom)
if foxEatsRandom < probFER:
foxEats = True
if foxEats == True:
#Reduces Rabit Population
if cRP >= 10:
cRP -= 1
#Can the fox reproduce?
foxBirthsRandom = random.random()
#print 'foxBirthsRandom ' + str(foxBirthsRandom)
if foxBirthsRandom < foxGivesBirth:
CURRENTFOXPOP += 1
if foxEats == False:
#Will the fox die?
foxDiesRandom = random.random()
#print 'foxDiessRandom ' + str(foxDiesRandom)
if foxDiesRandom < foxDies:
if CURRENTFOXPOP >= 10:
CURRENTFOXPOP -= 1
CURRENTRABBITPOP = int(cRP)
#print 'CRP @ foxGrowth End ' + str(CURRENTRABBITPOP)
CURRENTFOXPOP = CURRENTFOXPOP
#print 'CFP @ foxGrowth End ' + str(CURRENTFOXPOP)
def runSimulation(numSteps):
"""
Runs the simulation for `numSteps` time steps.
Returns a tuple of two lists: (rabbit_populations, fox_populations)
where rabbit_populations is a record of the rabbit population at the
END of each time step, and fox_populations is a record of the fox population
at the END of each time step.
Both lists should be `numSteps` items long.
"""
rabitPopList = []
foxPopList = []
for i in range(numSteps):
rabbitGrowth()
foxGrowth()
rabitPopList.append(CURRENTRABBITPOP)
foxPopList.append(CURRENTFOXPOP)
return (rabitPopList, foxPopList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment