import random
import pylab
import numpy

# Global Variables
MAXRABBITPOP = 1000
CURRENTRABBITPOP = 500
CURRENTFOXPOP = 30

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.
    """
    global CURRENTRABBITPOP
    
    pRabbitRepro = 1 - (CURRENTRABBITPOP)/ (MAXRABBITPOP)
    if pRabbitRepro > random.uniform(0.0, 1.0):
        CURRENTRABBITPOP += 1
        return True
    else:
        return False
    
    
            
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.
    """
    global CURRENTRABBITPOP
    global CURRENTFOXPOP    
    for i in range(CURRENTFOXPOP):
        if CURRENTRABBITPOP > 10:
            if random.random() <= (CURRENTRABBITPOP/MAXRABBITPOP):
                CURRENTRABBITPOP -= 1
                # fox reproducing
                if random.random() <= (1/3):
                    CURRENTFOXPOP += 1
        else:
            # fox dying
            if random.random() <= 0.1:
                CURRENTFOXPOP -= 1
                            
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.
    """
    RabbitPop = []
    FoxPop = []
    TimeSteps = []
    for i in range(numSteps):
        rabbitGrowth()
        foxGrowth()
        RabbitPop.append(CURRENTRABBITPOP)
        FoxPop.append(CURRENTFOXPOP)
        TimeSteps.append(i)
    pylab.title("Fox and Rabbit Pop")
    pylab.xlabel("TimeStep")
    pylab.ylabel("Population")
    pylab.plot(TimeSteps, RabbitPop)
    pylab.plot(TimeSteps, FoxPop, color= "red")
    coeff = numpy.polyfit(range(len(RabbitPop)),RabbitPop, 2)
    pylab.plot(numpy.polyval(coeff, range(len(RabbitPop))))