Skip to content

Estimate π using statistical simulation with random points in a circle-inscribed square. Dive into the math behind π!

License

Notifications You must be signed in to change notification settings

santagatiivan/pi-estimation-using-monte-carlo-simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Estimating π using Monte Carlo Simulation

This project implements a Monte Carlo simulation to estimate the value of π. The simulation utilizes a statistical method based on randomly generating points within a square inscribed in a unit circle. By counting how many of these points fall inside the circle, we can estimate the area of the circle and thus the value of π.

Monte Carlo Method: Theory

The Monte Carlo method is a statistical technique that leverages random sampling to estimate numerical quantities. In the context of estimating π, the method operates as follows:

  1. Generating Random Points: We generate a large number of random points within a square of side length 2, centered at the origin.

  2. Checking Point Locations: For each generated point, we check whether it falls inside the unit circle inscribed in the square. We do this by calculating the Euclidean distance from the point to the origin. If this distance is less than or equal to 1, the point falls inside the circle.

  3. Estimating π: The probability that a randomly generated point falls inside the circle is proportional to the area of the circle relative to the total area of the square. We use this probability to estimate the value of π using the following formula:

π ≈ 4 * (points inside circle) / (total points generated)

The factor of 4 is included to account for the fact that the area of the square is 4 times the area of the inscribed circle.

System Requirements

  • Python 3.x
  • Tkinter (standard with Python installation)
  • Matplotlib (installable via pip install matplotlib)

Usage Guide

Installing Requirements

Ensure you have Python 3 installed on your system. You can download it from the official Python website.

Install the matplotlib module by running the command:

pip install matplotlib

Running the Simulation

  • Download the repository and navigate to the directory containing the main.py file.
  • Run the main.py file with Python. You can do this via the terminal by executing the command:
python main.py

or

python3 main.py

Using the User Interface

Once the simulation is launched, a Tkinter window will appear with the following controls:

  • Speed: Adjusts the speed of point generation.
  • Marker Size: Adjusts the size of the marker for generated points.
  • Alpha: Adjusts the opacity of the generated points.

Managing the Simulation

  • Click the "Start" button to begin the simulation. You can pause the simulation at any time by clicking "Pause" and resume it by clicking "Start" again.
  • You can also reset the simulation by clicking "Reset."
  • Once the simulation is complete, you can save the results by clicking "Save Results". By doing so, you will be prompted to choose the name of the directory where the simulation data will be stored. The program will then automatically create the directory within the default downloads folder and save the results there.

Example Output

Here's an example of output generated by the simulation:

view

About

Estimate π using statistical simulation with random points in a circle-inscribed square. Dive into the math behind π!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages