Java Random Class in the java.util Package

The Random class is an object that can generate random numbers. It can generate values of various data types, such as double, float, int, and long, and it can also generate random numbers within a specific range. You can also make it generate the same sequence of random numbers each time the program runs.

Random Class Constructors

Constructor Description
Random() Default constructor. Creates a random number generator object that uses the current time as the initial value.
Random(long seed) Creates a random number generator object with a long seed value passed as an argument.

Specifying a seed value when creating a Random object means specifying the starting sequence value. If you generate random numbers after setting a seed value, the same random numbers are generated every time. The best way to get different random numbers each time the program runs is to use the current time as the seed value. Because time keeps changing, the same random numbers are not generated.

Random Methods

Method Description
void nextBytes(byte[] bytes) Fills the buffer with random values.
boolean nextBoolean() Returns a random boolean value.
double nextDouble() Returns a random double value.
float nextFloat() Returns a random float value.
int nextInt() Returns a random int value.
long nextLong() Returns a random long value.
int nextInt(int bound) Returns a random number using the given bound.
double nextGaussian() Returns a Gaussian-distributed random number as a double.
void setSeed(long seed) Sets a new seed value for the random number generator.

Random Examples

Example 1) This example gets random numbers based on the seed.

package com.devkuma.tutorial.java.util;

import java.util.Random;

public class RandomClass {

    public static void main(String[] args) {
        Random r1 = new Random();
        Random r2 = new Random(System.currentTimeMillis());
        Random r3 = new Random(10);

        for (int i = 0; i < 10; i++) {
            System.out.println("r1: " + r1.nextInt() + ", r2: " + r2.nextInt() + ", r3: " + r3.nextInt());
        }
    }
}

The execution result is as follows.

r1: 2011236985, r2: -772289311, r3: -1157793070
r1: 1578624710, r2: -604617698, r3: 1913984760
r1: -262276649, r2: 612254308, r3: 1107254586
r1: 1706079238, r2: -1664224378, r3: 1773446580
r1: -583579103, r2: -809586602, r3: 254270492
r1: 1201978631, r2: 1898792462, r3: -1408064384
r1: 1897434446, r2: 1738277915, r3: 1048475594
r1: 1847101385, r2: -1454243203, r3: 1581279777
r1: 44315199, r2: -482107837, r3: -778209333
r1: 1881949787, r2: -221323214, r3: 1532292428

r1 does not receive a seed value, r2 uses the current time as the seed, and r3 is set to the fixed value 10. If you run the program several times, you can see that r1 and r2 change periodically, while r3 does not change.

Example 2) This example gets random numbers from 0 to 99.

package com.devkuma.tutorial.javautil;

import java.util.Random;

public class RandomClass2 {

    public static void main(String[] args) {
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(r.nextInt(100));
        }
    }
}

The execution result is as follows.

42
38
33
14
21
41
11
47
96
7

When you pass 100 as the bound to the nextInt method and run it, you can see that it generates random numbers greater than or equal to 0 and less than 100.