Java.lang.Math.random() Method



The java.lang.Math.random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random

This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.


Following is the declaration for java.lang.Math.random() method

public static double random()


  • NA

Return Value

This method returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.


  • NA


The following example shows the usage of lang.Math.random() method.

package com.tutorialspoint;

import java.lang.*;

public class MathDemo {

   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));

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

Random number 1:0.11501691809557013
Random number 2:0.15726642068533314
Highest number:0.15726642068533314