Algorithm 寻找George Marsaglia的逆运算';s XorShift RNG 摘要
您好,假设您有128位自动机(由四个32位单词Algorithm 寻找George Marsaglia的逆运算';s XorShift RNG 摘要,algorithm,Algorithm,您好,假设您有128位自动机(由四个32位单词X,Y,Z,W)根据以下规则更改其状态: X = ... Y = ... Z = ... W = ... void next() { var t = X ^ (X << 11); X = Y; Y = Z; Z = W; W = W ^ (W >> 19) ^ (t ^ (t >> 8)); } 顺便说一句,这里有一个快速版本: public class Xor128
X
,Y
,Z
,W
)根据以下规则更改其状态:
X = ...
Y = ...
Z = ...
W = ...
void next()
{
var t = X ^ (X << 11);
X = Y;
Y = Z;
Z = W;
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;
}
}
公共类Xor128{
公共变量X:UInt32
公共变量Y:UInt32
公共变量Z:UInt32
公共变量W:UInt32
公共便利初始化(uuid:uuid\t){
对于Y、Z和W,设xa=(UInt32(uuid.0),我们可以很容易地将其反转。对于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)
或相当于:
(x0 + x8) % 2 = a0
(x1 + x9) % 2 = a1
....
我们可以很容易地通过应用来解决这个问题。您需要的基本构造块是一个算法,通过左移位操作来反转异或f(x)=x^(x 0)。给定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
似乎有效。有趣的问题。你尝试过什么?你离回答有多近?你应该在问答案之前自己尝试解决它!我认为这是不可能的,因为位移位操作会删除移位出变量范围的位=>不可逆从纯理论上讲,你可以这样做穷举搜索,找到所有2^128个状态,然后将其全部映射出来。需要一些时间和空间(不可能长且大),但在理论上它是有效的。@tucuxi-我想你只需要2^32
存储-我们知道W
(和Y
和Z
)的先前值。我们可以用它来计算(t^(t>>8))
与当前的W
值和公式相关。t
仅取决于X
,因此如果我们将X
值映射到(t^(t>>8))
值,我们应该能够反转它。它不能有2^128个可达状态,因为X=Y=Z=W=0将循环forever@Lu4看看等式X^(X>8)^(x8)^(X的第一位好的,现在我知道你在修补什么了,我最初是用big-endian格式思考的(第一位是最重要的位):)让我把所有的方程都写出来,如果我用你的答案做这个会不会有问题?@Lu4一点问题也没有:),小心点,有32个方程,所以它会很长,你可以写一些脚本来做。假设我们得到a
的值,它仍然需要分解成X^(顺便说一句,希望你能找到以下问题interesting@Lu4事实上,这是同一个算法,有一个更好的公式。谢谢:)哦,老实说,它渐进地更有效,因为它是O(logw)而不是O(w),太好了,你指的是哪一个?@Lu4我指的是哈罗德对另一个问题的回答
(x0 + x8) % 2 = a0
(x1 + x9) % 2 = a1
....
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)
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)