Java Data Type Tutorial - Java Math.random()








Syntax

Math.random() has the following syntax.

public static double random()

Example

In the following code shows how to use Math.random() method.

//from   w w  w .  ja  v a2 s .c  o  m
public class Main {

   public static void main(String[] args) {

      // get two random double numbers
      double x = Math.random();
      double y = Math.random();

      // print the numbers and print the higher one
      System.out.println("Random number 1:" + x);
      System.out.println("Random number 2:" + y);
      System.out.println("Highest number:" + Math.max(x, y));

   }
}

The code above generates the following result.