Calculating Pi Using The Monte Carlo Method In JavaScript · GitHub

Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@peterc peterc/pi.js Created February 23, 2013 13:44 Show Gist options
  • Star (3) You must be signed in to star a gist
  • Fork (1) You must be signed in to fork a gist
  • Embed Select an option
    • Embed Embed this gist in your website.
    • Share Copy sharable link for this gist.
    • Clone via HTTPS Clone using the web URL.

    No results found

    Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/peterc/5019804.js"></script>
  • Save peterc/5019804 to your computer and use it in GitHub Desktop.
Code Revisions 1 Stars 3 Forks 1 Embed Select an option
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/peterc/5019804.js"></script> Save peterc/5019804 to your computer and use it in GitHub Desktop. Download ZIP Calculating pi using the Monte Carlo method in JavaScript Raw pi.js This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
var r = 5;
var points_total = 0;
var points_inside = 0;
while (1) {
points_total++;
var x = Math.random() * r * 2 - r;
var y = Math.random() * r * 2 - r;
if (Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(r, 2))
points_inside++;
if (points_total % 10000 == 0)
console.log(points_inside + "/" + points_total + " pi == " + (4 * points_inside / points_total));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.

Tag » Approximation Of Pi Javascript