When a developer builds an application, he needs to keep all things and requirements in his mind and fulfill all those to make his application reliable and meets user expectations. So, what is the need to create a random number generator?
The answer is the need to generate a random number while a programmer makes an application that requires random values in the output. For example, a developer is developing a Ludo game in Java Programming Language.
Players roll a dice to play the game and get a number in the range of 1 to 6. He will need Java random number generator to call the number in between the range of dice numbers. The developer needs to add Java random number in range or Java random number between 1 to 6. Random number generation is the process to generate numbers from a range.
Table of Contents
Random Numbers
Random numbers are the numbers that choose or select a number from a large set of numbers randomly. It uses mathematical algorithms and satisfies two conditions.
- Generates values that are uniformly distributed over a definite interval.
- Randomly generates the value that is not guessable.
In java, there are different ways of generating random integer. We will discuss three of them here in our tutorial.
java.util.Random class
To use this class, you need to import “java.util.Random” in your program. You need to create an instance of the class random or random objects first and then invoke its methods such as nextInt method for integer type, nextDouble method for double type, method nextLong() for long type, and floating-point number etc., using that class instance.
You can pass a number inside the methods as an argument, and methods will take it as the upper bound limit. For example, if you give 2 inside the method, it will generate Java random number 0 or 1.
Example Code: generateRandom.java
Java Random Number Between 0 and 1
import java.util.Random;
public class generateRandom{
public static void main(String args[])
{
// creating an instance of the Random class
Random rand = new Random();
// Generate Java random number 0 or 1
int rand_int1 = rand.nextInt(2);
// Print random number
System.out.println("0 or 1 Random Number: "+rand_int1);
}
}
Output

Math.random
Math is a built-in class in the Java programming language, and this class includes many methods. You can perform many mathematical operations using those methods.
One of those methods is the random method defined as random() that is a random number generator object, and it returns double values between 0 and 1.
We are going to generate a Java random 4-digits number by applying mathematical operations over it and it will return random integer values.
Example Code: generateRandom4.java
Java Random 4 digit Number
import java.util.*;
public class generateRandom4
{
public static void main(String args[])
{
// Generating random doubles
System.out.println("Original Random Number Generated: " + Math.random());
// Generating 4-digits number
System.out.println("4-digits Random Numeber: " + ((int)(Math.random()*9000)+1000));
}
}
Output

ThreadLocalRandom class
The ThreadLocalRandom class comes in the java.util.concurrent package of java. You can pass a value as an argument, and it will take it as the upper bound limit as we were doing in java.util.Random class.
But, you can give a generation range in it by passing two arguments: the lower range and the upper range. For example, if you pass one upper bound value 6, it will consider the range as 0 to 5. And if you are giving two values, upper and lower limits 1 and 6, it will consider the range as 1 to 6. This process is also called pseudorandom number generator.
In the below example, we will perform java generate random numbers in the range of 6, and java generates random numbers between 1 to 6.
Example Code: generateRandomR.java
Java Random Number between 1 and 6
import java.util.concurrent.ThreadLocalRandom;
public class generateRandomR
{
public static void main(String args[])
{
// Generate random numbers in range of 6
int rand_int1 = ThreadLocalRandom.current().nextInt(6);
// Generate random number between 1 to 6
int rand_int2 = ThreadLocalRandom.current().nextInt(1,6);
// Print random numbers
System.out.println("Range of 6: " + rand_int1);
System.out.println("Range between 1 to 6: " + rand_int2);
}
}
Output

Conclusion
In conclusion, we have discussed the topic of random java numbers. Three different ways of generating them, with classes and methods. We performed one coding example of each process to understand it more clearly.
After completing the code examples, we run each of the programs multiple times to see different numbers generated on every execution.
Java Related Topics: