当前位置:网站首页>Using Monte Carlo method to calculate pi
Using Monte Carlo method to calculate pi
2022-06-22 06:18:00 【fire2fire2】
Ideas
The area of a square is :
The area of the circle is :
The area of a circle is... Larger than that of a square :π / 4
Use Monte Carlo method to scatter points randomly in the square , The point falling in the circle / The point falling in the square ( All points ), It's about equal to the area of the circle / The square area = π / 4
The code is implemented in the first quadrant 1/4 Circle as an example .
Code
import random
def count_pi(n):
# Here we use 1/4 Circle writing code logic
i = 0
count = 0
# n Is the total number of incoming points
while i < n:
# Randomly generated x,y coordinate
# random.random() Used to generate a 0 To 1 The number of random characters of : 0 <= n < 1.0
x = random.random()
y = random.random()
# If x square + y square < 1, Description in circle
if (pow(x, 2) + pow(y, 2)) < 1:
count += 1
i += 1
# π The value of is :4 * ( The point falling in the circle / Total point )
return 4 * (count / n)
pi = count_pi(100000)
print(pi)
Running results
function 100 Time , Average. .
Mont · Carol's method (Monte Carlo method), Also known as statistical simulation method , A kind of very important numerical calculation method guided by the theory of probability and statistics . It means using random numbers ( Or more common pseudo-random numbers ) To solve a lot of computational problems .
边栏推荐
- 信息系统项目管理 - 范围管理(划重点)
- -bash: telnet: command not found的解决方法
- 【NAND文件系统】UBIFS介绍
- reduce_ Reduction in sum()_ indices
- 基于卫星测深的牙买加沿岸水深测量
- ReadWriteLock
- C#中的数组及Foreach遍历
- 单细胞论文记录(part10)--Computational challenges and opportunities in SRT data
- GeoSwath plus 技术和数据采集处理
- Geoswath plus technology and data acquisition and processing
猜你喜欢

【NAND文件系统】UBI介绍

关于jinja2 宏定义的小问题

Discrete PID control based on MATLAB

单细胞论文记录(part11)--ClusterMap for multi-scale clustering analysis of spatial gene expression

Single cell literature learning (Part2) -- stplus: a reference based method for the exact enhancement of St

leetcode每周3道(八)图之最短路
![Detailed interpretation of tab[i = (n - 1) & hash]](/img/be/3e84b3e8406833c2a235494f1a035f.png)
Detailed interpretation of tab[i = (n - 1) & hash]

Usage of trim, ltrim and rtrim functions of Oracle

Unsafe concurrency of collection classes

单细胞论文记录(part12)--Unsupervised Spatial Embedded Deep Representation of Spatial Transcriptomics
随机推荐
reduce_ Reduction in sum()_ indices
Upload file prompt 413 request entity too large error
Use of stopwatch
牛客-TOP101-BM27
Mail sending function is realized through SMTP protocol and exchange
SQLServer中的子查询
单细胞论文记录(part12)--Unsupervised Spatial Embedded Deep Representation of Spatial Transcriptomics
博客添加邮箱私信 快捷
纵向求最大最小与横向求最大最小greatest(),least(),max(),min()
On the matrix order of MNIST linear model
Idea run scope locally
e.hash & oldCap == 0 详细解读
PyG教程(7):剖析邻域聚合
Little bear school bearpi HM micro officially integrated into openharmony trunk
【NAND文件系统】UBIFS介绍
New GDI functions and functions introduced in MiniGUl version 1.1.0 (II)
从入门到精通之专家系统CLIPS(一)CLIPS初识与概述
tab[i = (n - 1) & hash] 的详细解读
Oracle之trim,ltrim,rtrim三个函数的用法
Pytest数据参数化&数据驱动