XorShift 数字生成

2 投票
1 回答
4554 浏览
提问于 2025-04-17 23:12

同样的XorShift函数在C语言和Python中运行,结果却不一样。你能解释一下吗?

XorShift函数生成数字的方式如下:

x(0) = 123456789
y(0) = 362436069
z(0) = 521288629
w(0) = 88675123 

x(n+1) = y(n)
y(n+1) = z(n)
z(n+1) = w(n)
w(n+1) = w(n) ^ (w(n)>>19) ^ (x(n)^(x(n)<<11)) ^ ((x(n)^(x(n)<<11)) >> 8)

我在Python中写了这个函数来生成后续的w值:

X = 123456789
Y = 362436069
Z = 521288629
W = 88675123

def xor_shift():
    global X, Y, Z, W
    t = X ^ (X << 11)
    X = Y
    Y = Z
    Z = W
    W = W ^ (W >> 19) ^ t ^ (t >> 8)
    return W

W1 = xor_shift() # 252977563114
W2 = xor_shift() # 646616338854
W3 = xor_shift() # 476657867818

用C语言写的相同代码(可以在维基百科找到 http://en.wikipedia.org/wiki/Xorshift)却给出了不同的结果:

#include <stdint.h>
uint32_t xor128(void) {
    static uint32_t x = 123456789;
    static uint32_t y = 362436069;
    static uint32_t z = 521288629;
    static uint32_t w = 88675123;
    uint32_t t;

    t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = w ^ (w >> 19) ^ t ^ (t >> 8);
}

cout << xor128() <<'\n'; // result W1 = 3701687786
cout << xor128() <<'\n'; // result W2 = 458299110
cout << xor128() <<'\n'; // result W3 = 2500872618

我想可能是我的Python代码有问题,或者是我使用cout的方式不对(我对C++不太熟)。

编辑:有效的解决方案:

需要把返回值从 uint32_t 改成 uint64_t

#include <stdint.h>
uint64_t xor128(void) {
    static uint64_t x = 123456789;
    static uint64_t y = 362436069;
    static uint64_t z = 521288629;
    static uint64_t w = 88675123;
    uint64_t t;

    t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = w ^ (w >> 19) ^ t ^ (t >> 8);
}

1 个回答

5

把你所有的 uint32_t 类型改成 uin64_t,你会得到一样的结果。它们之间的区别在于 uint32_t 的精度和 Python 整数类型的无限精度。

撰写回答