How to generate Random Numbers in Java

A quick tutorial on various ways to Generate Random Numbers, like Integer, Float, Double, and Long, in Java.

Overview

There are several ways of generating random numbers in Java.

In this tutorial, we will use the java.util.Random class as well as Apache Commons Math Library to generate the numbers.

Generate Random Integer

This section covers examples of generating Random Integer numbers using plain Java and Apache Commons Math Library.

Random Integer using Plain Java (java.util.Random)

Random random = new Random();
int randomInt = random.nextInt();Code language: Java (java)

Random Integer using Apache Commons Maths

RandomDataGenerator randomGenerator = 
    new RandomDataGenerator();
int randomInt = randomGenerator
    .getRandomGenerator().nextInt();Code language: Java (java)

Generate Random Integer within Specific Range

We will see examples of Generating Random Integers within a particular Range using both Plain Java and Apache Commons Math library.

Random Integer Within Range using Plain Java (java.util.Random)

To generate Random Integer numbers within the given range, we need to use getFloat() method as shown next.

int min = 10;
int max = 20;
Random random = new Random();
int randomInt = 
    (int) (random.nextFloat() * (max - min) + min);Code language: Java (java)

Random Integer Within Range using Apache Commons Maths

int min = 10;
int max = 20;
RandomDataGenerator randomGenerator = 
    new RandomDataGenerator();
int randomInt = randomGenerator.nextInt(min, max);Code language: Java (java)

Generate Random Double

Examples of using Plain Java and Apache Commons Maths library to generate Random Double numbers

Random Double using Plain Java (java.util.Random)

Random random = new Random();
double randomDouble = random.nextDouble();Code language: Java (java)

Random Double using Apache Commons Maths

Random random = new Random();
double randomDouble = randomGenerator
    .getRandomGenerator().nextDouble();Code language: Java (java)

Generate Random Double within Specific Range

Let’s see examples of generating random Double numbers.

Random Double Within Range using Plain Java (java.util.Random)

double min = 10D;
double max = 20D;
Random random = new Random();
double randomDouble = 
    random.nextDouble() * (max - min) + min;Code language: Java (java)

Random Double Within Range using Apache Commons Maths

double min = 10D;
double max = 20D;
RandomDataGenerator randomGenerator = 
    new RandomDataGenerator();
double randomDouble = 
    randomGenerator.nextUniform(min, max);Code language: Java (java)

Generate Random Float

Next, we will see how to generate random Float numbers using Plain Java and Apache Commons Math Library.

Random Float using Plain Java (java.util.Random)

Random random = new Random();
float randomFloat = random.nextFloat();Code language: Java (java)

Random Float using Apache Commons Maths

RandomDataGenerator randomGenerator = 
    new RandomDataGenerator();
float randomFloat = randomGenerator
    .getRandomGenerator().nextFloat();Code language: Java (java)

Generate Random Float within Specific Range

Examples of generating Random float numbers within the given range.

Random Float Within Range using Plain Java (java.util.Random)

float min = 10F;
float max = 20F;
Random random = new Random();
float randomFloat = 
    random.nextFloat() * (max -min) + min;Code language: Java (java)

Random Float Within Range using Apache Commons Maths

float min = 10F;
float max = 20F;
RandomDataGenerator randomGenerator = 
    new RandomDataGenerator();
float randomFloat = randomGenerator
    .getRandomGenerator().nextFloat() * (max- min)  + min;Code language: Java (java)

Generate Random Long

Similarly, we can generate random Long numbers.

Random Long using Plain Java (java.util.Random)

Random random = new Random();
long randomLong = random.nextLong();Code language: Java (java)

Random Long using Apache Commons Maths

RandomDataGenerator randomGenerator = 
    new RandomDataGenerator();
long randomLong = randomGenerator
    .getRandomGenerator().nextLong();Code language: Java (java)

Generate Random Long within Specific Range

We will generate random Long numbers within the given range.

Random Long Within Range using Plain Java (java.util.Random)

long min = 10L;
long max = 20L;
Random random = new Random();
long randomLong = 
    (long) (random.nextFloat() * (max - min) + min);Code language: Java (java)

Random Long Within Range using Apache Commons Maths

long min = 10L;
long max = 20L;
RandomDataGenerator randomGenerator = 
    new RandomDataGenerator();
long randomLong = 
    randomGenerator.nextLong(min, max);Code language: Java (java)

Summary

This code-oriented tutorial covered various examples of generating random numbers in Java, like random integers, random floats, random doubles, and random longs. We have seen examples of generating unbounded random numbers and numbers within specific ranges. There are many ways to generate random numbers in Java, out of which we have covered the two most common ways: using Java Random class and Apache Commons Math library.

To find more Core Java Tutorials, please visit Java Tutorials.