java Random nextGaussian() Method



Description

The java Random nextGaussian() method is used to get the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

Declaration

Following is the declaration for java.util.Random.nextGaussian() method.

public double nextGaussian()

Parameters

NA

Return Value

The method call 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

Getting a Random Gaussian Value Example

The following example shows the usage of Java Random nextGaussian() method. Firstly, we've created a Random object and then using nextGaussian() we retrieved a random gaussian value and printed it.

package com.tutorialspoint;

import java.util.Random;

public class RandomDemo {
   public static void main( String args[] ) {
      
      // create random object
      Random randomNo = new Random();
    
      // check next Gaussian value  
      System.out.println("Next Gaussian value: " + randomNo.nextGaussian());
   }      
}

Output

Let us compile and run the above program, this will produce the following result.

Next Gaussian value: 1.8233950217896269

Getting a Random Gaussian Value with a Given Seed Example

The following example shows the usage of Java Random nextGaussian() method. Firstly, we've created a Random object with a given seed and then using nextGaussian() we retrieved a random gaussian value and printed it.

package com.tutorialspoint;

import java.util.Random;

public class RandomDemo {
   public static void main( String args[] ) {
      
      // create random object
      Random randomNo = new Random(10);
    
      // check next Gaussian value  
      System.out.println("Next Gaussian value: " + randomNo.nextGaussian());
   }      
}

Output

Let us compile and run the above program, this will produce the following result.

Next Gaussian value: 0.8746788966462123
java_util_random.htm
Advertisements