python编程问题?

用蒙特卡洛抛点法计算圆周率,对应抛点数为2的10次方,15次方,20次方,25次方。运行计算圆周率并统计运行之间。求大神解答

from random import random
from time import perf_counter

DARTS = 10000 * 10000 # 撒点总个数
hits = 0.0
start = perf_counter()
for i in range(1, DARTS + 1):
x, y = random(), random()
dist = pow(x ** 2 + y ** 2, 0.5)
if dist < 1:
hits += 1 # 落在圆内的个数
pi = 4 * (hits / DARTS)
print("圆周率的值是:{}".format(pi))
print("运行时间:{:.5f}s".format(perf_counter() - start))
温馨提示:答案为网友推荐,仅供参考
相似回答