Problem with Random.nextGaussian ()

Random.nextGaussian () should give random no values ​​with an average of 0 and a std deviation of 1. Many of them are generated outside the range [-1, + 1]. how can I configure it so that it gives normally distributed random values ​​only in the range of -1 to 1.

+6
java random
source share
6 answers

A Gaussian distribution with an average of 0 and a standard deviation means that the average value of the distribution is 0 and about 70% of the population is in the range [-1, 1]. Ignore numbers that are outside your range - they form a border of about 16% on both sides.

Perhaps the best solution is to create a distribution with mean=0 and std.dev=0.5 . This will give you a distribution with approximately 96% of the values ​​in the range [-1, 1].

An even better solution is to work backwards as described above and use the idea of ​​approx. 99.7% of the values ​​are in the 3-sigma range: use std.dev = 1/3 . This will almost nullify the number of not-so-useful values ​​you get. When you receive it, lower it.

Of course, if you are working on a mathematically intensive product, all this does not matter.

+17
source share

Doesn’t the normal distribution include numbers arbitrarily far from the average, but with increasingly smaller probabilities? Perhaps your desires (normal and limited to a certain range) are incompatible.

+10
source share

The normal distribution gives a non-zero (but "becoming extremely small") probability of seeing values ​​outside the [-1, +1] of any variation that you give - you just know how to curve.

You can use a little variance, and then just run the results through a map that cropped everything that is less than -1 -1 and something larger than 1 to 1, but that would not (strictly speaking) be a normal distribution anymore.

Why do you need this distribution out of interest?

+6
source share

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

+2
source share

A standard deviation of 1.0 means that many values ​​will fall outside the range [-1.1].

If you need to stick to this range, you should use a different method, perhaps nextDouble ().

+1
source share

This code displays the number of random Gaussian numbers per console (10 per line) and shows some statistics (lowest, highest and average).

If you try it with a small account number, random numbers will probably be in the range [-1.0 ... +1.0], and the average value may be in the range [-0.1 ... +0.1]. However, if the number exceeds 10.000, random numbers will probably fall in the range [-4.0 ... +4.0] (more incredible numbers may appear at both ends), although the average value can be in the range [-0.001 ... +0.001 ] (closer to 0).

 public static void main(String[] args) { int count = 20_000; // Generated random numbers double lowest = 0; // For statistics double highest = 0; double average = 0; Random random = new Random(); for (int i = 0; i < count; ++i) { double gaussian = random.nextGaussian(); average += gaussian; lowest = Math.min(gaussian, lowest); highest = Math.max(gaussian, highest); if (i%10 == 0) { // New line System.out.println(); } System.out.printf("%10.4f", gaussian); } // Display statistics System.out.println("\n\nNumber of generated random values following Gaussian distribution: " + count); System.out.printf("\nLowest value: %10.4f\nHighest value: %10.4f\nAverage: %10.4f", lowest, highest, (average/count)); } 
0
source share

All Articles