<?php
function random_float ($min,$max) {
return ($min+lcg_value()*(abs($max-$min)));
}
?>
Random numbers with Gauss distribution (normal distribution)
<?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();
}
?>
Other versions of Gauss Distribution
<?php
function gauss($algorithm = "polar") {
$randmax = 9999;
switch($algorithm) {
//polar-methode by marsaglia
case "polar":
$v = 2;
while ($v > 1) {
$u1 = rand(0, $randmax) / $randmax;
$u2 = rand(0, $randmax) / $randmax;
$v = (2 * $u1 - 1) * (2 * $u1 - 1) + (2 * $u2 - 1) * (2 * $u2 - 1);
}
return (2* $u1 - 1) * (( -2 * log($v) / $v) ^ 0.5);
// box-muller-method
case "boxmuller":
do {
$u1 = rand(0, $randmax) / $randmax;
$u2 = rand(0, $randmax) / $randmax;
$x = sqrt(-2 * log($u1)) * cos(2 * pi() * $u2);
} while (strval($x) == "1.#INF" or strval($x) == "-1.#INF");
// the check has to be done cause sometimes (1:10000)
// values such as "1.#INF" occur and i dont know why
return $x;
// twelve random numbers
case "zwoelfer":
$sum = 0;
for ($i = 0; $i < 12; $i++) {
$sum += rand(0, $randmax) / $randmax;
}
return $sum;
}
}
?>