How to Generate Random Numbers in Java?

How to Generate Random Numbers in Java?

2 mins read2.5K Views Comment
Updated on May 11, 2023 10:09 IST

Dive into the world of Java programming by learning how to generate random numbers. This detailed guide covers various methods, their applications, and uses in real-world scenarios. Perfect for both beginner and intermediate programmers who want to expand their Java toolkit and create more dynamic, unpredictable programs.

2023_05_Feature-Image-Templates-23.jpg

In this article, we will guide you on how to generate random numbers in Java. We will particularly focus on the predefined methods associated with generating random numbers. So, without further ado, let’s get started!

In Java, we have three ways to generate random numbers using inbuilt methods and classes; these are listed below:

Must Check: Top Free Online Java Courses and Certifications

Must Check: Top Online Java Courses and Certifications

Using the random() method of Math class

The Math class of Java has many methods to perform mathematical operations; to find the power of a number, we can use the Math.pow() method. The random() method is one of the methods of Math class by which we can find a random number between 0.0 to 1.0. Observe the below code.

Code:


 
import java.util.*;
public class Main {
public static void main(String[] args) {
// Use Math.random() method to generate random number
System.out.println(" Random number1:"+ Math.random());
System.out.println(" Random number2:"+ Math.random());
}
}
Copy code

Output:

2023_05_Using-the-random-method-of-Math-class-1.jpg

Also Read: What is Java?

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
6 weeks
β‚Ή15 K
3 months
β‚Ή2.05 K
3 months
– / –
170 hours
β‚Ή5.4 K
1 month
– / –
6 months
– / –
2 months
β‚Ή17 K
3 months
β‚Ή5.5 K
1 month

Using the Random Class

We can generate random numbers in Java using the Random class of Java.util package. Using this Random class, we can generate a random number of the types integer, float, double, long, and boolean. We have to follow below-mentioned steps to generate a number using the Random class.

  1. First, we have to import Java.util.Random class
  2. Second, create an object of Random class
  3. Invoke any of the following methods:
  • nextInt()
  • nextFloat()
  • nextDouble()
  • nextLong()
  • nextBoolean()

We can pass the argument to the above methods for placing an upper bound on the range of the number which is generated. Let’s make a program using discussed methods.

Code:


 
import java.util.Random;
public class Main {
public static void main(String[] args) {
// create instance of Random class
Random random=new Random();
//Generate random integer without upper bound
System.out.println(" Random Integer:"+ random.nextInt());
//generate random integer with upper bound 10000
//generate random integer between 0 to 999
System.out.println(" Random Integer:"+ random.nextInt(10000));
//Generate random double
System.out.println(" Random Double:"+ random.nextDouble());
//Generate random float
System.out.println(" Random Float:"+ random.nextFloat());
//Generate random long
System.out.println(" Random Long:"+ random.nextLong());
//Generate random boolean
System.out.println(" Random Boolean:"+ random.nextBoolean());
}
}
Copy code

Output:

2023_05_Using-the-Random-Class.jpg

Using the ThreadLocalRandom Class

We can generate random numbers using the ThreadLocalRandom class of Java.util.concurrent package. Observe the below code to understand how to use this class to generate random numbers.

Code:


 
import java.util.concurrent.ThreadLocalRandom;
public class Main {
public static void main(String[] args) {
//Generate random integer
System.out.println(" Random Integer:"+ ThreadLocalRandom.current().nextInt());
//Generate random double
System.out.println(" Random Double:"+ThreadLocalRandom.current().nextDouble());
//Generate random float
System.out.println(" Random Float:"+ ThreadLocalRandom.current().nextFloat());
//Generate random long
System.out.println(" Random Long:"+ ThreadLocalRandom.current().nextLong());
//Generate random boolean
System.out.println(" Random Boolean:"+ ThreadLocalRandom.current().nextBoolean());
}
}
Copy code

Output:

2023_05_Using-the-ThreadLocalRandom-Class-2.jpg

To generate a random number within a specific range then, we should go for two different ways using the formula which is mention below:

Using Random Class

Random random = new Random();
int randomNumber = random.nextInt(max-min+1)+min;

Here min and max are lower and higher limits of the range.
Observe the below code to understand how to use this in the program.
Code:


 
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random=new Random();
int min=100;
int max=500;
//Generate random integer between 100 to 500
System.out.println("Generating random number between "+min+" to "+max);
System.out.println(random.nextInt(max-min+1)+min);
System.out.println(random.nextInt(max-min+1)+min);
System.out.println(random.nextInt(max-min+1)+min);
}
}
Copy code
2023_05_Using-Math.random-method-1.jpg

Using Math.random() method

Below is the formula to generate random numbers within a specific range using Math.random() method.

Int randomNumber = min + Math.random( )*((max-min+1));

Here min and max are the lower and upper limits of the range.
Observe the below code to understand how to use this in the program.
Code:


 
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random=new Random();
int min=100;
int max=500;
//Generate random integer between 100 to 500
System.out.println("Generating random number between "+min+" to "+max);
System.out.println(random.nextInt(max-min+1)+min);
System.out.println(random.nextInt(max-min+1)+min);
System.out.println(random.nextInt(max-min+1)+min);
}
}
Copy code

Output:

2023_05_Using-Random-class.jpg

Contributed By: Shubham Kumar

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio