nist_sp_800_22_tests.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:py-prng 作者: czechnology 项目源码 文件源码
def frequency(generator, n_bits, misc=None):
    """Frequency (Monobit) Test.

    Test purpose as described in [NIST10, section 2.1]:
    "The focus of the test is the proportion of zeroes and ones for the entire sequence. The purpose
    of this test is to determine whether the number of ones and zeros in a sequence are
    approximately the same as would be expected for a truly random sequence. The test assesses the
    closeness of the fraction of ones to 1/2, that is, the number of ones and zeroes in a sequence
    should be about the same. All subsequent tests depend on the passing of this test."
    """

    s_n = 0
    for _ in range(n_bits):
        s_n += 2 * generator.random_bit() - 1  # 1 if generator.random_bit() else -1

    s_obs = abs(s_n) / sqrt(n_bits)

    p_value = erfc(s_obs / sqrt(2))

    if type(misc) is dict:
        misc.update(s_n=s_n, s_obs=s_obs)

    return p_value
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号