1. Introduction

In this tutorial, we'll look at how we can generate a random number in Java.

Throughout the article, we'll focus on three related topics, generating a random integer, generating a random long, and generating a random double.

2. Generate Random Integer

Firstly, we'll examine the ways to generate a random integer.

2.1. Use java.util.Random

java.util.Random enables us to generate a random integer:

public int randomUsingRandom(int minInclusive, int maxExclusive) {
    final Random random = new Random();
    if (minInclusive == maxExclusive) {
        return minInclusive;
    }
    
    return minInclusive + random.nextInt(maxExclusive - minInclusive);
}

Here, we have a java.util.Random instance which we use to create a random integer - random.nextInt. 

2.2. Use Stream with java.util.Random

Now we'll detail another approach that relies on java.util.Random:

public int randomUsingStreams(int minInclusive, int maxExclusive) {
    final Random random = new Random();
    return random.ints(minInclusive, maxExclusive).findFirst().getAsInt();
}

Unlike the previous example, we don't need to manage the boundaries. The Random.ints method emits integers in the given range - from minInclusive to maxExclusive.

2.3. Use java.lang.Math

Next, we'll look at how we can use java.lang.Math to generate a random integer:

public int randomUsingMath(int minInclusive, int maxExclusive) {
    if (minInclusive == maxExclusive) {
        return minInclusive;
    }
    return minInclusive + (int) (Math.random() * (maxExclusive - minInclusive));
}

Similar to the java.util.Random approach, we're managing the boundaries ourselves here.

2.4. Use Apache Commons

Now, we'll use the RandomUtils class from Apache Commons Lang:

public int randomUsingCommons(int minInclusive, int maxExclusive) {
    return RandomUtils.nextInt(minInclusive, maxExclusive);
}

2.5. Use Apache Commons Math

Another approach relies on RandomDataGenerator from Apache Commons Math:

public int randomUsingCommonsMath(int minInclusive, int maxExclusive) {
    final RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    return randomDataGenerator.nextInt(minInclusive, maxExclusive - 1);
}

Note that RandomDataGenerator.nextInt treats the second parameter as inclusive, unlike the previous solutions.

3. Generate Random Long

Now that we have seen how to generate a random integer, we'll now focus on random longs. The methods are very similar changing only in detail.

3.1. Use java.util.Random

We'll first generate a random long using java.util.Random:

public long randomUsingRandom(long minInclusive, long maxExclusive) {
    final Random random = new Random();
    if (minInclusive == maxExclusive) {
        return minInclusive;
    }
    return minInclusive + (long) (random.nextDouble() * (maxExclusive - minInclusive));
}

3.2. Use Stream with java.util.Random

Next, we'll use Random.longs:

public long randomUsingStreams(long minInclusive, long maxExclusive) {
    final Random random = new Random();
    return random.longs(minInclusive, maxExclusive).findFirst().getAsLong();
}

Similar to Random.ints, we don't need to manage boundaries when using Random.longs.

3.3. Use java.lang.Math

We'll now adapt the random integer generation - using java.lang.Math - for long values:

public long randomUsingMath(long minInclusive, long maxExclusive) {
    if (minInclusive == maxExclusive) {
        return minInclusive;
    }
    return minInclusive + (long) (Math.random() * (maxExclusive - minInclusive));
}

3.4. Use Apache Commons

RandomUtils.nextLong generates a random long:

public long randomUsingCommons(long minInclusive, long maxExclusive) {
    return RandomUtils.nextLong(minInclusive, maxExclusive);
}

3.5. Use Apache Commons Math

RandomDataGenerator.nextLong is another option:

public long randomUsingCommonsMath(long minInclusive, long maxExclusive) {
    final RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    return randomDataGenerator.nextLong(minInclusive, maxExclusive - 1);
}

4. Generate Random Double

Next, we'll adapt our solutions to generate a random double.

4.1. Use java.util.Random

We'll first use java.util.Random to generate a random double:

public double randomUsingRandom(double minInclusive, double maxExclusive) {
    final Random random = new Random();
    if (minInclusive == maxExclusive) {
        return minInclusive;
    }
    return minInclusive + (random.nextDouble() * (maxExclusive - minInclusive));
}

4.2. Use Stream with java.util.Random

We'll then use Random.doubles:

public double randomUsingStreams(double minInclusive, double maxExclusive) {
    final Random random = new Random();
    return random.doubles(minInclusive, maxExclusive).findFirst().getAsDouble();
}

4.3. Use java.lang.Math

We'll now use java.lang.Math to generate a random double:

public double randomUsingMath(double minInclusive, double maxExclusive) {
    if (minInclusive == maxExclusive) {
        return minInclusive;
    }
    return minInclusive + (Math.random() * (maxExclusive - minInclusive));
}

4.4. Use Apache Commons

RandomUtils.nextDouble generates a random double:

public double randomUsingCommons(double minInclusive, double maxExclusive) {
    return RandomUtils.nextDouble(minInclusive, maxExclusive);
}

4.5. Use Apache Commons Math

Lastly, we'll use RandomDataGenerator.nextUniform:

public double randomUsingCommonsMath(double minInclusive, double maxExclusive) {
    final RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    return randomDataGenerator.nextUniform(minInclusive, maxExclusive - 1, true);
}

5. Summary

In this tutorial, we've investigated different ways to generate random numbers in Java.

We first outlined how to generate random integers. Then we adapted our methods to generate random long and double values.

Finally, check out the source code for all examples over on Github.