Coder Social home page Coder Social logo

santagatiivan / pi-estimation-using-monte-carlo-simulation Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 70 KB

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

License: MIT License

Python 100.00%
monte-carlo-simulation pi pi-estimation pi-estimator

pi-estimation-using-monte-carlo-simulation's Introduction

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

pi-estimation-using-monte-carlo-simulation's People

Contributors

santagatiivan avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.