Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Monte Carlo Simulation For Finding Pi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Monte Carlo Simulation for Finding pi

August 2, 2021

1 Monte Carlo Simulations


The Monte Carlo method is used to solve problems in a probabilistic manner. The initial problem
may or may not be deterministic, but randomization is used to generate an accurate solution. We
will be looking at the implementation of random number generators. We will also see an analytical
proof showing that Monte Carlo works in the specific case of finding the value of π.

1.1 Code
[1]: import numpy as np
import matplotlib.pyplot as plt
import random
from datetime import datetime

[2]: def monte_carlo_for_pi(iterations):


circ = 0
sqr = 0
now = datetime.now()
current_time = now.strftime("%S")
current_time = int(current_time)
random.seed(current_time)
for i in range(iterations):

x = random.random()
y = random.random()
if(x*x+y*y <= 1):
circ = circ + 1
sqr = sqr + 1
else:
sqr = sqr + 1
pi_val = 4*float(circ)/float(sqr)

return pi_val

[3]: iterations = input("Enter the number of iterations : ")


print(monte_carlo_for_pi(int(iterations)))

1
Enter the number of iterations : 10000
3.1496

1.2 Implementation of Random Number Generators and Simple Proof


Random number generation is the process by which numbers are produced as output by a Ran-
dom Number Generator in an order that can only occur by chance. They can be truly random
numbers, such as in Hardware Random Number Generators (which use some physical parameter
like time to generate random numbers) or numbers that are seemingly random (Pseudo-random
Number Generators). There are also hybrid methods where seeding (using the physical parameter
as in HRNG) is done an regular intervals while at all other times, pseudo-random numbers are
generated.
This is the general methodology. Implementation varies depending on the system and the pro-
gram. For example, one can generate random numbers according to probability distributions or
uniformly. The numbers generated can be restricted to a particular range (as we have done here)
or to a particular domain, such as integers or multiples of some number.
For a simple proof, one can make the following argument. Since we are generating numbers in a
uniform manner all points have an equal chance of begin picked. As a result, the probability that
probability that a particular area a is picked out of the total area A is picked is Aa and the prob-
ability that m points out of N randomly chosen points lie in the area a will follow the binomial
distribution. From statistical mechanics, we know that the mean value for the binomial distribu-
tion in the limit of N → ∞ is simply N Aa . So the number of points inside the cirular arc enclosed
2
in the square is merely N π122
. we can now easily see why for a large number of random points
generated, the ratio of points within and without the circular arc is m π
N = 4.

You might also like