python利用Monte Carlo方法计算圆周率
作者:互联网
from random import random
def cal(n):
count = 0
for i in range(1, n):
x = random()
y = random()
if x*x + y*y <= 1:
count += 1
print("pi:", 4*count/n)
if __name__ == '__main__':
cal(10000000)
参考文献
蒙特卡洛(Monte Carlo, MCMC)方法的原理和应用[https://www.bilibili.com/video/BV17D4y1o7J2?p=3&spm_id_from=pageDriver]
标签:Monte,python,random,www,video,spm,Carlo 来源: https://blog.csdn.net/qq_41854291/article/details/122797901