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]
>>> sample(a,2)
[298023223876953125L, 847288609443L]
>>>
Is this what you were asking, or did you mean something different?
I'm thinking you might need to find another way to do whatever it is you
are trying to do.
If you can't, you could do something like this:
- you want to randomly choose a small number of items at random from a
population of size N, where N is very large.
e.g. you would do this: random.sample(xrange(10**10), 60)
except it raises an exception.
- divide your population of N items in B bins of size M, where both B and
M are in the range of small integers. Ideally, all your bins will be equal
in size.
e.g.
bins = [xrange(start*10**5, (start+1)*10**5) \
for start in xrange(10**5)]
- then, to take a sample of n items, do something like this:
# bins is the list of B bins;
# each bin has M items, and B*M = N the total population.
result = []
while len(result) < sample_size:
# choose a random bin
bin = random.choice(bins)
# choose a random element of that bin
selection = random.choice(bin)
if selecting_with_replacement:
result.append(selection)
else:
# each choice must be unique
if not selection in result:
result.append(selection)
Hope that helps.
--
Steven.
> "jordi" <jpah...@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.
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))
Thanks a lot!
--
Jordi