我想实现一个快速随机生成器,我发现了这个网站: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;
}
我想知道这里的 const 是否有任何用处,我可以安全地删除它吗?
最佳答案
const
这里防止y
被意外修改;例如,如果程序员不小心打错了 x
在第四条声明中为 y
( y ^= x << 23
) 编译器会提示。
您可以删除它而不会对程序产生任何语义影响,但我不明白您为什么要这样做。
关于c++ const 使用。我可以删除它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39117291/