Home » Java programming language

Java Random nextDouble() Method with Example

Random Class nextDouble() method: Here, we are going to learn about the nextDouble() method of Random Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020

Random Class nextDouble() method

  • nextDouble() method is available in java.util package.
  • nextDouble() method is used to generate the next pseudo-random double value between the range 0.0 and 1.0 from this Random Value Generator.
  • nextDouble() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • nextDouble() method does not throw an exception at the time of returning double.

Syntax:

    public double nextDouble();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is double, it returns next pseudo-random distributed double value between the given range from this Random Generator.

Example:

// Java program to demonstrate the example 
// of double nextDouble() method of 
// Random

import java.util.*;

public class NextDoubleOfRandom {
 public static void main(String args[]) {
  // Instantiates Random object
  Random ran = new Random();

  // By using nextDouble() method is
  // to return double pseudo-random
  // value between 0.0 and 1.0 by
  // using Random Value Generator
  double val = ran.nextDouble();

  // Display 
  System.out.println("ran.nextDouble(): " + val);
 }
}

Output

RUN 1:
ran.nextDouble(): 0.663707914081102

RUN 2:
ran.nextDouble(): 0.37675184205154066

RUN 3:
ran.nextDouble(): 0.3770186607832615


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.