首页 > 解决方案 > 顽固分子使用 python 的原始二进制流

问题描述

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

python3 generator.py | dieharder -a -g 200

-g 200表示我将使用stdin_input_raw

这是顽固者手册中的规范:

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

现在我使用随机生成器创建了一些 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)))

标签: python

解决方案


推荐阅读