# Monte Carlo approximation of PI

I'm trying to understand how to compute the value of Pi by means of the Monte Carlo simulation. I have a circle inside a square where the sides of the square are tangent to the circle. As data I have the number of random points and the ratio between the lenght of one side of the square and the diameter.

$$\displaystyle \frac{l}{d} = r_{a}$$

According to what I've read I have to count the number of points lying inside the circle and the total of points. The area of a circle is $A_{circle} = \pi r^2$ and the area of a square is $A_{square} = l^2$. To check if a point is inside the circle I have to check $\sqrt{x^2+y^2} <= r$

I don't know how to relate the ratio $r_a$ with the radius so to check if the point is inside or outside the circle. Any suggestion?

UPDATE

This has been a really frustrating stuff and is a really simple example of Monte Carlo approximation.

Assuming $d=1$ then I have that $\displaystyle \frac{l}{d} = l = r_a$ Therefore, to check if a random point is inside the circle I have to check only that

$$\sqrt{x^2 + y^2} ≤ \frac{1}{2}$$

or it has to be related to the ratio $r_a$ ??

• That's very simple: suppose that your circle have an unitary radius, then its area equal to $\pi$. Area of square equal to 4, so to calculate $\pi$ you simply need to count points inside a circle (say, $N_c$), divide it to total number of points (say, $N_t$) and multipy by 4: $\pi = 4\cdot\frac{N_c}{N_t}$. I did such test in my simple raytracing. Really, it gives $\pi$ with accuracy of $n$ digits, where $n = \sqrt{N_t}$. – Eddy_Em Feb 19 '13 at 11:52
• Thanks @Eddy_Em that part I know it already. The problem is that I don't know what's the radius of the circle. The only data available is the ratio between one side of the square and the diameter of the circle, $\frac{l}{d} = r_a$. From there I don't know how to calculate the radius. – BRabbit27 Feb 19 '13 at 12:14
• There's something unclear to me then. Can you explain/show a situation with r_a different from 1? – Dr_Sam Feb 19 '13 at 12:28
• Let's say we have a square and a circle where $d = l$ that means that the ratio $r_a = 1$. Now suppose I make my circle smaller but the square is the same size as before then the ratio $r_a > 1$. From here, I want to compute the value of $\pi$ but I only know the value of $r_a$, the values of $l$ and $d$ are unknown – BRabbit27 Feb 19 '13 at 12:35
• @BRabbit27 If you make the circle smaller, it will not be tangent anymore to the sides of the square. – Dr_Sam Feb 19 '13 at 12:53

Suppose, that your circle have unitary radius: $r=1$, then length of square's side equal to $l = r_a \cdot d = 2\cdot r_a$.

So, area of circle equals to $S_c = \pi$ and area of square equals to $S_s = 4 r_a^2$.

If you "throw" $N_t$ random points inside of square, a part of them (say, $N_c$) will fall into the circle. Relation $\frac{N_c}{N_t}$ equals to proportions of their areas, $\frac{S_c}{S_s}$:

$$\frac{N_c}{N_t} = \frac{S_c}{S_s}$$

Now, to calculate $\pi$ you should divide $N_c$ to $N_t$ and multipy by $S_s$:

$$\pi = 4r_a^2\cdot\frac{N_c}{N_t}$$

In case of unitary $l$ you should set $r = \frac{1}{2 r_a}$.

• I don't know why but the math formulas are not readable. I don't know if it's something wrong with my browser or you missed something. – BRabbit27 Feb 19 '13 at 13:06
• @BRabbit27, set r = 1 / (2 r_a) – Eddy_Em Feb 19 '13 at 13:07
• I understand all that. What I don't is how to check if a given point $(x,y)$ is inside the circle. It should be something like $\sqrt{x^2+y^2} <= radiusOfCircle$ but what would be that radius for any arbitrary circle inside a square? – BRabbit27 Feb 19 '13 at 13:20
• @BRabbit27, I've told you upper: if you "throw" your random numbers into square of 1x1, then check hits to circle simply: $x^2+y^2 < \frac{1}{4 r_a^2}$. – Eddy_Em Feb 19 '13 at 13:40
• (Sorry for formating: I didn't know that scicomp uses mathjax) – Eddy_Em Feb 19 '13 at 13:43

I don't think that you can find the radius $r$ with the informations that you provide. For example, you can imagine having

• a circle with diameter $d=1$ centered in a square with side $l=1$
• a circle with diameter $d=2$ centered in a square with side $l=2$

Both circles will be tangent to their respective squares and both yield $r_a=1$. So, if your data consists only in $r_a$, you cannot distinguish between the two situations.

In this context, the data $r_a$ seems even useless, since to obtain a circle tangent to the sides of the square, you need $r_a = 1$.

If you want to forget about the hypothesis that the circle is tangent to the square, you can still approximate $\pi$ for different values of $r_a$ but you need to modify your formula and this does not fix your problem with finding $r$.

One last point is that actually, $r$ does not really matter: if you have a set of random points in the square with size 1, you can scale them to make them appear in a square of size $l$ (whatever is $l$). Applying the formula to the square of size 1 or size $l$ will give you exactly the same approximation of $\pi$.

• I don't understand when you say $r$ does not really matter, if it does not, how can I compute if a point $P(x,y)$ is inside the circle? What I'm trying to understand is how to compute if the point is inside the circle, I have the formula $\sqrt{x^2 + y^2} ≤ r$ so I need something to count the number of points inside the circle... – BRabbit27 Feb 20 '13 at 7:42
• It does not matter in the sense that you can choose $r$, then take $l=d=2r$ and make your computations. Whatever value for $r$ you have taken, the result will be exactly the same. – Dr_Sam Feb 20 '13 at 7:55

yep this is just a simple Rejection Sampling application.

Just draw random numbers uniformly distributed in the space defined by the square, say (0,1) ..

keep track of which numbers you accept (fall inside the circle; radius,r=0.5), and which numbers you reject (fall outside the circle)

remember to offset the criteria for the r=sqrt(x^2+y^2) so that you are evaluating the random number as its distance from the center of the circle (say [0.5,0.5] = [x,y] if your square is (0,0),(0,1),(1,1),(1,0))

area circle: pi * r * r

area square: l*w

ratio --> accepted/(accepted+rejected) = area circle/area square

                                    = pi * r * r / l * w
= pi*(0.5*0.5)/1*1


etc.

You can just look up Rejection Sampling ... it's a Monte Carlo Method

Good Old Buffon's Needle