python随机生成20个整数的列表,在Python中创建随机整数列表

I'd like to create a random list of integers for testing purposes. The distribution of the numbers is not important. The only thing that is counting is time. I know generating random numbers is a time-consuming task, but there must be a better way.

Here's my current solution:

import random

import timeit

# Random lists from [0-999] interval

print [random.randint(0, 1000) for r in xrange(10)] # v1

print [random.choice([i for i in xrange(1000)]) for r in xrange(10)] # v2

# Measurement:

t1 = timeit.Timer('[random.randint(0, 1000) for r in xrange(10000)]', 'import random') # v1

t2 = timeit.Timer('random.sample(range(1000), 10000)', 'import random') # v2

print t1.timeit(1000)/1000

print t2.timeit(1000)/1000

v2 is faster than v1, but it is not working on such a large scale. It gives the following error:

ValueError: sample larger than population

Is there a fast, efficient solution that works at that scale?

Some results from the answer

Andrew's: 0.000290962934494

gnibbler's: 0.0058455221653

KennyTM's: 0.00219276118279

NumPy came, saw, and conquered.

解决方案

It is not entirely clear what you want, but I would use numpy.random.randint:

import numpy.random as nprnd

import timeit

t1 = timeit.Timer('[random.randint(0, 1000) for r in xrange(10000)]', 'import random') # v1

### Change v2 so that it picks numbers in (0, 10000) and thus runs...

t2 = timeit.Timer('random.sample(range(10000), 10000)', 'import random') # v2

t3 = timeit.Timer('nprnd.randint(1000, size=10000)', 'import numpy.random as nprnd') # v3

print t1.timeit(1000)/1000

print t2.timeit(1000)/1000

print t3.timeit(1000)/1000

which gives on my machine:

0.0233682730198

0.00781716918945

0.000147947072983

Note that randint is very different from random.sample (in order for it to work in your case I had to change the 1,000 to 10,000 as one of the commentators pointed out -- if you really want them from 0 to 1,000 you could divide by 10).

And if you really don't care what distribution you are getting then it is possible that you either don't understand your problem very well, or random numbers -- with apologies if that sounds rude...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值