我想实现一个快速随机生成器,我遇到了这个网站:https://en.wikipedia.org/wiki/Xorshift,其中提出以下代码
#include <stdint.h>
/* The state must be seeded so that it is not everywhere zero. */
uint64_t s[2];
uint64_t xorshift128plus(void) {
uint64_t x = s[0];
uint64_t const y = s[1];
s[0] = y;
x ^= x << 23; // a
s[1] = x ^ y ^ (x >> 17) ^ (y >> 26); // b, c
return s[1] + y;
}
Run Code Online (Sandbox Code Playgroud)
我想知道这里的const是否有用,我可以安全地删除吗?
在const这里可以防止y被意外修改; 例如,如果程序员x在第四个语句中意外错误输入为y(y ^= x << 23)编译器会抱怨.
您可以删除它,对程序没有任何语义影响,但我不明白您为什么要这样做.