The java.lang.Math.random() method returns a pseudorandom double type number greater than or equal to 0.0 and less than 1.0. . When this method is first called, it creates a single new pseudorandom-number generator,
exactly as if by the expression new java.util.Random.
Syntax :
public static double random() Return : This method returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.
Example 1 :To show working of java.lang.Math.random() method.
// Java program to demonstrate working // of java.lang.Math.random() method import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { // Generate random number double rand = Math.random(); // Output is different everytime this code is executed System.out.println("Random Number:" + rand); } } |
Output:
0.5568515217910215
Example 2 :To show working of java.lang.Math.random() method.
// Java program to demonstrate working // of java.lang.Math.random() method import java.lang.Math; class Gfg2 { // driver code public static void main(String args[]) { // define the range int max = 10; int min = 1; int range = max - min + 1; // generate random numbers within 1 to 10 for (int i = 0; i < 10; i++) { int rand = (int)(Math.random() * range) + min; // Output is different everytime this code is executed System.out.println(rand); } } } |
Output:
6 8 10 10 5 3 6 10 4 2
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Random vs Secure Random numbers in Java
- Random nextBoolean() method in Java with Examples
- Random nextFloat() method in Java with Examples
- Random nextDouble() method in Java with Examples
- Random nextGaussian() method in Java with Examples
- Random next() method in Java with Examples
- Random nextLong() method in Java with Examples
- Random nextBytes() method in Java with Examples
- Random setSeed() method in Java with Examples
- Java Math min() method with Examples
- Java Math max() method with Examples
- Java Math abs() method with Examples
- Java Math acos() method with Examples
- Java Math sin() method with Examples
- Java Math sinh() method with Examples
- Java Math cosh() method with Examples
- Java Math tanh() method with Examples
- Java Math cos() method with Examples
- Java Math tan() method with Examples
- Java Math copySign() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

