How can the Box-Muller method be implemented in PHP to transform uniformly distributed random numbers into normally distributed ones?
The Box-Muller method can be implemented in PHP to transform uniformly distributed random numbers into normally distributed ones by generating two random numbers between 0 and 1, transforming them into standard normal variables, and then using these variables to calculate two normally distributed random numbers.
function boxMuller() {
$u1 = rand(0, mt_getrandmax()) / mt_getrandmax();
$u2 = rand(0, mt_getrandmax()) / mt_getrandmax();
$z0 = sqrt(-2 * log($u1)) * cos(2 * pi() * $u2);
$z1 = sqrt(-2 * log($u1)) * sin(2 * pi() * $u2);
return [$z0, $z1];
}
// Generate two normally distributed random numbers
list($num1, $num2) = boxMuller();
echo "Random number 1: $num1\n";
echo "Random number 2: $num2\n";