Math.random() Method in Java

Math.random() Method in Java | The Math.random() method in Java returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. The returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

Prototype of Math.random() method:-
public static double random()

Math.random() Java Range:- Greater than or equal to 0.0 and less than 1.0

Java Math.random() Example

Java program to demonstrate Math.random() method. We will take a loop and call the Math.random() method multiple times. In each execution, it gives a different double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

public class Test {
  public static void main(String[] args) {

     // generate numbers
     double d1 = Math.random();
     double d2 = Math.random();

     // display generated numbers
     System.out.println(d1);
     System.out.println(d2);
  }
}

Output:-

0.6192157696340133
0.08796048507589238

Each time we get different floating-point values ranging from 0.0 to 1.0.

Note:- There are infinite floating-point numbers that are possible between 0.0 to 1.0, but the double data type gives only up to 15 to 16 decimal point precision.

If we import the Math class statically and then we can invoke the random() method without calling through its class name.

import static java.lang.Math.*;
public class Test {
  public static void main(String[] args) {
     double d = random();
     System.out.println(d);
  }
}

Output:-

0.2644603504223617

The “import static java.lang.Math.*;” statement will import all static members of the Math class. But if we want to import only the random() method of the Math class, not another static method and variables of the Math class then we can use the “import static java.lang.Math.random;” statement. Learn more about static import in Java

import static java.lang.Math.random;
public class Test {
  public static void main(String[] args) {
     System.out.println(random());
  }
}

Java Math.random() between 1 to N

By default Math.random() always generates numbers between 0.0 to 1.0, but if we want to get numbers within a specific range then we have to multiply the return value by the magnitude of the range.

Example:- If we want to generate a number between 1 to 100 using the Math.random() then we must multiply the returned value by 100.

Java program to generate floating-point numbers between 1 to 100.

public class Test {
  public static void main(String[] args) {

     // generate floating-point number b/w 1 to 100
     double d1 = Math.random()*100;
     double d2 = Math.random()*100;

     // display generated floating-point numbers
     System.out.println(d1);
     System.out.println(d2);
  }
}

Output:-

10.485508101155204
81.26887023085449

The above program generates floating-point numbers but if we want to generate integer numbers then we must type cast the result value.

Java program to generate integer numbers between 1 to 100.

public class Test {
  public static void main(String[] args) {

     // generate integer number b/w 1 to 100
     int n1 = (int) (Math.random()*100);
     int n2 = (int) (Math.random()*100);

     // display generated integer numbers
     System.out.println(n1);
     System.out.println(n2);
  }
}

Output:-

20
70

Generate Numbers within a given range in Java

The above programs generate numbers between 1 to N, but if we want to generate a number between two numbers or a given range then we must follow the below expressions,

int range = (max-min) + 1;
(Math.random() * range) + min;

Java program to generate a number between 100 to 1000

public class Test {
  public static void main(String[] args) {

     // declare max and min
     int min = 100;
     int max = 1000;

     // calculate range
     int range = (max-min) + 1;
  	 
     // generate floating-point number b/w 100 to 1000
     double d1 = Math.random()*range + min;

     // generate integer number b/w 100 to 1000
     int n1 = (int)(Math.random()*range + min);

     // display result
     System.out.println(d1);
     System.out.println(n1);
  }
}

Output:-

310.60949353170884
977

Java program to generate integer numbers between 10 to 100and 1000 to 10000

public class Test {
  public static int randomWithRange(int min, int max) {
     int range = (max-min) + 1;
     return (int)(Math.random()*range + min);
  }

  public static void main(String[] args) {

     // generate integer number b/w 10 to 100
     System.out.println(randomWithRange(10, 100));

     // generate integer number b/w 1000 to 10000
     System.out.println(randomWithRange(1000, 10000));
  }
}

Output:-

47
1703

Other Uses of Math.random() Method in Java

We can use the Math.random() number to get a random element from an array.

Java program to get a random element from an array

public class Test {
  public static void main(String[] args) {
     int[] arr = {12, 546, 87, 878, 44, 30, 99, 35, 80};

     // declare min, max
     int lower = 0;
     int upper = arr.length;

     // calculate range value (don't add 1)
     int range = (upper - lower);

     for(int i=0; i<5; i++) {
        // get random index
        int index = (int) (Math.random()*range + lower);

        // display array element at that index
        System.out.print(arr[index]+" ");
     }
  }
}

Output:-

35 99 878 80 546

How Math.random() is implemented

The Math.random() method internally 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. The nextDouble() method of the Random class is called on this pseudorandom-number generator object.

public static double random() {
   return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
private static final class RandomNumberGeneratorHolder {
   static final Random randomNumberGenerator = new Random();
}

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.

As the largest double value less than 1.0 is Math.nextDown(1.0), a value x in the closed range [x1,x2] where x1<=x2 may be defined by the statements.

double f = Math.random()/Math.nextDown(1.0);
double x = x1*(1.0 - f) + x2*f;

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *