寻找与George Marsaglia的XorShift RNG

时间:2015-07-20 09:38:45

标签: algorithm

摘要

嗨,假设您有128位自动机(由四个32位字XYZW表示),可以改变它的状态根据以下规则:

X = ...
Y = ...
Z = ...
W = ...

void next()
{
    var t = X ^ (X << 11);

    X = Y;
    Y = Z;
    Z = W;

    W = W ^ (W >> 19) ^ (t ^ (t >> 8));
}

^ - 表示二进制XOR操作

<< - 表示二进制左移操作

>> - 包含二进制右移操作

保证上述自动机不产生冲突,即每个状态是一个(且仅一个)先前状态的结果。还保证上述状态机产生2 ^ 128个唯一状态。

问题

对于任何给定状态(X,Y,Z,W)产生与next相反的(即prev)操作,该状态会将状态恢复为前一状态。

换句话说,如果您有以下状态(X=1, Y=2, Z=3, W=4)并且会调用next,状态将更改为(X=2, Y=3, Z=4, W=2061),假设在调用prev之后状态应该再次等于(X=1, Y=2, Z=3, W=4)

P.S。

next操作是George Marsaglia发现的XorShift伪随机数生成器的实现之一

https://en.wikipedia.org/wiki/Xorshift

此操作的反向通常非常有用,请考虑Guid.Next(...),Guid.Prev(...)可用性的含义

修改

我有点改进了Niklas B.原来的答案并将结果移植到了C#,所以这里是最后一段代码,希望有人能从Random.Next()和Random.Prev中受益( )操作:

public class Xor128
{
    public UInt32 X { get; set; }
    public UInt32 Y { get; set; }
    public UInt32 Z { get; set; }
    public UInt32 W { get; set; }

    public Xor128()
    {

    }

    public Xor128(UInt32 x, UInt32 y, UInt32 z, UInt32 w)
    {
        X = x;
        Y = y;
        Z = z;
        W = w;
    }

    //private UInt32 UnXorShl(UInt32 x, Int32 shift)
    //{
    //    for (var i = shift; i < 32; i <<= 1) {
    //        x ^= x << i;
    //    }

    //    return x;
    //}

    //private UInt32 UnXorShr(UInt32 x, Int32 shift)
    //{
    //    for (var i = shift; i < 32; i <<= 1) {
    //        x ^= x >> i;
    //    }

    //    return x;
    //}

    //public UInt32 Prev()
    //{
    //    var t = UnXorShr(W ^ Z ^ (Z >> 19), 8);

    //    W = Z;
    //    Z = Y;
    //    Y = X;
    //    X = UnXorShl(t, 11);

    //    return W;
    //}

    public UInt32 Prev()
    {
        var t = W ^ Z ^ (Z >> 19);

        t ^= t >> 8;
        t ^= t >> 16;

        W = Z;
        Z = Y;
        Y = X;

        t ^= t << 11;
        t ^= t << 22;

        X = t;

        return W;
    }


    public UInt32 Curr()
    {
        return W;
    }

    public UInt32 Next()
    {
        UInt32 t = X ^ (X << 11);

        X = Y;
        Y = Z;
        Z = W;

        return W = W ^ (W >> 19) ^ (t ^ (t >> 8));
    }
}

顺便说一句。这是一个快速版本:

public class Xor128 {
    public var X: UInt32
    public var Y: UInt32
    public var Z: UInt32
    public var W: UInt32

    public convenience init(uuid: uuid_t) {
        let xa = (UInt32(uuid.0 ) << 24)
        let xb = (UInt32(uuid.1 ) << 16)
        let xc = (UInt32(uuid.2 ) << 8 )
        let xd = (UInt32(uuid.3 ) << 0 )

        let ya = (UInt32(uuid.4 ) << 24)
        let yb = (UInt32(uuid.5 ) << 16)
        let yc = (UInt32(uuid.6 ) << 8 )
        let yd = (UInt32(uuid.7 ) << 0 )

        let za = (UInt32(uuid.8 ) << 24)
        let zb = (UInt32(uuid.9 ) << 16)
        let zc = (UInt32(uuid.10) << 8 )
        let zd = (UInt32(uuid.11) << 0 )

        let wa = (UInt32(uuid.12) << 24)
        let wb = (UInt32(uuid.13) << 16)
        let wc = (UInt32(uuid.14) << 8 )
        let wd = (UInt32(uuid.15) << 0)

        self.init(
            x: xa + xb + xc + xd,
            y: ya + yb + yc + yd,
            z: za + zb + zc + zd,
            w: wa + wb + wc + wd
        )
    }

