How to Generate Random Numbers in Java?
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.
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:
- Using the random() method of Math class
- Using the Random class
- With the help of ThreadLocalRandom class
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()); }}
Output:
Also Read: What is Java?
Best-suited Java courses for you
Learn Java with these high-rated online courses
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.
- First, we have to import Java.util.Random class
- Second, create an object of Random class
- 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()); }}
Output:
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()); }}
Output:
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); } }
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); } }
Output:
Contributed By: Shubham Kumar
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