Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created October 15, 2014 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmikola/42d61a366853edb81c61 to your computer and use it in GitHub Desktop.
Save jmikola/42d61a366853edb81c61 to your computer and use it in GitHub Desktop.
Monte Carlo Pi Estimation
<?php
$iterations = isset($argv[1]) ? (int) $argv[1] : 100000000;
$randmax = mt_getrandmax();
$hits = 0;
for ($i = 0; $i < $iterations; ++$i) {
$x = mt_rand() / $randmax;
$y = mt_rand() / $randmax;
if (sqrt($x * $x + $y * $y) <= 1.0) {
++$hits;
}
}
printf("Pi: %0.6f\n", (4 * ($hits / $iterations)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment