views:

77

answers:

1

Is there a single pass algorithm that can produce numbers distributed over part of a normal (Gaussian) distribution?

I want to specify a base value (the center of the distribution), a standard deviation, and a min and max values.

For example I might want all values to be distributed between -0.5 and +1 standard deviations in the same ratio as would be found in a normal distribution (obviously increased to take into account the missing tails).

Obviously one can use a loop and only exit if the generated number was between the minimum and maximum, but that could go on for a long time if min/max are too close together or too far out along a tail.

I'm assuming a language with a Gaussian random number function (I'm using Java, but can read almost anything).

+3  A: 

You can compute erf for the given Gaussian at the minimum and maximum points of interest, generate the random number (uniformly) between those two values, and take inverse erf.

I know the Apache library has an erf function in Java, see here, but I'm not sure where to point you for an inverse of erf (worst case, of course, you could calculate the latter with Newton-Raphson).

(I found an algorithm for inverse erf here, with Java implementation, but can't vouch for its quality).

Alex Martelli