Generate Random double type number in Java

Java 8Object Oriented ProgrammingProgramming

In order to generate Random double type numbers in Java, we use the nextDouble() method of the java.util.Random class. This returns the next random double value between 0.0 (inclusive) and 1.0 (exclusive) from the random generator sequence.

Declaration - The java.util.Random.nextDouble() method is declared as follows −

public float nextDouble()

Let us see a program to generate random double type numbers in Java −

Example

 Live Demo

import java.util.Random;
public class Example {
   public static void main(String[] args) {
      Random rd = new Random(); // creating Random object
      System.out.println(rd.nextDouble()); // displaying a random double value between 0.0 & 1.0
   }
}

Output

0.40755033612457214

Note - The output might vary on Online Compilers.

raja
Published on 09-Jan-2019 11:46:56
Advertisements