public class RandomTest
{
public static void main(String[] args)
{
Random rand1 = new Random(47);
Random rand2 = new Random(47);
for (int i = 0; i < 100; i++)
{
System.out.print(rand1.nextInt(100) + " ");
System.out.println(rand2.nextInt(100) + " ");
}
}
}
After running this program, if you will see that,
1) rand1 and rand2 will always generate the same number.
2) No matter how many times you run, the number serial never changes. It’s always the same.
The reason for this is because of the ---- “seed”.
Random rand1 = new Random(47);
Here, 47 is the seed. 47 will make it fixed, what number it is in some order.
If you want to get really random number, you need to use the default non-argument constructor for Random class.