Skip to content

Instantly share code, notes, and snippets.

@kezzyhko
Created November 6, 2020 08:23
Show Gist options
  • Select an option

  • Save kezzyhko/d686989929c5697f2a8a2a0795bbe17d to your computer and use it in GitHub Desktop.

Select an option

Save kezzyhko/d686989929c5697f2a8a2a0795bbe17d to your computer and use it in GitHub Desktop.
<?php
function random() {
return mt_rand() / mt_getrandmax() * 2 - 1;
}
$a = sqrt(3);
//$a = 1/2.0;
$n = 10000000;
$c = 0;
$Xsum = 0;
for ($i = 0; $i < $n; $i++) {
$x1 = random();
$x2 = random();
$Xsum += $x1;
$y1 = sqrt(1 - $x1*$x1);
if (random() >= 0) {
$y1 = -$y1;
}
$y2 = sqrt(1 - $x2*$x2);
if (random() >= 0) {
$y2 = -$y2;
}
$l = sqrt(($y1 - $y2)*($y1 - $y2) + ($x2 - $x1)*($x2 - $x1));
if ($l >= $a) $c++;
}
echo $c/$n;
echo '<br>';
echo $Xsum/$n;
<?php
function random() {
return mt_rand() / mt_getrandmax() * 2 - 1;
}
function random_angle() {
return mt_rand() / mt_getrandmax() * 360;
}
$a = sqrt(3);
//$a = 1/2.0;
$n = 100000;
$c = 0;
$Xsum = 0;
for ($i = 0; $i < $n; $i++) {
$a1 = random_angle();
$a2 = random_angle();
$x1 = sin(deg2rad($a1));
$x2 = sin(deg2rad($a2));
$Xsum += $x1;
$y1 = cos(deg2rad($a1));
$y2 = cos(deg2rad($a2));
$l = sqrt(($y1 - $y2)*($y1 - $y2) + ($x2 - $x1)*($x2 - $x1));
if ($l >= $a) $c++;
}
echo $c/$n;
echo '<br>';
echo $Xsum/$n;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment