What you need is a variant of a random number generator with gaussian distribution.
I modified this function from something I found on php.net:
PHP Code:
<?php
function gauss()
{
$x=random_0_1();
$y=random_0_1();
$u=sqrt(-2*log($x))*cos(2*pi()*$y);
return $u;
}
function gauss_ms($mean=0.0,$standardDev=1.0)
{
return round(abs(gauss()*$standardDev+$mean));
}
//generates a random number between 0 and 1 with psudo-even distribution
function random_0_1()
{
return (float)rand()/(float)getrandmax();
}
?>
What you'll want to do is call gauss_ms with the mean parameter as the number you want your random number to be closest to, you'll also need to use the modulus operator because there is a chance that the number generated can be higher than your max.
So if you want to generate a number close to 2 with a max of 5 do the following:
PHP Code:
<?
$x = guass_ms(2)%5;
?>
I didn't really test this code but it should work, hope it helps.
Also the solution Jeremy provided may work better but it requires a bit more code.
Last edited by NullPointer : 12-31-2007 at 02:33 PM.
|