免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 李营长
打印 上一主题 下一主题

python随机数生成不够随机, [复制链接]

论坛徽章:
0
21 [报告]
发表于 2011-07-22 14:10 |只看该作者
回复 19# iamkey9
SystemRandom在linux下是通过查询/dev/urandom。win下是使用CryptGenRandom调用。这些都是os提供的生成真随机数的办法。

论坛徽章:
0
22 [报告]
发表于 2011-07-22 17:05 |只看该作者
回复 21# 106033177


http://en.wikipedia.org/wiki//dev/random

A counterpart to /dev/random is /dev/urandom ("unlocked"/non-blocking random source[4]) which reuses the internal pool to produce more pseudo-random bits. This means that the call will not block, but the output may contain less entropy than the corresponding read from /dev/random. While it is still intended as a pseudorandom number generator suitable for most cryptographic purposes, it is not recommended for the generation of long-term cryptographic keys.


http://publib.boulder.ibm.com/in ... aixfiles/random.htm
The /dev/urandom device provides a reliable source of random output,

/dev/urandom man page

The random number generator gathers environmental noise from device drivers and other sources into an entropy pool. The generator also keeps an estimate of the number of bits of noise in the entropy pool. From this entropy pool random numbers are created.

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 additional environmental noise is gathered.

A read from the /dev/urandom device will not block waiting for more entropy. As a result, if there is not sufficient entropy in the entropy pool, the returned values are theoretically vulnerable to a cryptographic attack on the algorithms used by the driver. Knowledge of how to do this is not available in the current unclassified literature, but it is theoretically possible that such an attack may exist. If this is a concern in your application, use /dev/random instead.


单个计算机系统不可能生成数学意义上的真随机数,不同的计算机方法区别只是质量高低而已。搜一下PRNG 和 TRNG的区别就知道了

论坛徽章:
0
23 [报告]
发表于 2011-07-22 17:18 |只看该作者
回复 22# iamkey9
当我没说。我只是告诉你相比数学的方法os提供的调用更随机,你不需要跟我较劲。真正的随机数只能扔骰子,这个不需要你告诉我。

论坛徽章:
0
24 [报告]
发表于 2011-07-22 17:22 |只看该作者
回复 22# iamkey9
而且用于加密的都是用os调用而不是数学的方法。http://docs.python.org/library/os.html#os.urandom  这里有文档,其他的就不多说了。

论坛徽章:
0
25 [报告]
发表于 2011-07-22 17:27 |只看该作者
回复 23# 106033177


无意挑起矛盾,只是说明真随机还是很难实现的。python的随机其实已经实现的比较好了。

关于伪随机,可以看看这个

http://lcamtuf.coredump.cx/newtcp/

论坛徽章:
0
26 [报告]
发表于 2011-07-22 17:33 |只看该作者
回javascript:;复 22# iamkey9
补充一句你可以google一下   linux 内核随机数产生器  文章很多。在一些需要真随机的领域比如密码学、网络安全等是不能使用数学的方法产生的伪随机数的,而是使用os提供真随机数。
当然你非得说os提供的也不是真随机数,我就没办法了,那以后加密领域只好扔骰子了。

论坛徽章:
0
27 [报告]
发表于 2011-07-22 17:37 |只看该作者
回复 25# iamkey9
python 实现的没什么好不好仍然是数学的方法,只是加了一个时间种子。但是在本质上依然是伪随机数,就不能应用于比如加密、网络安全这样的领域,相反os提供随机数却可以,尽管你认为os提供也不是真随机数。

论坛徽章:
0
28 [报告]
发表于 2011-07-22 17:48 |只看该作者
本帖最后由 106033177 于 2011-07-22 17:50 编辑

回复 25# iamkey9
回复  106033177

无意挑起矛盾,只是说明真随机还是很难实现的。python的随机其实已经实现的比较好了。

关于伪随机......
iamkey9 发表于 2011-07-22 17:27

问一下   好到什么程度?可以用在加密领域?请注意看random文档里的这句话。http://docs.python.org/library/random.html
However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.
什么意思不需要我解释了吧?

论坛徽章:
0
29 [报告]
发表于 2011-07-22 17:54 |只看该作者
本帖最后由 iamkey9 于 2011-07-22 17:57 编辑
回javascript:;复  iamkey9
补充一句你可以google一下   linux 内核随机数产生器  文章很多。在一些需要真 ...
106033177 发表于 2011-07-22 17:33



    那你看LZ标题撒,我没想和你深入讨论加密领域是否需要真随机的问题,他是说python的随机数不够随机。 他的应用里有很多python随机数出现在取值范围的中位数附近,造成了切割图片时候存在问题,而按照我的经验没有遇到过这种问题,所以我说LZ不太可能得到这样的结论。可以尝试一下用random.org提供的采集大气粒子得到的随机数去试验一下。
   
   那么假设LZ说的是对的,那他后来绘制的点图应该在中央有集中点才对, 我又没有得到相关的验证。所以可以说python的随机是完全够用的,只是想知道为什么LZ的切割会有问题发生而已。

  如果你有兴趣,你可以测一下用系统方法得到的随机数和普通的python随机数之间到底有多少差别。
  
  另外,我从来没说过python的random可以用在加密领域,这个和LZ的应用完全是两码事情

论坛徽章:
0
30 [报告]
发表于 2011-07-22 18:03 |只看该作者
那你看LZ标题撒,我没想和你深入讨论加密领域是否需要真随机的问题,他是说python的随机数不够随 ...
iamkey9 发表于 2011-07-22 17:54

如果你要是这样说的话 我就不反驳你了。但是请不要再说os提供的也不是真正意义上的随机数,这样说不是讨论问题而是较劲。也不要说python的伪随机数已经够好了之类的,来模糊真随机数和伪随机数的差别。另外就lz的问题我从一开始就说randint提供的是伪随机数,想获得真随机数可以用random.SystemRandom。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP