这些是128位XorShift
PRNG的函数,通过查看代码,我从未想过类型(Int32 / UInt32)的差异可能会产生不同的结果。在这两种情况下,我们操作相同的位组,向左或向右移动相同数量的字节,为什么我们得到不同的结果呢?
Int32 XorShift128(Int32 x, Int32 y, Int32 z, Int32 w)
{
var t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = w ^ (w >> 19) ^ t ^ (t >> 8);
}
UInt32 XorShift128(UInt32 x, UInt32 y, UInt32 z, UInt32 w)
{
var t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = w ^ (w >> 19) ^ t ^ (t >> 8);
}
void Main()
{
Random rand = new Random();
for (var i = 0; i < 10000000; i++) {
var x = rand.Next();
var y = rand.Next();
var z = rand.Next();
var w = rand.Next();
var u = XorShift128(x,y,z,w);
var v = XorShift128((UInt32)x,(UInt32)y,(UInt32)z,(UInt32)w);
if ((Int32)v != u) {
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(z);
Console.WriteLine(w);
Console.WriteLine(u);
Console.WriteLine((Int32)v);
Console.WriteLine("-----------------");
}
}
}
答案 0 :(得分:0)
由于Phylogenesis,答案在评论中提供:
-2 >> 1 == -1