Search code examples
phprandommeanstandard-deviation

how can I choose a random number but with a normal probability distribution in PHP?


In php, how can I generate a a random number assuming a normal distribution with a specific mean and standard devaition?


Solution

  • function nrand($mean, $sd){
        $x = mt_rand()/mt_getrandmax();
        $y = mt_rand()/mt_getrandmax();
        return sqrt(-2*log($x))*cos(2*pi()*$y)*$sd + $mean;
    }