The following code should create two random objects with the same seeds:
System.out.println("System time before: " + System.currentTimeMillis());
Random r1 = new Random();
Random r2 = new Random(System.currentTimeMillis());
System.out.println("System time after: " + System.currentTimeMillis());
System.out.println("r1: " + r1.nextInt());
System.out.println("r2: " + r2.nextInt());
Seeds should be the same, since they System.currentTimeMillis()did not change before and after creating two objects, as shown in the output:
System time before: 1331889186449
System time after: 1331889186449
r1: -1836225474
r2: 2070673752
In documents, a constructor with no arguments is simple:
public Random() { this(System.currentTimeMillis()); }
So what gives? Can someone explain why two generators return different outputs when they must have the same seed?
source
share