参见 node 文件夹中的 README
给定一个由 java.util.Random 的 nextDouble 生成的 double,重建一个具有相同状态的 Random。这个 ReplicatedRandom 随后可用于预测原始 Random 的未来值。
示例:
Random r = new Random();
ReplicatedRandom rr = new ReplicatedRandom();
rr.replicateState(r.nextDouble());
System.out.println(r.nextDouble() == rr.nextDouble()); // True
System.out.println(r.nextDouble() == rr.nextDouble()); // True
System.out.println(r.nextDouble() == rr.nextDouble()); // True
由于 Math.random() 在内部使用 Random,因此它也同样适用:
ReplicatedRandom rr = new ReplicatedRandom();
rr.replicateState(Math.random());
System.out.println(Math.random() == rr.nextDouble()); // True
System.out.println(Math.random() == rr.nextDouble()); // True
System.out.println(Math.random() == rr.nextDouble()); // True
nextInt() 也同样适用,但需要两个连续的值:
Random r = new Random();
ReplicatedRandom rr = new ReplicatedRandom();
rr.replicateState(r.nextInt(), r.nextInt());
System.out.println(r.nextInt() == rr.nextInt()); // True
System.out.println(r.nextInt() == rr.nextInt()); // True
System.out.println(r.nextInt() == rr.nextInt()); // True
有关详细信息,请参见这篇博客文章。