GithubHelp home page GithubHelp logo

aleksamcode / monte-carlo-pi-approximation Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 4.93 MB

Pi approximation using Monte Carlo Simulation.

C# 100.00%
monte-carlo monte-carlo-simulation number-pi pi-calculator pi-approximation c-sharp math probability-distribution sampling statistics

monte-carlo-pi-approximation's Introduction

Monte Carlo π approximation

Monte Carlo simulations are computational algorithms that rely on sampling of random numbers. Simulation of random numbers is then repeated numerous times in order to estimate something. This program allows for a numerical determining the value of $\pi$. Let's begin by observing a circle with a radius of 1 whose area is calculated as $\pi \times r^2 = \pi \times (1)^2$. Then the area of the quarter circle is $\frac{\pi}{4}$ and the area contained within the square in the first quadrant is 1. Finally, the ratio of the quarter circle to the area of the square is $R = \pi / 4$, where the ratio is denoted with an $R$. From there, we can calculate the numerical value of $\pi$ as $\pi = R \times 4$.
This way, the problem of calculating the numerical value of π is transformed into the problem of determining the ratio $R$. Simulation starts by generating a coordinate (two values, $x$ and $y$) in the first quadrant. Two random values, $x$ and $y$, are both uniformly distributed between 0 and 1. During the simulation, we count the number of times a coordinate falls within the circle quarter and the number of total generated coordinates. Then the ratio of the two areas is calculated as $R = n_{circle} / n_{total}$. To identify which coordinate fall within the circle in an ZY Cartesian coordinate system, we use the equation of the circle $(x-a)^2 + (y-a)^2 = r^2$. In this example the circle center is $(0,0)$ and the radius is 1, so the equation can be simplified to $x^2 + y^2 = r^2$. Point $(x,y)$ falls within the circle quarter if inequality $\sqrt{x^2 + y^2} < 1$ is true. By repeating this process a large number of times, we can theoretically compute the value of $\pi$ precision.

Simulation allows calculation of the number π in two ways:

  1. By setting the repetition number of a simulation.
  2. By setting the number of decimal places we want to calculate.

Note: The simulator is limited to 16 digit precision when using the second option due to limitation of type double.

Screenshot

  1. Old design before coordinate system visualization:

  2. New design with coordinate system visualization:

References

Books

To-Do List

  • Refactor code
  • Implement coordinate system visualization.
  • Remove limit when calculating number π using number of decimal places.
  • Adjust the rendering frame rate in order to avoid lagging.

monte-carlo-pi-approximation's People

Contributors

aleksamcode avatar

Watchers

 avatar  avatar  avatar

monte-carlo-pi-approximation's Issues

Animation render lagging

The application's animation is lagging, probably due to the difference between frame rate and the monitor's refresh rate.

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.