Random Class in Java

Random class is used to generate pseudo-random numbers in java. An instance of this class is thread-safe. The instance of this class is however cryptographically insecure.

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 are provided in Java programming language.
 

What is Random Class in Java? 

 
Random class is part of java.util package. An instance of 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 Random class use a protected utility method than 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, 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 some times we need to generate random numbers. In java, we can generate random numbers by using Random class. By using Random class, we can generate random integers, long numbers, and double values. 
 
The complete program of generating a random number is listed below.
  1. import java.util.Random;  
  2. public class RandomNumberGenerator {  
  3.     public static void main(String a[]){  
  4.         Random rand = new Random();  
  5.         System.out.println("Random Integers:");  
  6.         System.out.println(rand.nextInt());  
  7.         System.out.println(rand.nextInt());  
  8.         System.out.println(rand.nextInt());  
  9.         System.out.println("Random Double Numbers:");  
  10.         System.out.println(rand.nextDouble());  
  11.         System.out.println(rand.nextDouble());  
  12.         System.out.println(rand.nextDouble());  
  13.         System.out.println("Random Long Numbers:");  
  14.         System.out.println(rand.nextLong());  
  15.         System.out.println(rand.nextLong());  
  16.         System.out.println(rand.nextLong());  
  17.     }  
  18.  
The above program generates the following output.
 
random-number-generator-output 
 

Constructors of Random class

 

Random()

 
Creates a new random number generator. Its seed initialized to a value based on the current time.
 
Syntax
 
public Random()  
    this(System.currentTimeMillis());  
}
 
The complete program of Random class using Random() constructor is listed below.
  1. import java.util.Random;    
  2. public class RandomClassExample1 {    
  3.  public static void main(String[] args) {    
  4.   Random random = new Random();    
  5.   System.out.println(random.nextBoolean());    
  6.   System.out.println(random.nextDouble());    
  7.   System.out.println(random.nextFloat());    
  8.   System.out.println(random.nextInt());    
  9.   System.out.println(random.nextInt(20));    
  10.  }    
  11. }   
     
The above program generates the following output.
 
random-class-example1-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.
  1. import java.util.Random;    
  2. public class RandomClassExample2 {    
  3.     public static void main(String[] args) {    
  4.         Random random = new Random(100);    
  5.         System.out.println("Using Random(long seed)Constructor");    
  6.         System.out.println(random.nextInt());    
  7.     }    
  8. }   
     
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 generators sequence.
 
The complete program of java.util.Random.next() method is listed below.
  1. import java.util.Random;    
  2. public class RandomClassExample3 {    
  3.     public static void main(String args[]) {    
  4.         // create random object    
  5.         Random randomno = new Random();    
  6.         // get next next pseudorandom value    
  7.         int value = randomno.nextInt();    
  8.         // check the value    
  9.         System.out.println("Value is: " + value);    
  10.     }    
  11. }   
     
The above program generates the following output.
 
randomclass-example3-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.
  1. import java.util.Random;    
  2. public class RandomClassExample4 {    
  3.     public static void main( String args[] ) {    
  4.         // create random object    
  5.         Random randomno = new Random();    
  6.         // get next next boolean value    
  7.         boolean value = randomno.nextBoolean();    
  8.         // check the value    
  9.         System.out.println("Value is: " + value);    
  10.     }    
  11. }   
     
The above program generates the following output.
 
randomclass-example4-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.
  1. import java.util.Random;    
  2. public class RandomClassExample5 {    
  3.     public static void main( String args[] ) {    
  4.         // create random object    
  5.         Random randomno = new Random();    
  6.         // create a byte array    
  7.         byte[] nbyte = new byte[30];    
  8.         // put the next byte in the array    
  9.         randomno.nextBytes(nbyte);    
  10.         // check the value of the array    
  11.         System.out.println("Value of byte array: " + nbyte);    
  12.     }    
  13. }   
     
The above program generates the following output.
 
randomclass-example5-output 
 

4) java.util.Random.nextDouble() Method

 
The nextDouble() method is used to get the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
 
The complete program of java.util.Random.nextDouble() method is listed below.
  1. import java.util.Random;    
  2. public class RandomClassExample6 {    
  3.     public static void main( String args[] ) {    
  4.         // create random object    
  5.         Random randomno = new Random();    
  6.         // check next double value    
  7.         System.out.println("Next double value: " + randomno.nextDouble());    
  8.     }    
  9. }   
     
The above program generates the following output.
 
randomclass-example6-output 
 

5) java.util.Random.nextFloat() Method

 
The nextFloat() method is used to get the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
 
The complete program of java.util.Random.nextFloat() method is listed below.
  1. import java.util.Random;    
  2. public class RandomClassExample7 {    
  3.     public static void main( String args[] ) {    
  4.         // create random object    
  5.         Random randomno = new Random();    
  6.         // check next float value    
  7.         System.out.println("Next float value: " + randomno.nextFloat());    
  8.     }    
  9. }   
     
The above program generates the following output.
 
randomclass-example7-output 
 

