SecureRandom getSeed() method in Java with Examples

The getSeed() method of java.security.SecureRandom class is used to return the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself. This call may be used to seed other random number generators.

This method is only included for backward compatibility. The caller is encouraged to use one of the alternative getInstance methods to obtain a SecureRandom object, and then call the generateSeed method to obtain seed bytes from that object.

Syntax:



public static byte[] getSeed(int numBytes)

Parameters: This method takes the number of seed bytes as a parameter to generate.

Return Value: This method returns the seed bytes.

Below are the examples to illustrate the getSeed() method:

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getSeed() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of SecureRandom
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  
            // getting the Provider of the SecureRandom sr
            // by using method getSeed()
            byte[] bb = sr.getSeed(5);
  
            // printing the byte array
            System.out.println("Seed Bytes : " + Arrays.toString(bb));
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Seed Bytes : [1, 2, 3, 4, 1]

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getSeed() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of SecureRandom
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  
            // getting the Provider of the SecureRandom sr
            // by using method getSeed()
            byte[] bb = sr.getSeed(10);
  
            // printing the byte array
            System.out.println("Seed Bytes : " + Arrays.toString(bb));
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Seed Bytes : [-64, 79, 82, -118, -97, -95, -80, -101, -40, 12]

Note:

  1. The above programs will not run on online IDE.
  2. Every time Secure Random class will generate random output.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.