Skip to content

Instantly share code, notes, and snippets.

@anupamsharma
Created October 17, 2012 15:04
Show Gist options
  • Save anupamsharma/3906035 to your computer and use it in GitHub Desktop.
Save anupamsharma/3906035 to your computer and use it in GitHub Desktop.
This is a simple script which shows power of Monte Carlo Simulation. It calculates value of Pie using Monte Carlo Simulation.
import random
square_arm = 10000
radius = 50000
points_in_circle = []
import random
no_of_simulation = 1000000
counter = 0
random.seed(5000)
x_series = []
while (counter < no_of_simulation):
x_series.append(random.uniform(-radius,radius))
counter = counter + 1
counter = 0
y_series = []
random.seed(0)
while (counter < no_of_simulation):
y_series.append(random.uniform(-radius,radius))
counter = counter + 1
radius_square = radius * radius
counter = 0
while (counter < no_of_simulation):
x = x_series[counter]
y = y_series[counter]
if ((x*x + y*y) < radius_square):
points_in_circle.append((x, y))
counter = counter + 1
print len(points_in_circle)
from math import pi
print "Real pi/4 is " + str(pi/4)
print "Calculated pi/4 is " + str(float((len(points_in_circle))) / no_of_simulation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment