In php, how can I generate a a random number assuming a normal distribution with a specific mean and standard devaition?
Using the inverse function is how we will get our set of normally distributed random values. We will use the RAND() function to generate a random value between 0 and 1 on our Y-axis and then get the inverse of it with the NORM. INV function which will result in our random normal value on the X-axis.
A normally distributed random variable may be called a “normal random variable” for short. We write X ∼ N ( μ , σ ) to mean that is a random variable that is normally distributed with mean and standard deviation .
A random variable with a Gaussian distribution is said to be normally distributed, and is called a normal deviate. Normal distributions are important in statistics and are often used in the natural and social sciences to represent real-valued random variables whose distributions are not known.
The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With