随机与铀的比较

2024-05-28 20:25:23 发布

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

有人问我:

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.

我目前对“决定随机性”的措辞迷茫了

我可以从铀和随机阅读:

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

或者

^{pr2}$

我认为这是一个比随机运动更快的目标。然而,如果我试图阅读任何超过15的东西,阅读的时间似乎会成倍增加。在

所以我现在不知道如何比较“随机性”。如果我把urandom和random都读到各自的文件中,我怎么能比较它们呢?在


Tags: andofthedevreadyourpiscript
2条回答

你的经历也许正是他们所期待的。从《天王星》的手册页(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.

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

是否可以简单到:

In [664]: f = open("/dev/random", "rb")
In [665]: len(set(f.read(256)))
Out[665]: 169


In [666]: ff = open("/dev/urandom", "rb")
In [667]: len(set(ff.read(256)))
Out[667]: 167


In [669]: len(set(f.read(512)))
Out[669]: 218

In [670]: len(set(ff.read(512)))
Out[670]: 224

例如,请求256个字节并不能返回256个唯一值。所以你可以根据唯一计数绘制出不断增加的样本量,直到它达到256个饱和点。在

相关问题 更多 >

    热门问题