java Random nextDouble() Method



Description

The java Random nextDouble() method is used to get the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

Declaration

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

public double nextDouble()

Parameters

NA

Return Value

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

Exception

NA

Getting a Random Double Value Example

The following example shows the usage of Java Random nextDouble() method. Firstly, we've created a Random object and then using nextDouble() we retrieved a random double 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 double value  
      System.out.println("Next double value: " + randomNo.nextDouble());
   }      
}

Output

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

Next double value: 0.17770969480553966

Getting a Random Double Value with Given Seed Example

The following example shows the usage of Java Random nextDouble() method. Firstly, we've created a Random object with a seed value and then using nextDouble() we retrieved a random double 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 double value  
      System.out.println("Next double value: " + randomNo.nextDouble());
   }      
}

Output

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

Next double value: 0.7304302967434272
java_util_random.htm
Advertisements