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.


/*w  ww .j a  va 2 s .co  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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable