我有以下代码(the xorshift128+
code from Wikipedia修改为使用向量类型):我的矢量xorshift +不是非常随机
#include <immintrin.h>
#include <climits>
__v8si rand_si() {
static auto s0 = __v4du{4, 8, 15, 16},
s1 = __v4du{23, 34, 42, 69};
auto x = s0, y = s1;
s0 = y;
x ^= x << 23;
s1 = x^y^(x >> 17)^(y >> 26);
return (__v8si)(s1 + y);
}
#include <iostream>
#include <iomanip>
void foo() {
//Shuffle a bit. The result is much worse without this.
rand_si(); rand_si(); rand_si(); rand_si();
auto val = rand_si();
for (auto it = reinterpret_cast<int*>(&val);
it != reinterpret_cast<int*>(&val + 1);
++it)
std::cout << std::hex << std::setfill('0') << std::setw(8) << *it << ' ';
std::cout << '\n';
}
其输出
09e2a657 000b8020 1504cc3b 00110040 1360ff2b 00150078 2a9998b7 00228080
每隔数是很小的,并且没有具有领先的位集。在另一方面,使用xorshift *代替:
__v8si rand_si() {
static auto x = __v4du{4, 8, 15, 16};
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
return x * (__v4du)_mm256_set1_epi64x(0x2545F4914F6CDD1D);
}
我得到更好的输出
0889632e a938b990 1e8b2f79 832e26bd 11280868 2a22d676 275ca4b8 10954ef9
但根据维基百科,xorshift +是一个很好的PRNG,而且比xorshift产生更好的伪随机性* 。那么,在我的RNG代码中是否存在一个错误,或者我错误地使用了它?
您正在用'clang'构建,对不对? [gcc说](https://godbolt.org/g/qLUFuz)*
@PeterCordes,是的,这是正确的。 – Dan
为什么使用'__v8si'和'__v4du'代替标准的Intel定义类型? –