user1851634
user1851634

Reputation: 33

How to create normally distributed random numbers in PHP with given standard deviation(sigma)?

I need normally distributed random numbers. Therefore I implemented the Polar Method like its mentioned here:

Polar Method

That's working good. But my problem is it only gives me standard normally distributed random numbers. But I also need some with another standard deviation(sigma).

So I would need this generator to generate normally distributed random numbers with a given standard deviation(sigma).

At the moment I am only able to create something like the blue one (standard): Graphics

Is this possible?

Upvotes: 2

Views: 1423

Answers (2)

Stochastically
Stochastically

Reputation: 7846

Standard normally distributed random numbers have a standard deviation equal to 1. If you just multiply those random numbers by X, where X is a positive number, then you create normally distributed numbers which have a standard deviation equal to X :-).

Upvotes: 2

Martin Drautzburg
Martin Drautzburg

Reputation: 5253

You may want to check out inverse transform sampling. It allows you to create random numbers of any distribution.

The basic idea is to take the density function of your desired distribution and integrate it. This gives you a CDF (cumulative distribution function). The horizontal x-axis shows the values and the vertical y-axis shows the probability that a random value is less or equal to its corresponding x-value and ranges between 0 and 1.

Then you take a stream of random numbers which are evenly distributed between 0 and 1, use them as y-values and look up the corresponing x-values. The stream of x-values will then be distributed as you had wished. This means you actually need the inverse of the CDF to look up the x-value. Hence "inverse" transform sampling.

It is not trivial to compute the ICDF for a normal distribution with given sigma, but you can look it up.

Upvotes: 1

Related Questions