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的更多相关文章

  1. (转)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 ...

  2. Monte Carlo Method(蒙特·卡罗方法)

    0-故事: 蒙特卡罗方法是计算模拟的基础,其名字来源于世界著名的赌城——摩纳哥的蒙特卡罗. 蒙特卡罗一词来源于意大利语,是为了纪念王子摩纳哥查理三世.蒙特卡罗(MonteCarlo)虽然是个赌城,但很 ...

  3. 蒙特·卡罗方法(Monte Carlo method)

    蒙特·卡罗方法(Monte Carlo method),也称统计模拟方法,是二十世纪四十年代中期由于科学技术的发展和电子计算机的发明,而被提出的一种以概率统计理论为指导的一类非常重要的数值计算方法.是 ...

  4. 蒙特卡罗算法(Monte Carlo method)

    蒙特卡罗方法概述 蒙特卡罗方法又称统计模拟法.随机抽样技术,是一种随机模拟方法,以概率和统计理论方法为基础的一种计算方法,是使用随机数(或更常见的伪随机数)来解决很多计算问题的方法.将所求解的问题同一 ...

  5. (转)Markov Chain Monte Carlo

    Nice R Code Punning code better since 2013 RSS Blog Archives Guides Modules About Markov Chain Monte ...

  6. Introduction To Monte Carlo Methods

    Introduction To Monte Carlo Methods I’m going to keep this tutorial light on math, because the goal ...

  7. 蒙特卡罗方法、蒙特卡洛树搜索(Monte Carlo Tree Search,MCTS)初探

    1. 蒙特卡罗方法(Monte Carlo method) 0x1:从布丰投针实验说起 - 只要实验次数够多,我就能直到上帝的意图 18世纪,布丰提出以下问题:设我们有一个以平行且等距木纹铺成的地板( ...

  8. 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 ...

  9. Monte Carlo Policy Evaluation

    Model-Based and Model-Free In the previous several posts, we mainly talked about Model-Based Reinfor ...

随机推荐

  1. 在TMemo上画一条线

    var C:TControlCanvas; begin C := TControlCanvas.Create; C.Pen.Color := clRed; C.Pen.Width := ; C.Con ...

  2. Android Studio 使用笔记:Git 的配置和第一次提交到仓库

    Git客户端网址:http://git-scm.com/download/ 根据自己的使用平台下载对应的客户端.这里以Mac系统为例,当客户端软件安装配置完毕后,打开AS的配置面板,找到Git的选项 ...

  3. HDU-2710 Max Factor

    看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. [原]RobotFrameWork(二)Ride简单使用及快捷键

    一.简单示例 注意:以下操作使用到快捷键的,请先确保没有与其他软件的快捷键设置冲突,比如sogou拼音.有道词典等等 1.启动ride 启动ride方法: 1)  通过界面图标 2)  dos命令行: ...

  5. AppFabric 版本区分

    目前CSDN上可以下载到的 AppFabric有两个版本: 1, WindowsServerAppFabricSetup_x64_6.1.exe,    2010/12/3 发布的, 不推荐使用,Wi ...

  6. Unity给力插件之ShaderForge(二)

    直接上实例: 一.自定义光照: 效果图(白色点光源照射时) 图片及参数 节点编辑 备注: 1.append为追加:两个一维数值合并为一个二维数值,一个二维数值与一个一维数值合并为一个三维数值,以此类推 ...

  7. 一段网上java常见escape和unescape方法的BUG

    escape编码和unescape编码,就是将一个字符转换为16进制unicode编码,前面加%字符进行标识. 此处不再多做解释,参考这里:http://www.jb51.net/article/23 ...

  8. Javascript类型——boolean类型

    布尔值在Javascript中有两个值:true和false. 布尔值和其他数据类型的转换关系 数据类型 true false boolean true false String 任何非空字符串 &q ...

  9. 在CentOS搭建Git服务器 转

    在CentOS搭建Git服务器 来自 :http://www.jianshu.com/p/69ea5ded3ede 前言 我们可以GitHub发布一些开源代码的公共仓库,但对于私密仓库就需要收费了.公 ...

  10. mysql按字段分组并获取每个分组按照某个字段排序的前三条

    这是原始数据 想按照brand_id分组 并获取每个分组total_num最高的前3位 SQL语句为: > (select count(*) from data where brand_id = ...