Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

like image 843
Maggie Avatar asked Apr 11 '13 14:04

Maggie


People also ask

How do you generate a random number from a normal distribution?

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.

What type of random variable do we deal with normal distribution?

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 .

Do random numbers have a normal distribution?

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.

How do I generate a random number in PHP?

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.


1 Answers

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;
}
like image 86
Paul Avatar answered Sep 22 '22 21:09

Paul