使用python的diehard的原始二进制流

2024-06-25 06:48:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将python数字生成器与diehard结合起来,我发现在linux中可以使用管道来完成

python3 generator.py | dieharder -a -g 200

-g 200表示我将使用stdin_input_raw

以下是Diehard手册中的规范:

Raw binary input reads 32 bit increments of the specified data 
stream. stdin_input_raw accepts a pipe from a raw binary stream.

现在我使用radom generator创建了一些python代码:

while True:
    sys.stdout.write("{0:b}".format(random.randint(0, 256)))

但是我认为它没有像预期的那样工作,因为上面第一次测试的终端命令的结果是:

     test_name   |ntup| tsamples |psamples|  p-value |Assessment 
#====================================================================#
diehard_birthdays|   0|       100|     100|0.00000000|  FAILED  

对于命令:

cat /dev/urandom | dieharder -a -g 200

它是:

     test_name   |ntup| tsamples |psamples|  p-value |Assessment
#====================================================================#
diehard_birthdays|   0|       100|     100|0.92477826|  PASSED  

我怀疑生成raw input stream的代码不正确,如何用python创建这个流?你知道吗

解决方案:

while True:
    sys.stdout.buffer.write(struct.pack('>I', random.randint(0, 2**32)))

Tags: 代码trueinputstreamrawstdinstdoutsys