Introduction
Java Random class is used to generate a stream of pseudorandom numbers. In this article, we will learn about the Java Random class, its methods, and constructors provided in the Java programming language.
What is Random Class in Java?
Random class is part of java.util package. An instance of the Java Random class is used to generate random numbers. This class provides several methods to generate random numbers of type integer, double, long, float, etc. The random number generation algorithm works on the seed value. If not provided, the seed value is created from system nano time. If two Random instances have the same seed value, then they will generate the same sequence of random numbers.
Java Random class is thread-safe. However, in the multithreaded environment, it's advised to use java.util.concurrent.ThreadLocalRandom class. Random class instances are not suitable for security-sensitive applications; better to use java.security.SecureRandom in those cases.
java.lang.Object
java.util.Random
The algorithms implemented by the Random class use a protected utility method that can supply up to 32 pseudorandomly generated bits on each invocation. This class provides various method calls to generate different random data types such as float, double, and int.
Random class declaration
public class Random
extends Object
implements Serializable
Basic Random number generator
Many times we need to generate a sequence of numbers. It is not a problem to generate a sequence of numbers. But sometimes, we need to generate random numbers. In java, we can generate random numbers by using the Random class. By using a Random class, we can generate random integers, long numbers, and double values.
The complete program for generating a random number is listed below.
import java.util.Random;
public class RandomNumberGenerator {
public static void main(String a[]){
Random rand = new Random();
System.out.println("Random Integers:");
System.out.println(rand.nextInt());
System.out.println(rand.nextInt());
System.out.println(rand.nextInt());
System.out.println("Random Double Numbers:");
System.out.println(rand.nextDouble());
System.out.println(rand.nextDouble());
System.out.println(rand.nextDouble());
System.out.println("Random Long Numbers:");
System.out.println(rand.nextLong());
System.out.println(rand.nextLong());
System.out.println(rand.nextLong());
}
}
The above program generates the following output.

Constructors of Random class
Random()
Creates a new random number generator. Its seed is initialized to a value based on the current time.
Syntax
public Random()
{
this(System.currentTimeMillis());
}
The complete program of the Random class using the Random() constructor is listed below.
import java.util.Random;
public class RandomClassExample1 {
public static void main(String[] args) {
Random random = new Random();
System.out.println(random.nextBoolean());
System.out.println(random.nextDouble());
System.out.println(random.nextFloat());
System.out.println(random.nextInt());
System.out.println(random.nextInt(20));
}
}
The above program generates the following output.

Random(long seed)
Creates a new random number generator using a specified seed.
Syntax
public Random(long seed)
{
setSeed(seed);
}
The complete program of Random class using Random(long seed) constructor is listed below.
import java.util.Random;
public class RandomClassExample2 {
public static void main(String[] args) {
Random random = new Random(100);
System.out.println("Using Random(long seed)Constructor");
System.out.println(random.nextInt());
}
}
The above program generates the following output.

Methods of Random Class
1) java.util.Random.next() Method
The next(int bits) method is used to generate the next pseudorandom number. The java.util.Random.next() method uses the bit parameter, which is random bits. The method call returns the next pseudorandom value from this random number generator sequence.
The complete program of java.util.Random.next() method is listed below.
import java.util.Random;
public class RandomClassExample3 {
public static void main(String args[]) {
// create random object
Random randomno = new Random();
// get next next pseudorandom value
int value = randomno.nextInt();
// check the value
System.out.println("Value is: " + value);
}
}
The above program generates the following output.

2) java.util.Random.nextBoolean() Method
The nextBoolean() method is used to get the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence. The java.util.Random.nextBoolean() method returns a boolean value.
The complete program of java.util.Random.next() method is listed below.
import java.util.Random;
public class RandomClassExample4 {
public static void main( String args[] ) {
// create random object
Random randomno = new Random();
// get next next boolean value
boolean value = randomno.nextBoolean();
// check the value
System.out.println("Value is: " + value);
}
}
The above program generates the following output.

3) java.util.Random.nextBytes(byte[] bytes) Method
The nextBytes(byte[] bytes) method is used to generate random bytes and places them into a user-supplied byte array. The java.util.Random.nextBytes(byte[] bytes) uses the bytes as a parameter which is the non-null byte array in which to put the random bytes.
The complete program of java.util.Random.nextBytes() method is listed below.
import java.util.Random;
public class RandomClassExample5 {
public static void main( String args[] ) {
// create random object
Random randomno = new Random();
// create a byte array
byte[] nbyte = new byte[30];
// put the next byte in the array
randomno.nextBytes(nbyte);
// check the value of the array
System.out.println("Value of byte array: " + nbyte);
}
} 









Join the conversation! Your thoughts help the community grow.