蒙特卡洛法求圆周率100亿数据

版权声明:本文为博主原创文章,但部分内容来源自互联网,大家可以随意转载,点赞或留言均可! https://blog.csdn.net/csdn_kou/article/details/83448697

代码

import time
import random
hits=0
pi=0
DARTS=100000*100000
start=time.perf_counter()
for i in range(DARTS):
    x,y=random.random(),random.random()
    dist=pow(x ** 2+y**2,0.5)
    if dist <= 1.0:
        hits+=1
pi=4*(hits/DARTS)
print("圆周率的值是{:.10f}".format(pi))
print("程序运行时间为{}s".format(time.perf_counter()-start))

100万个数据

在这里插入图片描述

100亿个数据

三个半小时运行时间
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/csdn_kou/article/details/83448697