Relevant code and results for yesterday's post on 'Estimating the value of \pi'
In [58]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [77]:
a = 100
alist = np.arange(0,a+1,1)
anotherlist = np.zeros(iterations)
count = 0.
iterations = 100
for i in xrange(iterations):
    tempx = np.random.choice(alist)
    tempy = np.random.choice(alist)
    if tempx**2 + tempy**2 < (a**2)+1:
        count += 1.
    anotherlist[i] = count/(i+1)*4.
In [78]:
print anotherlist[-1]
plt.plot(anotherlist)
    Out[78]:
In [85]:
a = 100
alist = np.arange(0,a+1,1)
andanotherlist = []
for iterations in [10+1,10**2+1,10**3+1,10**4+1,10**5+1,10**6+1]:
    #anotherlist = np.zeros(iterations)
    count = 0.
    #iterations = 100
    for i in xrange(iterations):
        tempx = np.random.choice(alist)
        tempy = np.random.choice(alist)
        if tempx**2 + tempy**2 < (a**2)+1:
            count += 1.
        #anotherlist[i] = count/(i+1)*4.
    andanotherlist.append(count/iterations*4.)
In [86]:
plt.plot(andanotherlist,'o')
    Out[86]:
In [87]:
a = 1000
alist = np.arange(0,a+1,1)
andanotherlist = []
for iterations in [10+1,10**2+1,10**3+1,10**4+1,10**5+1,10**6+1]:
    #anotherlist = np.zeros(iterations)
    count = 0.
    #iterations = 100
    for i in xrange(iterations):
        tempx = np.random.choice(alist)
        tempy = np.random.choice(alist)
        if tempx**2 + tempy**2 < (a**2)+1:
            count += 1.
        #anotherlist[i] = count/(i+1)*4.
    andanotherlist.append(count/iterations*4.)
In [88]:
plt.plot(andanotherlist,'o')
    Out[88]:
In []: