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 N Niraj_Pandey Follow Improve Article Tags : Java Java-lang package java-math Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial15+ min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like