random.sample with long int items

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Apr 12 10:18:08 EDT 2006


On Wed, 12 Apr 2006 06:44:29 -0700, Paul Rubin wrote:

> "jordi" <jpahullo at gmail.com> writes:
>> I need the random.sample functionality where the population grows up to
>> long int items. Do you know how could I get this same functionality in
>> another way? thanks in advance.
> 
> Nothing stops you:
> 
>     >>> from random import sample
>     >>> a = [n**25 for n in range(6)]
>     >>> a
>     [0, 1, 33554432, 847288609443L, 1125899906842624L, 298023223876953125L]
>     >>> sample(a,2)
>     [1125899906842624L, 298023223876953125L]

No, I think he means the size of the list is big enough to need a long
int. Something like xrange(10**10) or even bigger.

>>> random.sample(xrange(10*10), 10)
[96, 45, 90, 52, 57, 72, 94, 73, 79, 97]
>>> random.sample(xrange(10**10), 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: long int too large to convert to int


-- 
Steven.




More information about the Python-list mailing list