How to Generate Random Integers in Java


Generating random numbers using Java is one of the most common question beginners tend to fall upon. However, the task is not at all difficult. Let us look at the three common approaches which are being followed in order to generate a Random Integer.

  • java.util.Random
  • Math.random
  • java.util.concurrent.ThreadLocalRandom

java.util.Random

In order to generate random integers using this class, we need to create an instance of the class first and then invoke methods such as nextInt(), nextDouble(), etc.
We can generate random numbers of type integers, float, double, long, Boolean using this class. Let us look at an example to know how that works.



Output

Let us look at the output of the above example code.

Random Integer using Random Class
Fig.1- Generating Random Number using java.util.Random

Math.random()

The math class contains numerous methods for performing various numeric operations such as calculating exponential values, logarithms, etc. One of these methods is random(), which returns a double value in the range of 0.0 to 1.0. But this method can generate random values of type double only.
Let us look at an example to demonstrate how Math.random() is used to generate random value.


Output

Let us look at the output of the above example code.

Random Integer using Random Method
Fig.2- Generating Random Number using Math.random

java.util.concurrent.ThreadLocalRandom

This class was introduced way back in Java 1.7 version to generate random numbers of type integer, double, Boolean, etc. Let us look at an example code to generate random numbers of type integers, double and Boolean.


Output

Let us look at the output of the above example code.

Random Integer using ThreadLocalRandom Class
Fig.3- Generating Random Number using java.util.concurrent.ThreadLocalRandom

Generate Random Number within a Range

In a scenario where the random number being generated is restricted to a certain range we use the following technique.

Output

Let us look at the output of the above example code.

Random Integer within-a Range
Fig.4- Generating Random Number within a Range

Here, the number generated will always be in between 0 - 49. Basically, we just need to pass the upper limit of the range as the parameter and it will generate a random number between 0 to upper limit – 1.

Summary

Having discussed so many methods and different scenarios of random number generation, now you can manipulate and generate a random number as per your requirement.

Read More

Author : Satyam Kumar -



You will also like:


Tail Recursion

Tail recursion is another concept associated with recursion. Learn with examples.

Learning Journal

Kafka Core Concepts

Learn Apache Kafka core concepts and build a solid foundation on Apache Kafka.

Learning Journal

What is Programming and Programming Language

"PROGRAMMING" is the word which we all come across quite often. Let us see what that means.

Learning Journal

Pattern Matching

Scala takes the credit to bring pattern matching to the center.

Learning Journal

Dawn of Bigdata

Let us go through the birth of Bigdata.

Learning Journal