This post will discuss how to generate random numbers in Java between 0 and n, both inclusive.

1. Using Random class

The Random class from java.util package provides several methods to generate random numbers of different types, such as int, double, long, boolean, etc. We can create an instance of this class and invoke methods such as nextInt(), nextLong(), nextFloat(), nextDouble(), and nextBoolean() to get a pseudorandomly generated value of the corresponding type. These methods can optionally take an argument and return a value that is uniformly distributed between 0 and one minus the specified argument. They can also accept a range of numbers to be generated. For example, nextInt(n + 1) will generate a random number between 0 and n, and nextInt(m, n + 1) will generate a random number between m and n.

Download  Run Code

 
Java 8 introduced several new methods to the Random class, which generates a stream of pseudorandom values. One such method is ints() that returns an unlimited stream of pseudorandom int values within the specified range. For example:

Download  Run Code

2. Using Math.random() method

Another option is to use the Math.random() that returns a pseudorandom double value greater than or equal to 0.0 and less than 1.0. It internally uses Random.nextDouble() and requires about twice the time for processing. We can tweak its return value to get a random number within a specific range. For example, the expression (int)(Math.random() * (n + 1)) generates a random number between 0 and n. It works as Math.random() generates a random double value in the range [0.0, 1.0). On multiplying it by n + 1, the lower limit remains 0, but the upper limit becomes in range (n, n + 1). Now on casting it with an int, the range becomes [0, n].

Download  Run Code

3. Using ThreadLocalRandom class

The ThreadLocalRandom class from the java.util.concurrent package is a subclass of the java.util.Random class for multithreaded environments. The use of this class in concurrent programs will typically result in better performance as a random number is now generated locally in the current thread, reducing the conflicts and the overhead. We can call the static method current() to get an instance of this class and then invoke methods such as nextInt(), nextDouble(), nextLong(), etc. Like Random class, these methods can optionally take one or two arguments to specify the range of the numbers to be generated. For example, nextInt(n + 1) will generate a random number between 0 and n and nextInt(m, n + 1) will generate a random number between m and n.

Download  Run Code

4. Using SecureRandom Class

We can also use the SecureRandom class, which is a subclass of the java.util.Random class and provides a cryptographically strong random number generator. The SecureRandom class by default uses the RNG algorithm, when instantiated by using the default constructor. It inherits all the methods of the Random class, such as nextInt(), nextLong(), nextDouble(), etc. This class is available in the java.security package. Here is an example of how to use this class:

Download  Run Code

5. Using Apache Commons Math library

With Apache Commons, we can construct a RandomDataGenerator instance using the supplied RandomGenerator or default random generator as the source of randomness. We can also use the RandomData implementation, but it is deprecated now. The following code example demonstrates its usage:

Download Code

That’s all about generating random numbers in Java between 0 and n.