当前位置:网站首页>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 .
边栏推荐
- Oracle之trim,ltrim,rtrim三个函数的用法
- Current harmonic suppression strategy of grid connected inverter with PIR controller regulator
- Breakthrough in rich device platform: dayu200 based on rk3568 enters the openharmony 3.1 release trunk
- 单细胞论文记录(part8)--Cell2location maps fine-grained cell types in spatial transcriptomics
- Shengxin visualization (Part1) -- histogram
- R语言观察日志(part24)--writexl包
- 单细胞文献学习(part3)--DSTG: deconvoluting spatial transcriptomics data through graph-based AI
- Surfer grid file clipping
- Producer and consumer issues
- ERROR: cannot verify nih.at’s certificate, issued by “/C=US/O=Let‘s Encrypt/CN=R3”,wget报错
猜你喜欢

反射操作注解

Expert PID control in Simulink

Single cell thesis records (part9) -- spatial charting of single cell transcriptomes in lectures

Shengxin visualization (Part4) -- correlation diagram

SQLServer中的子查询

Shengxin visualization (Part2) -- box diagram

Pyg tutorial (7): dissecting neighborhood aggregation

Configuration files required for SSM integration and error reports caused by common configuration errors

Discrete PID control based on MATLAB

W800 chip platform enters openharmony backbone
随机推荐
Discrete PID control based on MATLAB
Single cell thesis record (part13) -- spagcn: integrating gene expression, spatial location and history to
Single cell paper record (Part14) -- costa: unsupervised revolutionary neural network learning for St analysis
Unsafe concurrency of collection classes
Difference between thread and process
单细胞论文记录(part7)--DL and alignment of spatially resolved single-cell transcriptomes with Tangram
上传文件提示 413 Request Entity Too Large 错误
单细胞论文记录(part12)--Unsupervised Spatial Embedded Deep Representation of Spatial Transcriptomics
单细胞论文记录(part6)--SpaGE: Spatial Gene Enhancement using scRNA-seq
Surfer格网文件裁剪
常用的辅助类—(重点)
idea本地运行scope
博客添加邮箱私信 快捷
New GDI functions and functions introduced in MiniGUl version 1.1.0 (II)
【Rust笔记】03-引用
四大函数式接口(必需掌握)
Callable
单细胞论文记录(part8)--Cell2location maps fine-grained cell types in spatial transcriptomics
MYSQL牛客刷题
e.hash & oldCap == 0 详细解读