How to generate random numbers using a seed in Java

In this problem, we will learn how to generate random numbers using a seed in Java. So what are random numbers? Some numbers have no correct sequence on how it is being produced and we can’t predict how the numbers are being generated. Right? Wrong. It turns out that in a computer nothing is truly random. As it uses a system clock to determine the random number it’ll show next, we can predict what the next number will be.

So, what is a seed? It is something you sow in the ground to get a plant out of. Similarly, in Java, you give seed to get a random (not truly random) number out of it. Random functions work even without a seed but if you want the number to be specifically random you’ll need to use the seed. The seed is the starting point of the randomly generated number. If we pass the same seed to two random functions they’ll produce the same results.

What is a random function in Java and how to use it?

A random function is a device we use to get a random number back from the computer. It works like this:

import java.util.Random;                //invoking Random class

public class RandomFunctionExample
{

  public static void main(String args[]) 
  {
    
    
    Random random = new Random();          //creating a new object of the random class
    System.out.println("Demonstration of using Random function without seed.");
    System.out.println(random.nextInt());     //This function generates a random Integer 
    System.out.println(random.nextFloat());    //This function generates a random Float
    System.out.println(random.nextDouble());     //This function generates a random Double
    
  }
}

The output of the following program is:

Demonstration of using Random function without seed.-740382190
0.41077667
0.5234249848577899

How to use a seed in the random function in Java

import java.util.Random;                //invoking Random class

public class RandomFunctionExample
{

  public static void main(String args[]) 
  {
    
    
    Random random1 = new Random(6);          //creating a new object of the random class with seed
    System.out.println("Demonstration of using Random function with similar seed.");
    System.out.println(random1.nextInt());     //This function generates a random Integer 
    System.out.println(random1.nextFloat());    //This function generates a random Float
    System.out.println(random1.nextDouble());     //This function generates a random Double
    
     Random random2 = new Random(6);          //creating a new object of the random class with same seed
    System.out.println(random2.nextInt());
    System.out.println(random2.nextFloat());    
    System.out.println(random2.nextDouble());

  }
}

The output of the above program:

Demonstration of using Random function with similar seed.
-1156254074
0.3695054
0.5796252073129174
-1156254074
0.3695054
0.5796252073129174

As you will notice that by providing the same seed we get the same result but if we give a different seed we will get a different result such as in the example below:

import java.util.Random;                //invoking Random class

public class RandomFunctionExample
{

  public static void main(String args[]) 
  {
    
    
    Random random1 = new Random(6);          // create a new object of the random class with a seed. 
    System.out.println("Demonstration of using Random function with different seed.");
    System.out.println(random1.nextInt());    
    System.out.println(random1.nextFloat());  
    System.out.println(random1.nextDouble());    
    System.out.println("\n");
    Random random2 = new Random(54);          //creating a new object of the random class with different seed
    System.out.println(random2.nextInt());     
    System.out.println(random2.nextFloat());    
    System.out.println(random2.nextDouble());

  }
}

Output:

Demonstration of using Random function with different seed.-11562540740.3695054
0.5796252073129174


-1162410057
0.6740209
0.29233554044436927

Leave a Reply

Your email address will not be published. Required fields are marked *