Code Monkey home page Code Monkey logo

estimating-pi's Introduction

Estimating-Pi

alt text

This simple, yet intriguing program uses a Monte Carlo method to estimate the value of pi.

How does this work? The method is simple. Imagine a circle inscribed in a square. The probability that a random point within the square is also within the circle is in fact pi/4, since the ratio of the area of the circle to the area of the square is pi/4. (Note that the length of one side of the square is double the length of the radius of the inscribed circle.

Area of the circle: pi * r^2 Area of the quare: (2r)^2 = 4 * r^2

Program Details

The program is a simulation, which takes random samples of points within a virtual square, and determines the number of points taken that also fall within the square.

The sampling works as follows: two random values are taken, each between 0 and 1. One represents the x coordinate, and the other represents the y coordinate. All points necessarily fall within a virtual square bounded by the coordinates (0, 0), (0, 1), (1, 1), and (1, 0). If the distance from the point to the center of the square is less than the radius, or half of the length of the square (which is 0.5 in our case), then it also falls within the imaginary circle inscribed in the square.

Thus, the program holds two counters: one for the total number of points, and the other for the number of points that fall within the imaginary circle. Over sampling several points the estimated value of pi becomes closer and closer to the real value.

estimating-pi's People

Contributors

sravit1 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.