Created
April 27, 2018 06:21
-
-
Save sanmai/f8ad7f27c67159778bf19b43e64ff48e to your computer and use it in GitHub Desktop.
Marsaglia polar method in PHP - stats_rand_gen_normal polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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