我最近发现_mm_crc32_*intel内在指令可用于生成(伪)随机32位数.
#include <nmmintrin.h> /* needs CRC32C instruction from SSE4.2 instruction set extension */
uint32_t rnd = 1; /* initialize with seed != 0 */
/* period length is 4,294,967,295 = 2^32-1 */
while (1) {
#if 0 // this was faster but worse than xorshift32 (fails more tests)
// rnd = _mm_crc32_u8(rnd, rnd >> 3);
#else // this is faster and better than xorshift32 (fails fewer tests)
rnd = _mm_crc32_u32(rnd, rnd << 18);
#endif
printf("%08X\n", rnd);
}
Run Code Online (Sandbox Code Playgroud)
此方法与LCG一样快,并且比xorshift32快.维基百科说,由于xorshift发电机"失败了一些统计测试,他们被指责为不可靠".
现在我想知道CRC32C方法是否通过了对随机数生成器进行的各种测试.我只是通过尝试使用PAQ8压缩器(失败)进行压缩来验证每个位,甚至LSB都是"随机的".有人可以帮我做更好的测试吗?
编辑: …