gpt4 book ai didi

python - 比较随机和urandom

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:00:13 25 4
gpt4 key购买 nike

有人问我:

Using your raspberry pi, write a python script that determines the randomness of /dev/random and /dev/urandom. Read bytes and histogram the results. Plot in matplotlib. For your answer include the python script.

我目前迷失在“决定随机性”这一措辞上。

我可以从 urandom 和 random 读取:

#rb - reading as binary
devrndm = open("/dev/random", 'rb')
#read to a file instead of mem?
rndmdata = devrndm.read(25) #read 25bytes

with open("/dev/random", 'rb') as f:
print repr(f.read(10))

我认为这个练习的目的是发现 urandom 比 random 更快并且有更大的池。但是,如果我尝试阅读超过 ~15 的任何内容,阅读时间似乎呈指数增长。

所以我现在不知道如何比较“随机性”。如果我将 urandom 和 random 读入各自的文件,我该如何比较它们?

最佳答案

您的体验可能正是他们所需要的。来自 urandom(4) 的手册页:

When read, the /dev/random device will only return random bytes within the estimated number of bits of noise in the entropy pool. /dev/random should be suitable for uses that need very high quality randomness such as one-time pad or key generation. When the entropy pool is empty, reads from /dev/random will block until addi‐ tional environmental noise is gathered.

A read from the /dev/urandom device will not block waiting for more entropy.

注意关于阻塞的一点。 urandom 不会,随机会。特别是在嵌入式上下文中,可能很难获得额外的熵,这会导致您看到的阻塞。

关于python - 比较随机和urandom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14738725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com