从均匀分布python生成随机数的最快方法 [英] Fastest way to generate random number from uniform distribtution python
问题描述
我需要根据均匀分布生成大量(100万)随机数.我做了一些实验,使用
从[0-999]
上的均匀分布生成60K随机数
I need to generate large(1 Million) number of random numbers from a uniform distribution. I did some experiments generating 60K random number from uniform distribution over [0-999]
using
scipy.stats.randint(0,1000).rvs(60000)
,大约花了0.6秒.然后我使用Boost库尝试了同样的事情
and it took ~0.6s. Then I tried same thing using boost library
boost::random::uniform_int_distribution
,大约用了0.25秒.所以我的问题是-使用python(libraries)是否有更好的方法来做我想做的事情?任何建议,不胜感激.谢谢.
and it took ~0.25s. So my question is this- Are there better ways of doing what I want to do using python(libraries)? Any suggestions are greatly appreciated. Thanks.
推荐答案
使用numpy.random.randint
:
numpy.random.randint(low=1, high=1001, size=60000)
在我的试用中,它的运行速度比scipy.stats.randint
快300倍,而c3是为通用性而非速度而设计的系统的一部分. (另外,scipy.stats.randint(0,1000).rvs(60000)
会生成0到999的整数,而不是1到1000的整数.)
On my trials, this runs about 300 times faster than scipy.stats.randint
, which is part of a system designed more for generality than speed. (Also, scipy.stats.randint(0,1000).rvs(60000)
is generating integers from 0 to 999, not 1 to 1000.)
这篇关于从均匀分布python生成随机数的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!