RosettaCodeData/Task/Monte-Carlo-methods/PHP/monte-carlo-methods.php

11 lines
276 B
PHP

<?
$loop = 1000000; # loop to 1,000,000
$count = 0;
for ($i=0; $i<$loop; $i++) {
$x = rand() / getrandmax();
$y = rand() / getrandmax();
if(($x*$x) + ($y*$y)<=1) $count++;
}
echo "loop=".number_format($loop).", count=".number_format($count).", pi=".($count/$loop*4);
?>