【发布时间】:2014-02-20 11:37:20
【问题描述】:
我在 Random 类下看到了 Java 中的 LCG 实现,如下所示:
/*
* This is a linear congruential pseudorandom number generator, as
* defined by D. H. Lehmer and described by Donald E. Knuth in
* <i>The Art of Computer Programming,</i> Volume 3:
* <i>Seminumerical Algorithms</i>, section 3.2.1.
*
* @param bits random bits
* @return the next pseudorandom value from this random number
* generator's sequence
* @since 1.1
*/
protected int next(int bits) {
long oldseed, nextseed;
AtomicLong seed = this.seed;
do {
oldseed = seed.get();
nextseed = (oldseed * multiplier + addend) & mask;
} while (!seed.compareAndSet(oldseed, nextseed));
return (int)(nextseed >>> (48 - bits));
}
但下面的链接告诉 LCG 应该是 x2=(ax1+b)modM
https://math.stackexchange.com/questions/89185/what-does-linear-congruential-mean
但上面的代码看起来并不相似。相反,它使用 & 代替以下行的模运算
nextseed = (oldseed * multiplier + addend) & mask;
有人可以帮我理解这种使用 & 代替模运算的方法吗?
【问题讨论】: