This project estimates the value of π using a Monte Carlo simulation and allows the user to compare estimations to evaluate
accuracy and consistency. It uses parallel computing with Python's multiprocessing module to speed up the computation.
If a quarter of a unit circle (i.e., radius equals 1) is placed into a unit square (i.e., side equals 1), a randomly chosen
point inside the square may also be inside the quarter of the circle. By taking a large number of random points,
the relation between the number of points inside the quarter circle to the total number of points represents an approximated
area of the quarter circle. Since the area of a whole circle is given by
where A is the area and r is the radius, the area of the quarter circle of radius 1 can be written as
Thus, the estimated value of π can be calculated based on the formula:
The program simulates random points inside a unit square and counts the number of points falling inside a unit quarter circle. Then the estimation of π is conducted based on the formula above. Since random sampling introduces variation, running the estimation multiple times with a large number of random points n` leads to better accuracy.
- Interactive CLI: Choose to either estimate π, compare previous results, ot quit.
- Input validation: Ensure the number of points
nis at least 1000 and a multiple of 10. - Parallel computation: Split work across 10 CPU cores for faster execution.
- Results storage: Keeps track of all your estimations.
- Comparison tool:
- Displays a boxplot of estimated π values.
- Calculates mean estimated π and percentage error relative to
math.pi. - Provides quick analysis of your
nsufficiency.
.
├── Initialization.py # Main entry point; handles user interaction and workflow
├── Estimation.py # Contains logic for Monte Carlo π estimation
├── Comparison.py # Handles statistical analysis and visualization of results
└── README.md # Project documentation (this file)- Run the program:
python Initialization.py- Follow the prompts:
- Estimate π - enter the number of points
nand get an estimation. - Compare π values - vie statistics and boxplot of your previous estimations.
- Quit - exit the program.
- Estimate π - enter the number of points
- Python 3.11
- matplotlib (for plotting)
- multiprocessing (built-in)
- statistics (built-in)
- math (built-in)
Install dependencies:
pip install matplotlib- The number of CPU cores is fixed at 10 and may not match the actual number of cores on your machine.
- Estimations are stored during the current section only.
- The program must be run interactively.
- The results will vary even with the same input since the points are chosen randomly.