from random import random
from time import perf_counter
sadianshu = int(input())
start =perf_counter()
hit = 0
for i in range(sadianshu):
x = random()
y = random()
z = pow(x ** 2 + y ** 2, 0.5)
if z <= 1:
hit+=1
pi = 4*(hit/sadianshu)
print(“圆周率是:{}”.format(pi))
print(“运行时间是:{:.5f}s”.format(perf_counter()-start))
python中用蒙特卡洛的方法计算圆周率和运行时间
最新推荐文章于 2024-06-13 22:43:09 发布