Estimation of pi with a sample size of n=10000
- Cython is used for calculations to increase performance
- Random points are generated using Numpy
- Graphs are plotted using Matplotlib
- Clone the repository
git clone https://github.com/emrecil/pi-monte-carlo
cd pi-monte-carlo
- Install requirements
pip install -r requirements.txt
- Build Cython file
python setup.py build_ext --inplace
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]
First we draw a unit square which has an Area of . Then we draw a circle inside this square with a radius of 0.5. The area of this circle is .
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