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

A Generating random number 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.

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.

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.

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.

Output: 01194

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

Answer: Use the Set data structure and use any one of 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.

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 below example of generating random numbers in java within range of 1 to 5.

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

Sidenotes: So this is the Java programs to generate random numbers if you have any doubt and suggestion do comment in 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.