note 53784 added to function.rand
| From: | umpalump at poczta dot neostrada dot pl | Date: | Mon, 13 Jun 2005 12:32:07 +0000 |
| Subject: | note 53784 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90350@lists.php.net to get a copy of this message | ||
Random numbers with Gauss distribution (normal distribution).
A correct alghoritm. Without aproximations, like Smaaps'
It is specially usefull for simulations in physics.
Check yourself, and have a fun.
<?php
function gauss()
{ // N(0,1)
// returns random number with normal distribution:
// mean=0
// std dev=1
// auxilary vars
$x=random_0_1();
$y=random_0_1();
// two independent variables with normal distribution N(0,1)
$u=sqrt(-2*log($x))*cos(2*pi()*$y);
$v=sqrt(-2*log($x))*sin(2*pi()*$y);
// i will return only one, couse only one needed
return $u;
}
function gauss_ms($m=0.0,$s=1.0)
{ // N(m,s)
// returns random number with normal distribution:
// mean=m
// std dev=s
return gauss()*$s+$m;
}
function random_0_1()
{ // auxiliary function
// returns random number with flat distribution from 0 to 1
return (float)rand()/(float)getrandmax();
}
?>
JanS
student of astronomy
on Warsaw University
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53784
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53784&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53784&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53784&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53784&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53784&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53784&report=yes
Search -- http://master.php.net/manage/user-notes.php