note 33102 deleted from function.mt-rand by bjori
| From: | bjori@php.net | Date: | Sun, 16 Apr 2006 12:28:32 +0000 |
| Subject: | note 33102 deleted from function.mt-rand by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108256@lists.php.net to get a copy of this message | ||
Note Submitter: joeldg at listbid.com
----
/* This function return a gaussian distribution of floating-point
* numbers. This implementation is recommenden by D. Knuth, but due
* originally to Marsaglia.
*
* Taken from the Comp.lang.c FAQ, maintained by Steve Summit.
* ported to php from c by Joel De Gan
* http://lucifer.intercosmos.net
*/
function gaussrand(){
#static double V1, V2, S;
$phase = 0;
#double X;
if (phase == 0) {
while ($S >= 1 || $S == 0){
$U1 = (double) rand() / 2147483647;
$U2 = (double) rand() / 2147483647;
$V1 = 2 * $U1 - 1;
$V2 = 2 * $U2 - 1;
$S = $V1 * $V1 + $V2 * $V2;
};
$X = $V1 * sqrt(-2 * log($S) / $S);
}
else {
$X = $V2 * sqrt(-2 * log($S) / $S);
}
$phase = 1 - $phase;
return $X;
}