Java Math random() Method Last Updated : 04 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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. Declaration of Java Math random()Below is the declaration of java.lang.Math.random() method is mentioned below:public static double random()Return TypeThis method returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.Java Math random() Method with ExamplesThe Math.random() method is often used for generating random values in Java. Example 1:To show the working of java.lang.Math.random() method. java // Java program to demonstrate working // of java.lang.Math.random() method import java.lang.Math; // Driver Class 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.5568515217910215Example 2:To show the working of java.lang.Math.random() method. Now to get random integer numbers from a given fixed range, we take a min and max variable to define the range for our random numbers, both min and max are inclusive in the range. java // 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:6810105361042 Comment More infoAdvertise with us Next Article Java Math random() Method N Niraj_Pandey Follow Improve Article Tags : Java Java-lang package java-math Practice Tags : Java Similar Reads StrictMath random() Method in Java The random() is an inbuilt method of StrictMath class in java which is used to get a double value with a positive sign that is greater than or equal to 0.0 and less than 1.0. random() method is accurately organized to acquiesce appropriate use by more than one thread. The values which are returned a 2 min read Java.util.Random.nextInt() in Java Generating random numbers themselves has a good utility. Java provides a method Random.nextInt() which is the part of Random Class present in the util package. The nextInt() method is used to get the random integer values in the range of int.Syntaxint nextInt() int nextInt(int bound)int nextInt(int 4 min read Random next() method in Java with Examples The next() method of Random class returns the next pseudorandom value from the random number generator's sequence. Syntax: protected int next(int bits) Parameters: The function accepts a single parameter bits which are the random bits. Return Value: This method returns the next pseudorandom number. 1 min read Random nextLong() method in Java with Examples The nextGaussian() method of Random class returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. Syntax: public long nextLong() Parameters: The function does not accepts any parameter. Return Value: This method returns the next pseudorandom, uni 1 min read Random nextFloat() method in Java with Examples The nextFloat() method of Random class returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from the random number generator's sequence. Syntax: public float nextFloat() Parameters: The function does not accepts any parameter. Return Value: This method returns the nex 1 min read Like