Summary: How to calculate PI? Based on Monte Carlo method
refer to: http://www.stealthcopter.com/blog/2009/09/python-calculating-pi-using-random-numbers/
During my undergraduate degree I wrote a program in fortran 95 to calculate pi using random numbers. My aim is to rewrite it efficiently in python. I know its a terrible way to calculate pi, and there are much better ways to do it but its fun!
First I’ll explain the maths so you can visualise what’s going on. As we should know _pi_ is the ratio of circle’s radius to its circumference, which is conveniently the same as the ratio of a circle’s area to the square of its radius (wiki…)
So what we are going to be doing is picking lots of random coordinates in an x-y grid and calculating if they are within the circle or the square.
We will assign the radius to be 1, because that makes it easy to work with. By default a random number in python ( random() ) will return a floating point number between 0 and 1. To test if a point is within a circle we simply use Pythagoras.
So if the sqrt(a**2+b**2)<=1 then the point lies inside the circle’s radius. In the diagram above we see that point A lies within the circle, and point B lies outside the circle.
We can really don’t need to use the whole circle as it has symmetry, so we can just take a quartre, which makes the generating of random numbers easier as you only need to use a random number for x and y between 0 and 1, rather than -1 and 1. It will look like the diagram below.
Now for a confusing bit of maths. We are calculating the ratio of the area of a circle to the area of a square.
# Area of circle
A=pi*r**2
# where r = 1
A = pi
# Area of square
A = l ** 2
# in this case (see diagram) our square's length is twice the radius
l=2*r
A=(1+1)**2 = 4 #Therefore our ratio will be pi : 4.
# Which means we must multiply our result by four to get pi.
Final version (efficient for using)
from random import *
from math import sqrt
inside=0
n=1000
for i in range(0,n):
x=random()
y=random()
if sqrt(x*x+y*y)<=1:
inside+=1
pi=4*inside/n
print pi
Below we can see the values it creates
n calc error
1 4.00000000 0.73686317
10 3.60000000 0.45840735
100 3.24000000 0.09840735
1000 3.06400000 -0.07759265
10000 3.16160000 0.02000735
100000 3.14140000 -0.00019265
1000000 3.14293600 0.00134335
10000000 3.14117920 -0.00041345
So we can see that the program quickly solves pi to about two decimal places, but it is a terribly inefficient method and will struggle to get much more accuracy than this.
Resources to check out:
Summary: How to calculate PI? Based on Monte Carlo method的更多相关文章
- (转)Monte Carlo method 蒙特卡洛方法
转载自:维基百科 蒙特卡洛方法 https://zh.wikipedia.org/wiki/%E8%92%99%E5%9C%B0%E5%8D%A1%E7%BE%85%E6%96%B9%E6%B3%9 ...
- Monte Carlo Method(蒙特·卡罗方法)
0-故事: 蒙特卡罗方法是计算模拟的基础,其名字来源于世界著名的赌城——摩纳哥的蒙特卡罗. 蒙特卡罗一词来源于意大利语,是为了纪念王子摩纳哥查理三世.蒙特卡罗(MonteCarlo)虽然是个赌城,但很 ...
- 蒙特·卡罗方法(Monte Carlo method)
蒙特·卡罗方法(Monte Carlo method),也称统计模拟方法,是二十世纪四十年代中期由于科学技术的发展和电子计算机的发明,而被提出的一种以概率统计理论为指导的一类非常重要的数值计算方法.是 ...
- 蒙特卡罗算法(Monte Carlo method)
蒙特卡罗方法概述 蒙特卡罗方法又称统计模拟法.随机抽样技术,是一种随机模拟方法,以概率和统计理论方法为基础的一种计算方法,是使用随机数(或更常见的伪随机数)来解决很多计算问题的方法.将所求解的问题同一 ...
- (转)Markov Chain Monte Carlo
Nice R Code Punning code better since 2013 RSS Blog Archives Guides Modules About Markov Chain Monte ...
- Introduction To Monte Carlo Methods
Introduction To Monte Carlo Methods I’m going to keep this tutorial light on math, because the goal ...
- 蒙特卡罗方法、蒙特卡洛树搜索(Monte Carlo Tree Search,MCTS)初探
1. 蒙特卡罗方法(Monte Carlo method) 0x1:从布丰投针实验说起 - 只要实验次数够多,我就能直到上帝的意图 18世纪,布丰提出以下问题:设我们有一个以平行且等距木纹铺成的地板( ...
- History of Monte Carlo Methods - Part 1
History of Monte Carlo Methods - Part 1 Some time ago in June 2013 I gave a lab tutorial on Monte Ca ...
- Monte Carlo Policy Evaluation
Model-Based and Model-Free In the previous several posts, we mainly talked about Model-Based Reinfor ...
随机推荐
- ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求
一.代码 1.xml(1)activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- Android ContentProvider和getContentResolver
安卓系统中的数据库SqlLite操作和java中mysql的数据库操作很不一样,造成这样的原因是因为在安卓中数据库是属于进程的不存在数据库客户端,也不存在数据库服务器. 关于SqlLite数据库的文章 ...
- bzoj1055
不难想到是一个布尔型dp, 不难想到用f[i,j,k]表示区间[i,j]能否变为字母k 不难想到对于f[i,j,k],拆[i,j]成两个区间,然后穷举k的每一个变换来判断 感觉记忆化搜索写的比较顺,就 ...
- [PeterDLax著泛函分析习题参考解答]第1章 线性空间
1. 证明定理 1. 2. 验证上述结论. 3. 证明定理 3. 4. 证明定理 4. 证明: 由 $$\bex x=\sum_{k=1}^{n-1}a_k\cdot \sum_{j=1}^{n-1} ...
- python 零散记录(一) input与raw_input 数学相关函数 转换字符串的方法
input()与raw_input(): 两者都是接受命令行输入,但区别在于,raw_input()接受原始数据(raw data). #使用input()来提示输入名字 input("en ...
- Apache.NMS.Stomp 下载
最近项目中有用到ActiveMQ, MQ服务器61613的端口是用的STOMP协议, 原来项目中有使用MQ, 但发现缺少Apache.NMS.Stomp.dll的引用,于是上官网上找,结果发现所有的A ...
- css权威指南(下)
第七章 基本视觉格式化 正常流(没有浮动和定位元素).非替换元素(包含在文档中).替换元素(用作其它内容的占位符,如img).块级元素(会和其它元素形成换行,如div).行内元素(span之类的元素) ...
- 51单片机产生1Hz-5kHz可调占空比方波
学校的课程设计,总结一下. 注意 1.高低电平的改变不适合在主函数的while循环中,因为要有数码管动态显示的延时和其它逻辑处理,时间太长会不能及时改变高低电平值. 2.中断的执行时间一定是不能超过定 ...
- win7IIS错误修改路径最全的
http://blog.csdn.net/testcs_dn/article/details/8726480 http://www.myexception.cn/asp-dotnet/1341569. ...
- 安装Office时出现windows installer服务不能更新一个或多个受保护的windows文件错误的解决方法
今天在Windows XP上安装Microsoft Office 2010时,总是遇到“Windows Installer服务不能更新一个或多个受保护的windows文件,安装失败,正在回滚更改”提示 ...