views:

549

answers:

5

Random.nextGaussian() is supposed to give random no.s with mean 0 and std deviation 1. Many no.s it generated are outside range of [-1,+1]. how can i set so that it gives normally distributed random no.s only in the range -1 to 1.

+5  A: 

A normal distribution gives a non-zero (but "becoming extremely small") probability of seeing values outside [-1, +1] whatever variance you give - you're just squishing the curve, effectively.

You could use a small variance and then just run the results through a map which cropped anything less than -1 to -1, and anything greater than 1 to 1, but it wouldn't (strictly speaking) be a normal distribution any more.

What do you need this distribution for, out of interest?

Jon Skeet
It's not that small. Close to 30% of the values have to be outside 1 standard deviation. Something like 5% will lie outside 2 standard deviations.
S.Lott
The "becoming extremely small" was intended to imply that as you get further away from the mean, the probability of generating the value gets smaller, but still non-zero.
Jon Skeet
i am implementing a statistical analysis program. It uses normal distibution.
BHARATH
BHARATH, If your statistical program requires a normal distribution, why do you not want to use a normal distribution, i.e. why don't you want to let it have its natural range?
John D. Cook
+1 to John's comment.
Jon Skeet
+8  A: 

Doesn't the normal distribution include numbers arbitrarily far from the mean, but with increasingly small probabilities? It might be that your desires (normal and limited to a specific range) are incompatible.

Ned Batchelder
+7  A: 

A Gaussian distribution with a mean 0 and standard deviation one means that the average of the distribution is 0 and about 70% of the population lies in the range [-1, 1]. Ignore the numbers that are outside your range -- they form the fringe 16% approx on either side.

Maybe a better solution is to generate a distribution with mean=0 and std.dev=0.5. This will give you a distribution with about 96% of the values in the range [-1, 1].

An even better solution is to work backward as above and use the idea that approx. 99.7% of the values lie in the 3-sigma range: use a std.dev = 1/3. That will almost nullify the amount of not-so-useful values that you are getting. When you do get one, omit it.

Of course, if you are working on a math intensive product, all of this bears no value.

dirkgently
Except, of course, ignoring those numbers means that your random values aren't really normal any more, are they?
S.Lott
By that definition, any clamping you do on any random number generator is introducing a bias.
dirkgently
@dirkgently: Absolutely. It's not a normal distribution any more, just one which is "quite like" a normal distribution.
Jon Skeet
@Jon Skeet: I understand the implications of my suggestion. I'm only saying that S. Lott's observations are right on spot and the OP's question is a paradoxical one. Mais,c'est la vie.
dirkgently
+1  A: 

A standard deviation of 1.0 entails that many values will lie outside the [-1,1] range.

If you need to keep within this range, you should use another method, perhaps nextDouble().

Tor Haugen
+1  A: 

Gaussian distribution with your parameters. is has density e^(-x^2/2). In general it is of the form e^(linear(x)+linear(x^2)) which means whatever settings you give it, you have some probability of getting very large and very small numbers.
You are probably looking for some other distribution.

SurDin