You can also seed the random number generator by using the setSeed method as shown below. This way makes it easy to create a given set of random numbers more than once in the same program run:
Java > cat prog48.java
Java > cat prog48.java
import java.util.Random;
public class prog48
{
public static void main (String args[])
{
Random random_number = new Random();
System.out.println("Seeding random_number");
random_number.setSeed(123);
System.out.println("Random number 1 = "
+ random_number.nextInt());
System.out.println("Random number 2 = "
+ random_number.nextInt());
System.out.println("Random number 3 = "
+ random_number.nextInt());
System.out.println("Reseeding random_number");
random_number.setSeed(123);
System.out.println("Random number 4 = "
+ random_number.nextInt());
System.out.println("Random number 5 = "
+ random_number.nextInt());
System.out.println("Random number 6 = "
+ random_number.nextInt());
}
}
Java > javac prog48.java
Java > java prog48
Seeding random_number
Random number 1 = -1188957731
Random number 2 = 1018954901
Random number 3 = -39088943
Reseeding random_number
Random number 4 = -1188957731
Random number 5 = 1018954901
Random number 6 = -39088943
Java >