Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

SecureRandom getSeed() method in Java with Examples

  • Last Updated : 04 Dec, 2018

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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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:




// 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);
        }
    }
}

Output:

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

Examples 2:




// 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);
        }
    }
}

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
Recommended Articles
Page :

Start Your Coding Journey Now!