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]