random.sample with long int items

Paul Rubin http
Wed Apr 12 10:18:45 EDT 2006


Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
> e.g. you would do this: random.sample(xrange(10**10), 60)
> except it raises an exception.

For a population that large and a sample that small (less than
sqrt(population size), the chance of collision is fairly small, so you
can just discard duplicates.

This relies on Python 2.4's randrange function to generate arbitrarily
large ranges, which in turn relies on having getrandbits (new 2.4
feature, thanks Ray) available:

    samp = Set()
    while len(samp) < 60:
       samp.add(random.randrange(10**10))
    



More information about the Python-list mailing list