Random setSeed() method in Java with Examples
Last Updated :
07 Jan, 2019
The setSeed() method of Random class sets the seed of the random number generator using a single long seed.
Syntax:
public void setSeed()
Parameters: The function accepts a single parameter seed which is the initial seed.
Return Value: This method has no return value.
Exception: The function does not throws any exception.
Program below demonstrates the above mentioned function:
Program 1:
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Random r = new Random();
System.out.println( "Random Integer value : "
+ r.nextInt());
long s = 24 ;
r.setSeed(s);
System.out.println( "Random Integer value : " + r.nextInt());
}
}
|
Output:
Random Integer value : -2053473769
Random Integer value : -1152406585
Program 2:
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Random r = new Random();
System.out.println( "Random Integer value : "
+ r.nextInt());
long s = 29 ;
r.setSeed(s);
System.out.println( "Random Integer value : "
+ r.nextInt());
}
}
|
Output:
Random Integer value : -388369680
Random Integer value : -1154330330
Share your thoughts in the comments
Please Login to comment...