2015-12-22 24 views
5

我需要一个很好的伪随机数发生器(PRNG),它看起来像当前最先进的技术是xorshift128 + algoritm。不幸的是,我发现了两个不同的版本。这似乎直线前进足够xorshift128 +算法的真正定义是什么?

uint64_t s[2]; 

uint64_t xorshift128plus(void) { 
    uint64_t x = s[0]; 
    uint64_t const y = s[1]; 
    s[0] = y; 
    x ^= x << 23; // a 
    s[1] = x^y^(x >> 17)^(y >> 26); // b, c 
    return s[1] + y; 
} 

:在维基百科上的一个:Xorshift显示为。更重要的是,编辑日志似乎显示这个代码片段是由名为“Vigna”的用户添加的,该用户可能是“Sebastiano Vigna”,他是xorshift128 +:Further scramblings of Marsaglia’s xorshift generators上论文的作者。不幸的是,在该文件的执行略有不同:

uint64_t next(void) { 
    uint64_t s1 = s[0]; 
    const uint64_t s0 = s[1]; 
    s[0] = s0; 
    s1 ^= s1 << 23; // a 
    s[1] = s1^s0^(s1 >> 18)^(s0 >> 5); // b, c 
    return s[1] + s0; 
} 

除了一些不同的名字,这两个片段是除了最后两班相同。在维基百科版本中,这些转换是17和26,而纸张的转换是18和5.

有谁知道哪个是“正确”算法?这有什么不同吗?这显然是一个相当广泛使用的算法 - 但使用哪个版本?

+0

我发现[本塞巴斯豇豆公众意见(http://v8project.blogspot.com/2015/12/theres-mathrandom-and-then- theres.html?showComment = 1450389868643#c2004131565745698275)引用不同的常量值。两种算法都是“正确的”,你可以联系作者询问他是否有首选版本。 – Blastfurnace

+0

@blastfurnace - 谢谢,看起来像我需要的东西。 –

+0

@Blastfurnace:评论似乎(对我来说)使他的偏好非常清楚,尽管他确实说它主要是理论性的。 –

回答

3

感谢@Blastfurnace,看来答案是根据算法的作者最近一组常量是:23,18和5.显然它并不重要,但它们是理论上比他使用的最初一组数字更好。 Sebastiano Vigna在回应news that the V8 Javascript engine转而使用这种算法时发表了这些评论。

,我使用的实现是:

uint64_t a = s[0]; 
uint64_t b = s[1]; 

s[0] = b; 
a ^= a << 23; 
a ^= a >> 18; 
a ^= b; 
a ^= b >> 5; 
s[1] = a; 

return a + b;