Skip to content

Instantly share code, notes, and snippets.

@sanmai
Created April 27, 2018 06:21
Show Gist options
  • Save sanmai/f8ad7f27c67159778bf19b43e64ff48e to your computer and use it in GitHub Desktop.
Save sanmai/f8ad7f27c67159778bf19b43e64ff48e to your computer and use it in GitHub Desktop.
Marsaglia polar method in PHP - stats_rand_gen_normal polyfill
<?php
if (!function_exists('stats_rand_gen_normal')) {
function stats_rand_gen_normal($mu, $sigma) {
do {
$u = (rand() / getrandmax()) * 2 - 1;
$v = (rand() / getrandmax()) * 2 - 1;
$s = $u * $u + $v * $v;
} while ($s >= 1 || $s == 0);
$multiplier = sqrt(-2 * log($s) / $s);
return $mu + $sigma * $u * $multiplier;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment