- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
您好,假设您有 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));
}
^
- 表示二进制 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)
.
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;
}
}
最佳答案
您需要的基本构建 block 是一种通过左移运算反转 XOR 的算法 f(x) = x ^ (x << s)
对于某些 s > 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
现在剩下的就变得相当容易了。请注意,我正在为右移模拟重用左移反转:
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)
似乎有效。
关于algorithm - 寻找 George Marsaglia 的 XorShift RNG 的逆运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31513168/
我开发了一个 RNG 算法并生成随机数,现在我想测试生成的输出的随机性。我从 nist(sts) 下载了一个测试套件。有一个选项可以提供输入二进制文件来测试随机性。但我不知道如何生成这些二进制文件。我
因此,我编写了一个函数(以及一个 RNG 函数,上述函数调用该函数)来将随机数量的星号打印到控制台窗口,直到达到 90 个空格。星号代表汽车的运动,90个空格是轨道的长度。下面我包含的代码打印随机数量
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我正在尝试为我的模拟器重现一些行为。 我有一个百分比支票,从 30% 到 70% 不等。问题是它不是严格随机的。这是我提取的实时数据: https://docs.google.com/spreadsh
这个问题在这里已经有了答案: How to generate a random integer in the range [0,n] from a stream of random bits wit
我目前正在实现一个代表 52 张牌的牌组的 Deck 类。它使用 boost Random 库来洗牌代表卡片的整数。 #include #include #include "constants.h
在本文的最后一个示例 (http://www.daniweb.com/software-development/cpp/threads/1769/c-random-numbers) 中,作者声称这是一
我想使用 在我的 C++ 应用程序中使用多个 RNG .播种它们的最佳方法是什么?我担心当使用来自单个 RNG 的随机数为每个 RNG 播种时,来自不同 RNG 的随机数会过于相关。 最佳答案 如果
现在我正在使用 Mersenne Twister RNG 并执行 Fisher-Yates 洗牌算法 100 次: std::vector shufCards; for(int i =
我正在开发一款纸牌游戏,我需要洗牌算法来做得很好,并且每次游戏运行时都不同,并且没有可预测的纸牌序列。 我正在使用 Mersenne twister 算法,但它仍然需要一个种子,所以实际上,虽然它产生
我可以使用两种方法中的一种来创建具有两个重要特征的伪随机数序列 - (1) 它可以在不同的机器上重现,并且 (2) 该序列从不重复范围内的数字,直到所有数字都被发出. 我的问题是 - 这些方法中的任何
我有一段冗长而复杂的源代码,它使用带有修复种子的随机数生成器。 这段代码是一个模拟器,这个模拟器的参数就是这个RNG给出的随机值。当我在同一台机器上执行代码时,无论我尝试多少次,输出都是一样的。但是当
我在想这个问题。我听说全局变量不好,它们会损害代码的可维护性、可用性、可重用性等。但在这种情况下,我还能做什么呢?也就是说,我有一个“伪随机数生成器”(PRNG),正如人们所知,它们涉及一种内部状态,
回复 this question我运行了以下 VBA 实验: Sub Test() Dim i As Long, A As Variant Dim count1 As Long, co
我知道我可以用例如设置 RNGversion RNGversion("3.5.2") 但是是否可以查询我当前使用的 R 版本? 编辑: 我的问题是版本 3.6.0 发生了一些变化,这在 RNGkind
我今天刚开始学习JavaScript,这个很简单的问题给我带来了麻烦。这只是较大代码段的一部分,但我将其隔离以尝试修复它。这是: document.write(blarg()); func
我制作了一个随机数生成器,如果有人在没有数字的情况下使用它,它会崩溃如何确保用户输入是否是字符串,它不会崩溃,而是说“无效参数”? static int random; static String s
好吧,我正在编写一个与 cpu 对抗的程序,但每次它都会先失败然后致命,我会分解我的代码,以便让你们更容易提供帮助。 进口: import javax.swing.JOptionPane; impor
我的问题是http://rcpp-devel.r-forge.r-project.narkive.com/qJMEsvOK/setting-the-r-random-seed-from-rcpp的后续
作为一个学习 Haskell 的 Java 人,我已经习惯了思考一切的新方法,但我花了半天时间尝试用简单的 RNG 实现一些东西,但一无所获。在 Java 中,我可以创建一个静态 RNG 并使用 Cl
我是一名优秀的程序员,十分优秀!