Thursday, October 9, 2025
HomeLanguagesJavaSecureRandom generateSeed() method in Java with Examples

SecureRandom generateSeed() method in Java with Examples

The generateSeed() 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.

Syntax:  

public byte[] generateSeed(int numBytes)

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

Return Value: This method returns the generated seed bytes.

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

Note:  

  1. This program will not run on online IDE.
  2. Every time Secure Random class will generate random output.

Example 1:  

Java




// Java program to demonstrate
// generateSeed() 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 generated seed by into byte array
            // by using method generateSeed()
            byte[] arr = sr.generateSeed(8);
 
            // printing the byte array
            System.out.println(Arrays.toString(arr));
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

[24, -16, -12, 25, -3, 66, -90, 103]

Example 2: 

Java




// Java program to demonstrate
// generateSeed() method
 
import java.security.*;
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
 
            // creating the object of SecureRandom getting the instance of TAJMAHAL
            System.out.println("Trying to get the instance from an unknown source");
            SecureRandom sr = SecureRandom.getInstance("TAJMAHAL");
 
            // getting the generated seed by into byte array
            // by using method generateSeed()
            byte[] arr = sr.generateSeed(5);
 
            // printing the byte array
            System.out.println("Seed Bytes: " + Arrays.toString(arr));
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Trying to get the instance from an unknown source
Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL SecureRandom not available

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32348 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6791 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS