[ACCEPTED]-problem with Random.nextGaussian()-random

Accepted answer
Score: 17

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

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

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

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

Score: 11

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

Score: 6

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

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

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

Score: 2

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

Score: 1

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

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

Score: 1

This code will display count number of random 11 Gaussian numbers to console (10 in a line) and 10 shows you some statistics (lowest, highest 9 and average) afterwards.

If you try it with 8 small count number, random numbers will be probably 7 in range [-1.0 ... +1.0] and average can 6 be in range [-0.1 ... +0.1]. However, if 5 count is above 10.000, random numbers will fall 4 probably in range [-4.0 ... +4.0] (more 3 improbable numbers can appear on both ends), although 2 average can be in range [-0.001 ... +0.001] (way 1 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));
}

More Related questions