蒙特卡洛方法计算圆周率

#蒙特卡洛方法计算圆周率
from random import random
s=1000*1000#撒点的个数
hist=0.0#圆内的点的初值
for i in range(0,s+1):
         x=random()
         y=random()
         if pow(x**2+y**2,0.5)<=1:
                  hist+=1
print("pi is :",4*hist/s)

运行结果:

== RESTART: C:/Users/lenovo/AppData/Local/Programs/Python/Python36/calpi.py ==
pi is : 3.14204

你可能感兴趣的:(数学算法)