加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

每日一程-9.五种基本随机数测试(1/2)

发布时间:2020-12-14 04:24:17 所属栏目:大数据 来源:网络整理
导读:Author: Notus([email?protected]) Create: 2019-02-16 Update: 2019-02-16 随机数测试(一):Frequencey test(monobit test),Serial test(two-bit test) and Poker test 环境 Python version: 3.7.1 代码如下(PseudorandomTest.py) ‘‘‘ Pseudorandom Se

Author: Notus([email?protected])
Create: 2019-02-16
Update: 2019-02-16

随机数测试(一):Frequencey test(monobit test),Serial test(two-bit test) and Poker test

环境

Python version: 3.7.1

代码如下(PseudorandomTest.py)

‘‘‘
    Pseudorandom Sequences Tests: 
        1. Frequency test (monobit test)
        2. Serial test (two-bit test)
        3. Poker test
        4. Runs test
        5. Autocorrelation test
    @Author: Notus([email?protected])
    @Create: 2019-02-15
    @Update: 2019-02-16
‘‘‘

# 1. Frequency test(monobit test)
def monobitTest(num):
    n = len(num)
    n0 = num.count(‘0‘)
    n1 = num.count(‘1‘)
    x1 = (n0 - n1) ** 2 / n
    return n,n0,n1,x1

# 2. Serial test(two-bit test)
def serialTest(num):
    n = len(num)
    n0 = num.count(‘0‘)
    n1 = num.count(‘1‘)
    n00 = n01 = n10 = n11 = 0

    for i in range(0,n-1):
        i0 = num[i]
        i1 = num[i+1]
        if i0 == ‘0‘:
            if i1 == ‘0‘: 
                n00 += 1
            else: 
                n01 += 1
        else:
            if i1 == ‘0‘: 
                n10 += 1
            else: 
                n11 += 1
        i += 1

    x2 = 4 * (n00**2 + n01**2 + n10**2 + n11**2) / (n-1) - 2 * (n0**2 + n1**2) / n + 1
    return n,n00,n01,n10,n11,x2    


# 3. Poker test
def pokerTest(num,m):
    n = len(num)
    k = n // m
    if k < 5 * (2 ** m):
        raise ValueError("Error: the value of m is invalid for Poker Test!")

    # ni count list,0 <= i <= 2**m - 1
    ni_list = [0] * (2 ** m)

    # counting  
    for b in range(0,n-m,m):
        index = 0
        for c in range(m):
            index = index * 2 + int(num[b + c])
        ni_list[index] += 1

    s = 0
    for i in range(1,2**m + 1):
        s += ni_list[i - 1] ** 2

    x3 = (2 ** m) * s / k - k
    return k,ni_list,x3

# 4. Runs test
# Todo

# 5. Autocorrelation test
# Todo

if __name__ == ‘__main__‘:
    num = (‘11100‘ + ‘01100‘ + ‘01000‘ + ‘10100‘ + ‘11101‘ + ‘11100‘ + ‘10010‘ + ‘01001‘) * 4
    
    x1 = monobitTest(num)
    print("frequency test: nn = {},n0 = {},n1 = {},nX1 = {}n".format(*x1))

    x2 = serialTest(num)
    print("serial test: nn = {},n00 = {},n01 = {},n10 = {},n11 = {},nX2 = {}n".format(*x2))

    try:
        x3 = pokerTest(num,3)
    except ValueError as err:
        print(err.args[0])
        sys.exit()
    print("poker test: nk = {},ni_list = {} nX3 = {}n".format(*x3))

运行结果

$python PseudorandomTest.py
frequency test:
n = 160,n0 = 84,n1 = 76,
X1 = 0.4

serial test:
n = 160,n00 = 44,n01 = 40,n10 = 40,n11 = 35,
X2 = 0.6251572327043959

poker test: k = 53,ni_list = [5,10,6,4,12,3,7] X3 = 9.641509433962263

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读