#3-3.py
#APPM4560
#Lab01
#Scott Baker
#Brennon Lee
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np

def simulateU():
    return np.random.random(1)

def nIndependentFlips():
  n = 128
  q = 0.5
  i = 0
  while (i < n):
      u = simulateU()
      if (u < q):
          print("tails")
      else:
          print("heads")
      i = i + 1

def longestTailRun():
    n = 128
    q = 0.5
    i = 0
    t = 0
    run = 0
    while (i < n):
        u = simulateU()
        if (u < q):
            t = t + 1
        else:
            t = 0
        if (t > run):
            run = t
        i = i + 1
    return run

def main():
    tailRuns = []
    i = 0
    for i in range(0, 10000):
        tailRuns.append(longestTailRun())
    n, bins, patches = plt.hist(tailRuns, 15, align='left', facecolor='green')
    plt.xlabel('Longest Tail-Run')
    plt.ylabel('Frequency')
    plt.title(r'$\mathrm{10K \ Simulations \ of \ Tn:}\ $')
    plt.grid(True)
    plt.show()

main()
