Java Random nextGaussian() Method
Example
Get a random number from a normal distribution:
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rand = new Random();
double value = rand.nextGaussian();
System.out.println(value);
}
}
Note: The output is different each time you run the program. Most values are between -1.0 and 1.0.
Definition and Usage
The nextGaussian() method returns a random double value from a normal distribution (also called a bell curve). In a normal distribution, values close to the average are common, and values far from the average are rare.
The average is 0.0 and the standard deviation (a measure of how far the values usually are from the average) is 1.0. About 68% of the values are between -1.0 and 1.0.
Tip: To get values around another average, multiply the result by the standard deviation you want, and add the average. For example, 170 + rand.nextGaussian() * 10 gives heights (in cm) that are often between 160 and 180.
Syntax
public double nextGaussian()
Parameter Values
None.
Technical Details
| Returns: | A random double value from a normal distribution, with an average of 0.0 and a standard deviation of 1.0. |
|---|
Related Pages
Java How To Generate Random Numbers
❮ Random Methods