user77005
user77005

Reputation: 1859

Whats the difference between nextGaussian and nextDouble in java Random class?

If I want to sample from a uniform distribution and get double values, I feel like I can use both Random.nextDouble() or Random.nextGaussian() in java. Can someone explain to me the difference please.

Upvotes: 1

Views: 4281

Answers (1)

Mr Rho
Mr Rho

Reputation: 578

As per the Java API docs for nextDouble:

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

and for nextGaussian:

Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

This has to do with the probability of returning certain values. For a uniform distribution, there is an equal probability of returning every value between 0.0 and 1.0. In other words, the probability of getting a 0.0 is the same as the probability of getting a 0.5 or 0.7, etc.

For the normal distribution the probability of returned values will be according to a normal distribution curve. The chances of getting 0.0 (the mean) are essentially far greater than getting -1.0 or 1.0 which is a standard deviation of 1.0 away from the mean. The further negative/positive you go from 0.0, the lower the probability of getting that number returned.

Upvotes: 12

Related Questions