How to Generate 6 Digit Random Number Java?

Hello Friends Today, through this tutorial, I will tell you how to generate a 6 digit Unique random number in Java.

Here’s how to generate a 6-digit random number in Java:

Using `Random.nextInt(upperBound)` and ensuring 6 digits.

public class RandomNumberGenerator {
public static int generateSixDigitNumber() {
// Create a Random object
Random random = new Random();
// Ensure the number has 6 digits by adding 100000 to a random number between 0 and 999999
int randomNumber = random.nextInt(900000) + 100000;
return randomNumber;
public static void main(String[] args) {
int number = generateSixDigitNumber();
System.out.println(“Generated 6-digit number: ” + number);


1. The `generateSixDigitNumber` method creates a `Random` object.
2. It uses `random.nextInt(upperBound)` to generate a random integer between 0 (inclusive) and `upperBound` (exclusive). In this case, we set `upperBound` to 900000 to ensure the range falls within numbers less than 1 million.
3. To guarantee a 6-digit number, it adds 100000 to the generated random number, shifting the range from 0 to 999999 to 100000 to 999999 (inclusive).