In [2]:
import matplotlib.pyplot as plt
import numpy as np
import timeit
from math import cos
from math import sin
from math import pi
%matplotlib inline

In [3]:
def montecarlo(nofp):
    points = np.random.rand(nofp,2)
    count = 0
    for x,y in points:
        if (x**2+y**2)<1:
            count +=1
    return(points, ((count/nofp)*4))

In [4]:
def plotdarts(points):
    x, y = [], []
    for i in range(len(points)):
        x.append(points[i][0])
        y.append(points[i][1])
    plt.plot(x, y, 'x')

In [5]:
def plotcircle():
    angles = np.linspace(0., pi/2., 30)
    circle = []
    for i in range(30):
        circle.append([cos(angles[i]), sin(angles[i])])
    xc, yc = [], []
    for i in range(30):
        xc.append(circle[i][0])
        yc.append(circle[i][1])
    plt.plot(xc, yc)

In [6]:
points, mypi = montecarlo(100)
plotdarts(points)
plotcircle()
plt.show()



In [7]:
def nmontecarlo(nofp, nofe): #paramterers: number of points, number of estimates
    estimates = []
    for i in range(nofe):
        estimates.append(montecarlo(nofp)[1])
    return(estimates)

In [22]:
histo = nmontecarlo(100,5)
nofres = {} #results
for item in histo:
    if item in nofres:
        nofres[item] += 1
    else:
        nofres[item] = 1
nofres


Out[22]:
{3.08: 2, 3.12: 2, 3.36: 1}

In [9]:
help(timeit.timeit)


Help on function timeit in module timeit:

timeit(stmt='pass', setup='pass', timer=<built-in function perf_counter>, number=1000000, globals=None)
    Convenience function to create Timer object and call timeit method.


In [10]:
timeit.timeit()


Out[10]:
0.014474469000106183

In [20]:
dict = {1:0, 2:4, 3:3, 5:2}
num = 0
if num in dict:
    dict[num] += 1
else:
    dict[num] = 1
dict


Out[20]:
{0: 1, 1: 0, 2: 4, 3: 3, 5: 2}

In [17]:
help(dict)


Help on dict object:

class dict(object)
 |  dict() -> new empty dictionary
 |  dict(mapping) -> new dictionary initialized from a mapping object's
 |      (key, value) pairs
 |  dict(iterable) -> new dictionary initialized as if via:
 |      d = {}
 |      for k, v in iterable:
 |          d[k] = v
 |  dict(**kwargs) -> new dictionary initialized with the name=value pairs
 |      in the keyword argument list.  For example:  dict(one=1, two=2)
 |  
 |  Methods defined here:
 |  
 |  __contains__(self, key, /)
 |      True if D has a key k, else False.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __sizeof__(...)
 |      D.__sizeof__() -> size of D in memory, in bytes
 |  
 |  clear(...)
 |      D.clear() -> None.  Remove all items from D.
 |  
 |  copy(...)
 |      D.copy() -> a shallow copy of D
 |  
 |  fromkeys(iterable, value=None, /) from builtins.type
 |      Returns a new dict with keys from iterable and values equal to value.
 |  
 |  get(...)
 |      D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
 |  
 |  items(...)
 |      D.items() -> a set-like object providing a view on D's items
 |  
 |  keys(...)
 |      D.keys() -> a set-like object providing a view on D's keys
 |  
 |  pop(...)
 |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
 |      If key is not found, d is returned if given, otherwise KeyError is raised
 |  
 |  popitem(...)
 |      D.popitem() -> (k, v), remove and return some (key, value) pair as a
 |      2-tuple; but raise KeyError if D is empty.
 |  
 |  setdefault(...)
 |      D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
 |  
 |  update(...)
 |      D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
 |      If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
 |      If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
 |      In either case, this is followed by: for k in F:  D[k] = F[k]
 |  
 |  values(...)
 |      D.values() -> an object providing a view on D's values
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None