c++ - 我的 PRNG 实现与我要复制的实现有何不同?

标签 c++ random prng

我需要一个用于模拟项目的 PRNG,并找到了 a resource,考虑到我对 PRNG 的有限但并非不存在的知识,它似乎是合理且消息灵通的。我试图将此报告中给出的算法封装在一个类中,但由于某种原因,我得到了很多重复值。可能只是 PRNG 不如报告中所说的那么好,但我怀疑这是我实现过程中的某些失败之处。

背景:报告中的PRNG代码

第 3 页给出了以下代码示例:

/* Public domain code for JKISS RNG */
// seed variables
static unsigned int x = 123456789,y = 987654321,z = 43219876,c = 6543217;

unsigned int JKISS()
{
    unsigned long long t;
    x = 314527869 * x + 1234567;
    y ^= y << 5; y ^= y >> 7; y ^= y << 22;
    t = 4294584393ULL * z + c; c = t >> 32; z = t;
    return x + y + z;
}

与此相关,报告称

The period of JKISS is ≈2 127 = 1.7x10 38 (2 32 x (2 32 -1) x (1/2 * 4294584393 x 2 32 - 1)) and it passes all of the Dieharder tests and the complete BigCrunch test set in TestU01.

所以这对我来说绝对足够好了。在报告的后面(第 6 页),指出

The following C code generate [sic] a random (double precision) floating point number 0 <= x < 1:

double x;
x = JKISS() / 4294967296.0;

我把这个封装在一个类中

我在头文件中有以下内容:

class JKISS : public IPRNG {
private:
    // Seed variables
    static unsigned int x;
    static unsigned int y;
    static unsigned int z;
    static unsigned int c;


public:
    static unsigned int randui32();
    static double randdouble();
};

使用以下实现文件

#include "prng.hpp"

unsigned int JKISS::x = 123456789;
unsigned int JKISS::y = 987654321;
unsigned int JKISS::z = 43219876;
unsigned int JKISS::c = 6543217;

unsigned int JKISS::randui32() {
    unsigned long long t;
    x = 314527869 * x + 1234567;
    y ^= y << 5; y ^= y >> 7; y ^= y << 22;
    t = 4294584393ULL * z + c; c = t >> 32; z = t;
    return x + y + z;
}

double JKISS::randdouble() {
    return randui32() / 4294967296.0;
}

和下面的主程序

#include <iostream>
#include "prng.hpp"

int main() {
    for (int i = 0; i < 10000; ++i) {
        std::cout << JKISS::randdouble() << std::endl;
    }
}

如您所见,我已经复制粘贴了大部分代码。

但是,当我运行它时,我得到了 68 个重复值,即使我只是获取了 10000 个值。这向我暗示我的封装有问题,但我无法弄清楚问题出在哪里。

以防万一,我在具有以下规范的 Ubuntu 13.10 上运行 GCC 4.8.1:

Platform Info:
  System: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU           E5410  @ 2.33GHz
  WORD_SIZE: 64

我们欢迎任何关于可能导致这种情况的想法。

最佳答案

您将返回一个float,而原始代码生成一个double

关于c++ - 我的 PRNG 实现与我要复制的实现有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23278702/

相关文章:

c++ - 一些设计模式代码中的奇怪语法 : explanation?

c++ - 如何在当前页面末尾的 QTextEdit 中将几个不可分割的文本 block 放在一起(全部放在下一页)?

java - 调用方法和填充随机数组时出现问题

php - WHERE 子句中的 MySQL RAND() 匹配一小组行

python - 如何使用某种 "momentum"生成随机方向(N、S、E、W、无)?

c++ - 将 boost 随机数生成器合并为类变量

c# - 在 native 可执行文件中嵌入 .net 二进制文件

perl - 这种改组算法的效率和质量如何?

c++ - 错误 : forward declaration of ‘class SActionPrivate’ when using PIMPL