This Python script estimates the value of Pi using a multiprocessing approach. The method is based on the Monte Carlo simulation, where random points are generated inside a unit square and the number of points falling inside the unit circle is counted to approximate Pi.
- Utilizes all available CPU cores for parallel computation.
- Employs the Monte Carlo method for Pi estimation.
- Python 3.x
- multiprocessing module
- Set the variable
n
to the desired number of points for the simulation. The higher the number, the more accurate the estimation (e.g.,10**8
). - Run the script:
python pi_multiprocessing.py
The script will output the estimated value of Pi based on the given number of points.
- The script divides the total number of points by the number of available CPU cores.
- Each core calculates the number of points within the unit circle for its portion of points.
- The results are aggregated and used to estimate Pi.
Note: The script prints the number of available CPUs for reference.
- Change the value of
n
for a different number of points. - Uncomment the lines setting the
pool
variable to limit the number of processes used.