########################### # Problem: Pi Again # # by Ching-Shoei Chiang # # 2013/10/07 # ########################### import math import random def MonteCarlo(n): inp = 0 for i in range(n): x=random.uniform(0,1) y=random.uniform(0,1) # the same as if math.sqrt(x*x+y*y<1): if x*x+y*y<1: inp = inp + 1 return 4*inp/n for i in [100, 1000, 10000, 100000, 1000000]: print("Pi simulates by Monte Carlo method ", i, "times is equal to ", MonteCarlo(i))