Skip to content

emrecil/pi-monte-carlo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pi-monte-carlo: Estimate π using the Monte Carlo Method



Estimation of pi with a sample size of n=10000

Getting Started

Dependencies

  • Cython is used for calculations to increase performance
  • Random points are generated using Numpy
  • Graphs are plotted using Matplotlib

Preperations

  1. Clone the repository
git clone https://github.com/emrecil/pi-monte-carlo
cd pi-monte-carlo
  1. Install requirements
pip install -r requirements.txt
  1. Build Cython file
python setup.py build_ext --inplace

Usage

To start the program run the following command

python monte_carlo_pi.py

By default 10000 random points will be generated. You can specify a custom sample size as an argument:

python monte_carlo_pi.py [sample size]

How it works

First we draw a unit square which has an Area of area of square. Then we draw a circle inside this square with a radius of 0.5. The area of this circle is area of circle.

Now we create n random points inside the square. With the ratio of the number of points inside the circle to the total number of points an approximation of π can be computed since:

and thus