CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > How to Generate Random Number in Java?

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

How to Generate Random Number in Java?

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

Generating random numbers in Java is a common task that is often used in a variety of applications, such as games, simulations, and data processing. Fortunately, Java provides several convenient ways to generate random numbers. In this post, we will learn different ways to Generate Random Number in Java.

Table of Contents

Using Math.random() method
Using Random class

Using Math.random() method

One way to generate random numbers in Java is to use the Math.random() method. This method returns a random double value between 0 (inclusive) and 1 (exclusive). Here is an example of how to use the Math.random() method to generate a random integer between two values:

import java.util.Random;

public class Main {
  public static void main(String[] args) {
    int min = 10;
    int max = 20;
    int range = max - min + 1;
    int randomNumber = (int)(Math.random() * range) + min;
    System.out.println(randomNumber);
  }
}

Using Random class

Another way to generate random numbers in Java is to use the java.util.Random class. This class provides several methods for generating random numbers of different types, such as nextInt(), nextLong(), and nextDouble(). Here is an example of how to use the java.util.Random class to generate a random integer:

import java.util.Random;

public class Main {
  public static void main(String[] args) {
    Random random = new Random();
    int randomNumber = random.nextInt();
    System.out.println(randomNumber);
  }
}

You can also use the java.util.Random class to generate a random integer within a specific range by using the nextInt(int n) method, which returns a random integer between 0 (inclusive) and n (exclusive). Here is an example of how to use the nextInt(int n) method to generate a random integer between two values:

import java.util.Random;

public class Main {
  public static void main(String[] args) {
    int min = 10;
    int max = 20;
    Random random = new Random();
    int randomNumber = random.nextInt(max - min + 1) + min;
    System.out.println(randomNumber);
  }
}

It’s worth noting that the java.util.Random class is not suitable for all applications that require random numbers. For example, it is not suitable for applications that require a high level of security, such as generating secure passwords or cryptographic keys. In these cases, you should use the java.security.SecureRandom class instead.

That’s it! You now know how to generate random numbers in Java using the Math.random() method and the java.util.Random class. Whether you need to generate random numbers for a game, a simulation, or any other application, these methods will come in handy.

Related Posts:

  • Control Statements in Java
  • Math class in Java
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Integers In Java
  • List in Java
  • What is Java? Exploring Its Key Features, Benefits,…
Tags:java
Was this article helpful?
← Previous ArticleBoolean Data Type in Java
Next Article →What is Try-With-Resources in Java? A Complete Guide

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap