以撒点模拟,计算圆内点的数目与所有正方形内点的比值,近似的得出圆周率
#蒙特卡洛方法求解圆周率
from random import random
from time import perf_counter
DARTS = 1000*1000*10
hit = 0
star = perf_counter()
for i in range(1,DARTS+1):
x,y=random(),random()
dist = pow(x ** 2+y ** 2,0.5)
if dist <=1.0:
hit+=1
pi = 4 * (hit/DARTS)
print("圆周率的值为:{}".format(pi))
print("运行时间为:{:.5f}s".format(perf_counter()-star))