日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

蒙特卡罗法近似求解圆周率π

發布時間:2024/7/5 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 蒙特卡罗法近似求解圆周率π 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 原理
    • 2. 模擬代碼

1. 原理

  • 給出 x∈[0,1),y∈[0,1)x \in [0,1),y\in[0,1)x[0,1),y[0,1) 的均勻分布隨機點,模擬 ttt 次,落在以 (0,0)(0,0)(0,0) 為圓心,半徑 r=1r=1r=1 的圓以內的次數為 ccc
  • 當模擬次數足夠大時,可以看成面積比 π4≈ct?π≈4c/t\frac{\pi}{4} \approx \frac{c}{t}\Rightarrow \pi \approx 4c/t4π?tc??π4c/t

2. 模擬代碼

# -*- coding:utf-8 -*- # @Python Version: 3.7 # @Time: 2020/5/2 9:02 # @Author: Michael Ming # @Website: https://michael.blog.csdn.net/ # @File: monte_carlo_cal_pi.py # @Reference: import random import matplotlib.pyplot as pltsimulations = [100, 1000, 10000] plt.figure() plt.rcParams['font.sans-serif'] = 'SimHei' # 消除中文亂碼 plotid = 1 color = ['r', 'b'] for i in range(len(simulations)):f = plt.subplot(1, 3, plotid)plotid += 1count = 0t = simulations[i]time = simulations[i]while time > 0:time -= 1x = random.random() # [0,1)y = random.random() # [0,1)val = x ** 2 + y ** 2pos = 1if (val < 1):pos = 0count += 1f.scatter(x, y, c=color[pos])pi = 4 * count / tf.set_title("模擬次數{},pi的值{:.4f}".format(t, pi)) plt.suptitle("蒙特卡羅法近似求解圓周率pi") plt.show()

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的蒙特卡罗法近似求解圆周率π的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。