6) java.util.Random.nextGaussian() Method

 
The nextGaussian() method is used to get the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence. 
 
The complete program of java.util.Random.nextGaussian() method is listed below.
  1. import java.util.Random;    
  2. public class RandomClassExample8 {    
  3.     public static void main( String args[] ) {    
  4.         // create random object    
  5.         Random randomno = new Random();    
  6.         // check next Gaussian value      
  7.         System.out.println("Next Gaussian value: " + randomno.nextGaussian());    
  8.     }    
  9. }   
     
The above program generates the following output.
 
randomclass-example8-output 
 

7) java.util.Random.nextInt() Method

 
The nextInt() method is used to get the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
 
The complete program of java.util.Random.nextInt() method is listed below.
  1. import java.util.Random;  
  2. public class RandomClassExample9 {  
  3.     public static void main(String args[]) {  
  4.         // create random object  
  5.         Random randomno = new Random();  
  6.         // check next int value  
  7.         System.out.println("Next int value: " + randomno.nextInt());  
  8.     }  
  9.  
The above program generates the following output.
 
 
 

8) java.util.Random.nextInt(int n) Method

 
The nextInt(int n) method is used to get a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The java.util.Random.nextInt(int n) uses the n as a parameter, this is the bound on the random number to be returned. Must be positive.
 
Exception: IT throws the IllegalArgumentException if n is not positive.
 
The complete program of java.util.Random.nextInt(int n) method is listed below.
  1. import java.util.Random;  
  2. public class RandomClassExample10 {  
  3.     public static void main( String args[] ) {  
  4.         // create random object  
  5.         Random randomno = new Random();  
  6.         // check next int value  
  7.         System.out.println("Next int value: " + randomno.nextInt(10000));  
  8.     }  
  9.  
The above program generates the following output.
 
randomclass-example10-output 
 

9) java.util.Random.nextLong() Method

 
The nextLong() method is used to return the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
 
The complete program of java.util.Random.nextLong() method is listed below.
  1. import java.util.Random;  
  2. public class RandomClassExample11 {  
  3.     public static void main( String args[] ) {  
  4.         // create random object  
  5.         Random randomno = new Random();  
  6.         // get next long value  
  7.         long value = randomno.nextLong();  
  8.         // check the value  
  9.         System.out.println("Long value is: " + value);  
  10.     }  
  11.  
The above program generates the following output.
 
randomclass-example11-output 
 

10) java.util.Random.setSeed(long seed) Method

 
The setSeed(long seed) method is used to set the seed of this random number generator using a single long seed. The java.util.Random.setSeed(long seed) method uses a parameter as a seed, which is the initial seed.
 
The complete program of java.util.Random.setSeed(long seed) method is listed below.
  1. import java.util.Random;  
  2. public class RandomClassExample12 {  
  3.     public static void main( String args[] ) {  
  4.         // create random object  
  5.         Random randomno = new Random();  
  6.         // setting seed  
  7.         randomno.setSeed(20);  
  8.         // value after setting seed  
  9.         System.out.println("Object after seed: " + randomno.nextInt());  
  10.     }  
  11.  
The above program generates the following output.
 
randomclass-example12-output 
 

What is seed in Random number generator Java? 

 
The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int). Creates a new random number generator using a single long seed.
 
The invocation of new Random(seed) is equivalent to:
 
Random rnd = new Random();
rnd.setSeed(seed);
 

How to change Random class seed value?

 
Some times we need to generate the same random number sequence every time we call the sequence generator method on every call. We cannot achieve this if we use simple Random(), class constructor. We need to pass seed to the Random() constructor to generate the same random sequence. You can change the seed by calling the setSeed() method. Each time you pass the same seed, you will get the same random sequence.
 
The complete program shows the process of changing the seed value in the Random class.
  1. import java.util.Random;  
  2. public class RandomClassExample13 {  
  3.     public static void main(String a[]) {  
  4.         Random rnd = new Random(40);  
  5.         for (int i = 0; i < 3; i++) {  
  6.             System.out.println(rnd.nextInt(100));  
  7.         }  
  8.         System.out.println("Changing seed to change to sequence");  
  9.         rnd.setSeed(45);  
  10.         for (int i = 0; i < 3; i++) {  
  11.             System.out.println(rnd.nextInt(100));  
  12.         }  
  13.         System.out.println("Changing seed to change to sequence");  
  14.         rnd.setSeed(50);  
  15.         for (int i = 0; i < 3; i++) {  
  16.             System.out.println(rnd.nextInt(100));  
  17.         }  
  18.         System.out.println("Setting seed 40 to produce the previous sequence");  
  19.         rnd.setSeed(40);  
  20.         for (int i = 0; i < 3; i++) {  
  21.             System.out.println(rnd.nextInt(100));  
  22.         }  
  23.     }  
  24.  
The  above program generates the following output.
 
randomclass-example13-output 
 

Summary

 
In this article, we learned about the Random class, its methods and the constructors of the Random Class in Java programming language and how to use the Random class in the Java programs.