如何在PHP中选择随机数但具有正态概率分布?

Mag*_*gie 9 php

在php中,我如何生成一个随机数,假设具有特定均值和标准devaition的正态分布?

Pau*_*aul 13

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这里使用的公式称为 [Box-Muller 变换](https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform) (2认同)