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
评论列表
文章目录