Java Utility Library

Java Random - nextGaussian() Method



The java.util.Random.nextGaussian() method 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.

Syntax

public double nextGaussian()

Parameters

No parameter is required.

Return Value

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.

Exception

NA

Example:

In the example below, the java.util.Random.nextGaussian() method is used to get pseudorandom number, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a random object
    Random rand = new Random();

    //printing normally distributed double random numbers
    for(int i = 0; i < 5; i++)
      System.out.println("Next Gaussian Value: " + rand.nextGaussian()); 
  }
}

One of the possible outcome is given below:

Next Gaussian Value: 0.786091922956519
Next Gaussian Value: 0.05360648045197623
Next Gaussian Value: -1.118697531044655
Next Gaussian Value: 1.8747141648702537
Next Gaussian Value: -0.8139458529918242

❮ Java.util - Random