Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Methods
Next article icon

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 Type

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

Java Math random() Method with Examples

The 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.5568515217910215

Example 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:

6
8
10
10
5
3
6
10
4
2


Next Article
Java Methods

N

Niraj_Pandey
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 Methods
    Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects. Example: Java program to demonstrate how to cre
    8 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(in
    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
  • Java.math.BigInteger.probablePrime() method in Java
    Prerequisite : BigInteger Basics The probablePrime() method will return a Biginteger of bitLength bits which is prime. bitLength is provided as parameter to method probablePrime() and method will return a prime BigInteger of bitLength bits. The probability that a BigInteger returned by this method i
    2 min read
  • Java Tutorial
    Java is a high-level, object-oriented programming language used to build applications across platforms—from web and mobile apps to enterprise software. It is known for its Write Once, Run Anywhere capability, meaning code written in Java can run on any device that supports the Java Virtual Machine (
    11 min read
  • Random setSeed() method in Java with Examples
    The setSeed() method of Random class sets the seed of the random number generator using a single long seed. Syntax: public void setSeed() Parameters: The function accepts a single parameter seed which is the initial seed. Return Value: This method has no return value. Exception: The function does no
    2 min read
  • Random nextDouble() method in Java with Examples
    The nextDouble() method of Random class returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence. Syntax: public double nextDouble() Parameters: The function does not accepts any parameter. Return Value: This method returns th
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences