Home calculating pi with monte carlo and while loops
Reply: 0

calculating pi with monte carlo and while loops

user844
1#
user844 Published in May 21, 2018, 2:58 am

trying to calculate pi using this while loop but can not figure out how to stop it once it hits 3.14, it just keeps going infinitely

import turtle
import math
import random

t = turtle.Turtle()
wn = turtle.Screen()
wn.setworldcoordinates(-400,-400,400,400)

t.pu()
t.speed(0)
t.goto(0,-400)
t.pd()
t.color("blue")
t.circle(400)
t.color("orange")
hits=0.0
numdarts =0
eps=0.01
actPi=3.1415
estPi=3
error=abs(actPi-estPi)



while abs(error) >= eps:

    randx=random.uniform(-1,1)
    randy=random.uniform(-1,1)
    if (randx**2+ randy**2)<1:
        hits+=1.0
    numdarts+=1
    estPi=(hits/numdarts)
    error=abs(actPi-estPi)
    t.pu()
    t.goto(400*randx,400*randy)
    t.pd()
    t.dot()


print 4*(hits/numdarts)
wn.exitonclick()

i dont know how to get the loop to stop at 3.14

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.36336 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO