Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with Random.nextGaussian()

Tags:

java

random

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.

like image 526
BHS Avatar asked Mar 10 '09 11:03

BHS


3 Answers

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.

like image 116
dirkgently Avatar answered Nov 03 '22 02:11

dirkgently


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.

like image 20
Ned Batchelder Avatar answered Nov 03 '22 02:11

Ned Batchelder


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?

like image 25
Jon Skeet Avatar answered Nov 03 '22 03:11

Jon Skeet