import numpy as np
import matplotlib.pyplot as plt

def plot_avg(N, ran_vals):
    ran_count = [i + 1 for i in range(0,N,int(N/500))]
    avg_vals = np.zeros(len(ran_count))

    for i in range(len(avg_vals)):
        avg_vals[i] = np.mean(ran_vals[:ran_count[i]])

    plt.plot(ran_count, avg_vals)
    plt.plot([1,N], [0.5,0.5], linestyle = '--', color='orange')
    plt.ylim(0.4,0.6)

    plt.title('Average of values generated by np.random.random('+str(N)+')')

def plot_portion(N, ran_vals):
    ran_count = [i + 1 for i in range(0, N, int(N / 50))]
    portion_below = np.zeros(len(ran_count))

    for i in range(0,len(ran_count)):
        portion_below[i] = sum(np.where(ran_vals[:ran_count[i]] < 0.5, 1, 0))/ran_count[i]

    plt.plot(ran_count[:N], portion_below[:N])
    plt.plot([1, N], [0.5, 0.5], linestyle='--', color='orange')
    plt.ylim(0.4, 0.6)
    plt.title('Proportion of values generated by np.random.random(' + str(N) + ') below 0.5')

def plot_hist(N, ran_vals):
    lims = [0.1*i for i in range(11)]

    plt.hist(ran_vals[:N], lims)
    plt.title('Histogram of values generated by np.random.random(' + str(N) + ')')

def plot_points(N, ran_vals,below, above):
    below = below[:np.searchsorted(below,N)]
    above = above[:np.searchsorted(above,N)]

    plt.plot(below, [ran_vals[b] for b in below], color = 'red', marker = '.',
             markersize = 1, linestyle='')
    plt.plot(above, [ran_vals[a] for a in above], color='green', marker='.',
             markersize=1, linestyle='')
    plt.plot([1,N],[0.5,0.5], color = 'orange', linestyle = '--' )
    plt.title('Values generated by np.random.random('+str(N)+') used in the other plots')