Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def withinCircle(x,y):
- if(x**2+y**2<1):
- return True
- else:
- return False
- def main():
- circleArea = 0.0
- squareArea = 0.0
- math = 3.14159
- pi = 0.0
- for i in range(0,100000):
- x = random.random()
- y = random.random()
- if withinCircle(x,y):
- circleArea=circleArea+1
- squareArea=squareArea+1
- pi = 4*circleArea/squareArea
- print "Approximate value for pi: ", pi
- print "Difference to exact value of pi: ", pi-math
- print "Error: (approx-exact)/exact=", (pi-math)/math*100, "%"
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement