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.
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?
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.
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.
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().
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.