    public convenience init(uuid: UUID) {
        self.init(uuid: uuid.uuid)
    }

    public init(x: UInt32, y: UInt32, z: uint32, w: UInt32) {
        X = x
        Y = y
        Z = z
        W = w
    }

    @discardableResult
    public func next() -> UInt32 {
        let t = X ^ (X << 11);

        X = Y;
        Y = Z;
        Z = W;

        W = W ^ (W >> 19) ^ (t ^ (t >> 8))

        return W;
    }

    public var curr: UInt32 {
        return W
    }

    @discardableResult
    public func prev() -> UInt32 {
        var t = W ^ Z ^ (Z >> 19);

        t ^= t >> 8;
        t ^= t >> 16;

        W = Z;
        Z = Y;
        Y = X;

        t ^= t << 11;
        t ^= t << 22;

        X = t;

        return W;
    }
}

2 个答案:

答案 0 :(得分:7)

您需要的基本构建块是一种算法,可以通过左移操作f(x) = x ^ (x << s)来反转XOR,而某些s&gt;给定f(x),你已经知道x的低s位。

您可以从低到高迭代地重建剩余的位,因为您已经知道每个点已被异或以得到f(x)的位。这是Python中的一个例子:

def reverse_xor_lshift(y, shift, w=32):
    x = y & ((1<<shift) - 1)
    for i in range(w - shift):
        x |= (1 if bool(x & (1<<i)) ^ bool(y & (1<<(shift+i))) else 0)<<(shift+i)
    return x

现在其余部分变得相当容易。请注意,我正在重新使用右移模拟的左移位反转:

def reverse_bin(x, w=32):
    return int(bin(x)[2:].rjust(w, '0')[::-1], 2)

def reverse_xor_rshift(y, shift, w=32):
    # for simplicity, we just reuse reverse_xor_lshift here
    return reverse_bin(reverse_xor_lshift(reverse_bin(y), shift))

def forward(X, Y, Z, W):
    t = (X ^ (X << 11)) & 0xffffffff
    X = Y
    Y = Z
    Z = W
    W = W ^ (W >> 19) ^ (t ^ (t >> 8))
    return (X, Y, Z, W)

def backward(X, Y, Z, W):
    t = reverse_xor_rshift(W ^ Z ^ (Z >> 19), 8)
    return (reverse_xor_lshift(t, 11), X, Y, Z)

backward是反转状态转换的函数。一些随机测试:

import random
for _ in range(1000):
    X, Y, Z, W = [random.randint(0,2**32-1) for _ in range(4)]
    assert backward(*forward(X,Y,Z,W)) == (X, Y, Z, W)

似乎工作。

答案 1 :(得分:3)

对于Y,Z和W,我们可以轻松扭转它。对于X,我们需要做一些观察:

W' = W ^ (W >> 19) ^ (t ^ (t >> 8)), -> t ^ (t >> 8) = W' ^ (W ^ (W >> 19))

所以,现在,我们有t ^ (t >> 8) = W' ^ (W ^ (W >> 19)) = a

t = X ^ (X << 11) 

-> t ^ (t >> 8) = X ^ (X << 11) ^ ((X ^ (X <<11)) >> 8) 
                = X ^ (X << 11) ^ (X >> 8) ^ (X << 3)

将X的每个位表示为x0,x1,x2,... x31,并将a的每个位表示为a0,a1,...我们可以形成以下等式系统:

x0 ^ x8 = a0
x1 ^ x9 = a1
.....

或者,相当于:

(x0 + x8) % 2 = a0
(x1 + x9) % 2 = a1
....

我们可以通过应用Gaussian elimination轻松解决这个问题。