问题描述
基本上我想生成随机数,这些数字在很长一段时间内都不会重复(我不想使用序列),例如java使用的LCG:
Basically I want to generate random numbers that won't ever repeat for a very long period (I don't want to use a sequence) like for example the LCG that java uses:
synchronized protected int next(int bits) {
seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
return (int)(seed >>> (48 - bits));
}
据我所知,这种情况下的种子只会在2 ^ 48个电话后重复下一步是正确的吗?
As I understand the seed in this case will only repeat after 2^48 calls to next is this correct?
所以我理解如果我做了一个方法:
so it is my understand that if I did a method like:
synchronized protected long next() {
seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
return seed;
}
保证种子值在2 ^ 48个电话之前不重复?
The seed value is guaranteed not to repeat before 2^48 calls?
推荐答案
不适用于那个LCG,因为每次调用时你都会被2 ^ 48修改(因此周期/状态是最多2 ^ 48的长度)。如果你想要一个更好的随机数生成器,你可以尝试Mersenne twister:
Not for that LCG, since you are modding out by 2^48 each time you call it (and thus the period/state is at most 2^48 in length). If you want a better random number generator, you could try the Mersenne twister:
标准MT19937的周期为2 ^ 19937-1 (!!!)这应该比你需要的更多。
The standard MT19937 has a period of 2^19937-1 (!!!) That should be more than you will ever need.
这篇关于Java生成“随机”数字不重复2 ^ 48周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!