我看过很多关于 WELLRNG512 的帖子。假设我已经得出结论,对于一个 roguelike 地牢爬行者来说,这将是一个比 Mersenne Twister 更好的选择。我试图让这段代码生成随机数并且行为很像 rand()。
使用的代码:
static unsigned long state[16];
static unsigned int index = 0;
int main (void) {
int i;
for (i = 0; i < 16; i++) {
index = i;
printf("random: %lu\n", WELLRNG512());
}
return 0;
}
unsigned long WELLRNG512 (void) {
unsigned long a, b, c, d;
a = state[index];
c = state[(index+13)&15];
b = a^c^(a<<16)^(c<<15);
c = state[(index+9)&15];
c ^= (c>>11);
a = state[index] = b^c;
d = a^((a<<5)&0xDA442D24UL);
index = (index + 15)&15;
a = state[index];
state[index] = a^b^d^(a<<2)^(b<<18)^(c<<28);
return state[index];
}
预期成绩:
random: 231544
random: 542312
random: 588690
(...etc...)
获得的结果:
random: 4195755
random: 4195755
random: 4195755
(...etc...)
有谁知道我如何成功地使那段代码表现得像 rand()?
PS:我是学生,绝对不是数学家,所以如果您要使用任何象形文字来解释公式等,请详细解释您的答案。