Search This Blog

Wednesday, June 5, 2013

Value of Pi hit and miss method Monte carlo integration

from pylab import*
darts=1000
hits=0
for i in range(1,darts):
    x=random()
    y=random()
    plot(x,y,'.b')
   
    dist=(x**2+y**2)
    if dist<=1.0:
        hits=hits+1.0
pi=4*(hits/darts)
print pi
cx = linspace(0,1,100)
cy = sqrt(1 - cx**2)
plot(cx, cy, 'r-', linewidth=2)
show()

No comments:

Post a Comment