setSeed(byte [] seed)
java.security.SecureRandom類的setSeed()方法用於重新設置此隨機對象的種子。給定的種子補充而不是替換現有種子。因此,保證重複調用永遠不會降低隨機性。
用法:
public void setSeed(byte[] seed)
參數:此方法將種子作為參數。
Note:每次“安全隨機”類將生成隨機輸出
下麵是說明setSeed()方法的示例:
範例1:
// Java program to demonstrate
// setSeed() 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");
// Declaring the string variable
String str = "Tajmahal";
// Declaring the byte Array b
byte[] b = str.getBytes();
// Reseeding the random object
sr.setSeed(b);
// getting the seeds
byte[] seeds = sr.getSeed(10);
// printing the seed serialwise
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);
}
}
}
輸出:
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
範例2:
// Java program to demonstrate
// setSeed() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of SecureRandom
SecureRandom sr = new SecureRandom(new byte[] { 1, 2, 3, 4 });
// Declaring the string variable
String str = "Tajmahal";
// Declaring the byte Array b
byte[] b = str.getBytes();
// Reseeding the random object
sr.setSeed(b);
// getting the seeds
byte[] seeds = sr.getSeed(10);
// printing the seed serialwise
for (int i = 0; i < seeds.length; i++)
System.out.println("Seed[" + i + "]:" + seeds[i]);
}
catch (ProviderException e) {
System.out.println("Exception thrown:" + e);
}
}
}
輸出:
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(長種子)
java.security.SecureRandom類的setSeed(long seed)方法用於使用給定long種子中包含的八個字節來重新播種此隨機對象。給定的種子補充而不是替換現有種子。因此,保證重複調用永遠不會降低隨機性。
用法:
public void setSeed( long seed )
參數:該方法以種子為參數
Note:每次“安全隨機”類將生成隨機輸出
下麵是說明setSeed()方法的示例:
範例1:
// Java program to demonstrate
// setSeed() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating the object of SecureRandom
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "SUN");
// Reseeding the random object
sr.setSeed(101L);
// getting the seeds
byte[] seeds = sr.getSeed(10);
// printing the seed serialwise
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);
}
}
}
輸出:
Seed[0]:-36 Seed[1]:-65 Seed[2]:-94 Seed[3]:16 Seed[4]:-104
範例2:
// Java program to demonstrate
// setSeed() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of SecureRandom
SecureRandom sr = new SecureRandom(new byte[] { 1, 2, 3, 4 });
// Reseeding the random object
sr.setSeed(101L);
// getting the seeds
byte[] seeds = sr.getSeed(10);
// printing the seed serialwise
for (int i = 0; i < seeds.length; i++)
System.out.println("Seed[" + i + "]:" + seeds[i]);
}
catch (ProviderException e) {
System.out.println("Exception thrown:" + e);
}
}
}
輸出:
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
相關用法
- Java Random setSeed()用法及代碼示例
- Java SecureRandom nextBytes()用法及代碼示例
- Java SecureRandom getSeed()用法及代碼示例
- Java SecureRandom getProvider()用法及代碼示例
- Java SecureRandom generateSeed()用法及代碼示例
- Java SecureRandom getInstance()用法及代碼示例
- Java SecureRandom getAlgorithm()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 SecureRandom setSeed() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。