Forum Moderators: coopster
rand(0,1) and mt_rand(0,1) are both causing me problems. They both seem to return either 0 or 1. I'm not sure if there is some setting my web host has so that the numbers are rounded or what.
I also tried rand( 0, getrandmax()/getrandmax() ) with no luck.
here is the function I'm writing:
function boxmuller()
{
$a = mt_rand(0,1);
$b = mt_rand(0,1);
$bm = sqrt( -2 * log($a) ) * cos( 2 * pi() * $b);
return $bm;
}
Any thoughts?
In case anyone is curious, Box Muller is a way to generate a standard normal distribution.
int rand (int $min, int $max)Note how the return value is an integer. So of course you aren't getting a float.
rand(0, getrandmax()/getrandmax()) is closer (you want to divide something by getrandmax()), but think about what it does: it divides getrandmax() by itself, so is equivalent to saying rand(0, 1). To compress a random number between 0 and MAX to be between 0 and 1, you need to divide the random number itself by MAX.
$number = mt_rand(1000000,9999999);
echo $number='.'.$number;
$number=floatval($number);
echo "<br>";
echo gettype ($number);
Not sure if it helps or not...
Thanks for the help!