Dieharder随机测试套件-可疑的好结果

时间:2019-07-18 13:04:51

标签: python random lcg random-testing

我基于以下生成器(2500000个数字)生成了一个txt文件

import numpy as np

class LCG(object):

    UZERO: np.uint32 = np.uint32(0)
    UONE : np.uint32 = np.uint32(1)

    def __init__(self, seed: np.uint32, a: np.uint32, c: np.uint32) -> None:
        self._seed: np.uint32 = np.uint32(seed)
        self._a   : np.uint32 = np.uint32(a)
        self._c   : np.uint32 = np.uint32(c)

    def next(self) -> np.uint32:
        self._seed = self._a * self._seed + self._c
        return self._seed

    def seed(self) -> np.uint32:
        return self._seed

    def set_seed(self, seed: np.uint32) -> np.uint32:
        self._seed = seed

    def skip(self, ns: np.int32) -> None:
        """
        Signed argument - skip forward as well as backward

        The algorithm here to determine the parameters used to skip ahead is
        described in the paper F. Brown, "Random Number Generation with Arbitrary Stride,"
        Trans. Am. Nucl. Soc. (Nov. 1994). This algorithm is able to skip ahead in
        O(log2(N)) operations instead of O(N). It computes parameters
        A and C which can then be used to find x_N = A*x_0 + C mod 2^M.
        """

        nskip: np.uint32 = np.uint32(ns)

        a: np.uint32 = self._a
        c: np.uint32 = self._c

        a_next: np.uint32 = LCG.UONE
        c_next: np.uint32 = LCG.UZERO

        while nskip > LCG.UZERO:
            if (nskip & LCG.UONE) != LCG.UZERO:
                a_next = a_next * a
                c_next = c_next * a + c

            c = (a + LCG.UONE) * c
            a = a * a

            nskip = nskip >> LCG.UONE

        self._seed = a_next * self._seed + c_next


#%%
np.seterr(over='ignore')

a = np.uint32(1664525)
c = np.uint32(1013904223)
seed = np.uint32(1)

rng = LCG(seed, a, c)
q = [rng.next() for _ in range(0, 2500000)]

我使用以下代码保存了文件:

第一个单元格

%%capture cap --no-stderr
print(q)

第二个单元格

with open('output5.txt', 'w') as f:
    f.write(cap.stdout)

然后我用Diehard suite通过以下方式进行测试:

dieharder -f output5.txt -a

我不确定测试是否确实在txt文件上运行,以及我的txt文件是否正确。 250万个数字的样本约为30mb。

所有测试进展顺利,我感到很惊讶。

下面是终端中的结果。

我很困惑,因为名称是MT19937-这不是我的名字,文件“ output5.txt”是我的文件。我不知道是否对我的文件执行了测试

enter image description here

1 个答案:

答案 0 :(得分:0)

我用生成器生成2,5条mio行,并使用以下标头将它们保存到文件testrands.txt中:

#==================================================================
# generator lcg  seed = 1
#==================================================================
type: d
count: 100000
numbit: 32
1015568748
1586005467
2165703038
3027450565
217083232
1587069247
......

我打电话给

dieharder -g 202 -f testrands.txt -a

现在结果出奇地薄弱(也许我生成的数字太少了?)

我也不确定所有这些测试是否都适合进行LCG测试,但结果出乎意料的

1

2

我按照指南中的说明进行操作,但似乎仍然不符合要求-LCG通过了生日间隔(我认为不应该这样做),其余结果出奇的弱

相关问题