python取随机数的数组_用Python随机数的大数组

1586010002-jmsa.png

I need to generate a big array (or list) with random numbers ( 10⁵ numbers) . I was trying like that:

vet = random.sample(range(10),100000)

But when I try to run :

vet = random.sample(range(10),10000)

File "/usr/lib/python2.7/random.py", line 320, in sample

raise ValueError("sample larger than population")

ValueError: sample larger than population

Any solution?

tkns

解决方案

What you want is

[random.random() for _ in xrange(100000)]

random.sample(population, k) Return a k length list of unique elements

chosen from the population sequence. Used for random sampling without

replacement.

so when calling random.sample(range(10), 100000) you're trying to extract 100000 unique elements in a sequence of length 10 which obviously can't work.

Note that

random.random() returns a floating value between [0 ; 1)

random.randrange([start], stop[, step]) returns a random element from the sequence range([start], stop[, step])

random.randint(a, b) returns an integer value in [a ; b]

when using random.sample, the equality len(population) >= k must hold

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值