Random生成的随机数都是伪随机数,也就是说生成的随机数是有依据可循。
查看Random源代码:
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
private static long seedUniquifier() {
// L'Ecuyer, "Tables of Linear Congruential Generators of
// Different Sizes and Good Lattice Structure", 1999
for (;;) {
long current = seedUniquifier.get();
long next = current * 181783497276652981L;
if (seedUniquifier.compareAndSet(current, next))
return next;
}
}
其中System.nanoTime() 是与系统时间无关的纳米维度时间,和CPU和线程有关
本文深入探讨了Java中Random类的实现原理,解释了其如何生成伪随机数,并通过源代码分析展示了System.nanoTime()和seedUniquifier()方法在生成随机种子中的作用,揭示了随机数生成与CPU和线程的关联。
1864

被折叠的 条评论
为什么被折叠?



