SecureRandom setSeed() method in Java with Examples
Last Updated :
04 Dec, 2018
setSeed( byte[] seed )
The setSeed() method of java.security.SecureRandom class is used to reseeds this random object. The given seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.
Syntax:
public void setSeed(byte[] seed)
Parameters: This method takes the seed as a parameter.
Note: Every time Secure Random class will generate random output
Below are the examples to illustrate the setSeed() method:
Example 1:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
SecureRandom sr = SecureRandom.getInstance( "SHA1PRNG" );
String str = "Tajmahal" ;
byte [] b = str.getBytes();
sr.setSeed(b);
byte [] seeds = sr.getSeed( 10 );
for ( int i = 0 ; i < seeds.length; i++)
System.out.println( "Seed[" + i + "] : " + seeds[i]);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Seed[0]:69
Seed[0] : 23
Seed[1] : -99
Seed[2] : -51
Seed[3] : 107
Seed[4] : 41
Seed[5] : -96
Seed[6] : -93
Seed[7] : -86
Seed[8] : 127
Seed[9] : 47
Example 2:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
SecureRandom sr = new SecureRandom( new byte [] { 1 , 2 , 3 , 4 });
String str = "Tajmahal" ;
byte [] b = str.getBytes();
sr.setSeed(b);
byte [] seeds = sr.getSeed( 10 );
for ( int i = 0 ; i < seeds.length; i++)
System.out.println( "Seed[" + i + "]:" + seeds[i]);
}
catch (ProviderException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Seed[0]:92
Seed[1]:127
Seed[2]:28
Seed[3]:-127
Seed[4]:-100
Seed[5]:-110
Seed[6]:86
Seed[7]:-55
Seed[8]:48
Seed[9]:-78
setSeed(long seed)
The setSeed(long seed) method of java.security.SecureRandom class is used to reseeds this random object, using the eight bytes contained in the given long seed. The given seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.
Syntax:
public void setSeed( long seed )
Parameters: This method takes the seed as a parameter
Note: Every time Secure Random class will generate random output
Below are the examples to illustrate the setSeed() method:
Example 1:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
SecureRandom sr = SecureRandom.getInstance( "SHA1PRNG" , "SUN" );
sr.setSeed(101L);
byte [] seeds = sr.getSeed( 10 );
for ( int i = 0 ; i < seeds.length; i++)
System.out.println( "Seed[" + i + "] : " + seeds[i]);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Seed[0] : -36
Seed[1] : -65
Seed[2] : -94
Seed[3] : 16
Seed[4] : -104
Example 2:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
SecureRandom sr = new SecureRandom( new byte [] { 1 , 2 , 3 , 4 });
sr.setSeed(101L);
byte [] seeds = sr.getSeed( 10 );
for ( int i = 0 ; i < seeds.length; i++)
System.out.println( "Seed[" + i + "]:" + seeds[i]);
}
catch (ProviderException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Seed[0]:-29
Seed[1]:-93
Seed[2]:6
Seed[3]:66
Seed[4]:126
Seed[5]:93
Seed[6]:-58
Seed[7]:-91
Seed[8]:-62
Seed[9]:-58
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...