Random Number Generator Java | Within Range | 5 Digit | Examples

Generating random numbers is very important in some application services like OTP, Random Password. Random number generator Java is easy because Java already provides built-in methods and classes.

Random Number Generator Java Within Range 5 Digit Examples

Different ways to Random number generator in Java.

Here is some way to Java Random Range.

  • Using Random class – java.util.Random
  • Using the Math package random method –Math.random (Note – Generate double in the range of 0.0 to 1.0 and not integers)
  • java.util.concurrent.ThreadLocalRandom class

Let’s start one by one method with examples

We will see the example and some explanation for how to Random Number Generator in Java.

Random class – java.util.Random

In the example, we are generating random numbers between the range of 1 – 100.

First, need to import a Random package(module) then create an object of the Random class. Then Run the Java for loop 5 times to get the number 5 times.

import java.util.Random;

public class RandomNumbers {
    public static void main(String[] args) {
        // create random object
        Random randomObj = new Random();
        //for loop
        for (int i = 1; i < 6; i++) {
            int randomNumber = randomObj.nextInt(100);
            System.out.println(i + " Random No: " + randomNumber);
        }
    }
}

Output: 1 Random No: 27
2 Random No: 90
3 Random No: 68
4 Random No: 41
5 Random No: 88

Java Math.Random

Now do the same example with Math package random method –Math.random.

But in this example will get a random number in double and no need to import any package.

public class RandomNumbers {

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println(Math.random());
        }
    }
}

Output: 0.6476096759285015
0.02079713078539991
0.7415838333842604
0.6114225690043168
0.1259080370215

java.util.concurrent.ThreadLocalRandom class

This class is Introduced in Java 1.7 to generate random numbers of type integers, doubles, Booleans etc.

Here is How to use this class to generate random numbers. You have to import a required package to get access to this class.

import java.util.concurrent.ThreadLocalRandom;

public class RandomNumbers {
    public static void main(String[] args) {
        // Generate random integers
        int randomInt = ThreadLocalRandom.current().nextInt();
        System.out.println("Random Integers in Java: " + randomInt);

        // Generate Random doubles
        double randomDob = ThreadLocalRandom.current().nextDouble();
        System.out.println("Random Doubles in Java: " + randomDob);

        // Generate random booleans
        boolean randomBool = ThreadLocalRandom.current().nextBoolean();
        System.out.println("Random Booleans in Java: " + randomBool);
    }
}

Output: Random Integers in Java: -752176930
Random Doubles in Java: 0.5973833535773254
Random Booleans in Java: true

Generate 5 digits random number in Java

Use a substring(0, 5) methods in a random java class.

Here an example of  Random Number Generator Java with 0 is inclusive and 100000 is exclusive. Then format it into 5 digits by appending zero.

import java.util.Random;

public class RandomNumbers {
    public static void main(String[] args) {
        Random random = new Random();
        int num = random.nextInt(100000);
        String formatted = String.format("%05d", num);
        System.out.println(formatted);
    }
}

Output: 01194

Question: How to Random number generator java without repetition (no duplicates)?

Answer: Use the Set data structure and use any one of the methods. Check the set if the number is not there then Store the random number in the HashSet and print the number.

See below example of Random number generator java without repetition or no duplicates program.

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class RandomNumbers {
    public static void main(String[] args) {
        Random randomObj = new Random();
        Set set=new HashSet<Integer>();

        for (int i = 1; i <= 3; i++) {
            int randomNumber = randomObj.nextInt(4);
            if (!set.contains(randomNumber)){
                set.add(randomNumber);
                System.out.println(i + " Random No: " + randomNumber);
            }else i--;
        }
    }
}

Output: 1 Random No: 3
2 Random No: 1
3 Random No: 2

Question: How to generate random numbers in java within range?

Answer: In Java 1.7+ you can do it in one line using java.util.concurrent.ThreadLocalRandom class. Check the below example of generating random numbers in java within the range of 1 to 5.

import java.util.concurrent.ThreadLocalRandom;

public class RandomNumbers {
    public static void main(String[] args) {
        int randomNo = ThreadLocalRandom.current().nextInt(1, 20);
        System.out.println("A Random number between 1 to 20 is: " + randomNo);
    }
}

Output: A Random number between 1 to 20 is: 6

Sidenotes: So this is the Java program to generate random numbers if you have any doubts and suggestions do comment below.

Same as you generated a Random number in java you can do it for java random range.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Array Length Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.