为什么 Random.java
选择 181783497276652981
和 8682522807148012
?
下面是来自 Java SE JDK 1.7的相关源代码:
/**
* Creates a new random number generator. This constructor sets
* the seed of the random number generator to a value very likely
* to be distinct from any other invocation of this constructor.
*/
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;
}
}
private static final AtomicLong seedUniquifier
= new AtomicLong(8682522807148012L);
So, invoking new Random()
without any seed parameter takes the current "seed uniquifier" and XORs it with System.nanoTime()
. Then it uses 181783497276652981
to create another seed uniquifier to be stored for the next time new Random()
is called.
字面值 181783497276652981L
和 8682522807148012L
不放在常量中,但它们不会出现在其他任何地方。
起初,这个评论给了我一个简单的线索。在线搜索该文章会得到 真正的文章。8682522807148012
没有出现在论文中,但是 181783497276652981
出现了——作为另一个数字 1181783497276652981
的子串,1181783497276652981
是 181783497276652981
,后面加上 1
。
The paper claims that 1181783497276652981
is a number that yields good "merit" for a linear congruential generator. Was this number simply mis-copied into Java? Does 181783497276652981
have an acceptable merit?
为什么选择 8682522807148012
?
在线搜索任何一个数字都不会得到解释,只有 this page会注意到 181783497276652981
前面丢失的 1
。
Could other numbers have been chosen that would have worked as well as these two numbers? Why or why not?