Python Forum
random.sample raises OverflowError
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random.sample raises OverflowError
#1
from random import sample

sample(range(2 ** 63 - 1), 3) # OK
sample(range(2 ** 63), 3) # OverflowError
Error:
Traceback (most recent call last): File "c:\...\test.py", line 4, in <module> sample(range(2 ** 63), 3) # OverflowError File "C:\Users\...\AppData\Local\Programs\Python\Python38\lib\random.py", line 361, in sample n = len(population) OverflowError: Python int too large to convert to C ssize_t
Documentation of random.sample
Quote:random.sample(population, k)
Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices).

Members of the population need not be hashable or unique. If the population contains repeats, then each occurrence is a possible selection in the sample.

To choose a sample from a range of integers, use a range() object as an argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), k=60).

If the sample size is larger than the population size, a ValueError is raised.

OverflowError will be raised if the population size is larger than (2 ** 63 - 1).

Python version: 3.8.1
OS: Windows 10 pro 1909 (18363.592)
Reply
#2
so what is your question?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jan-28-2020, 07:03 PM)buran Wrote: so what is your question?

I don't think OverflowError should be raised in this case.
Just because population size can't convert to a 64-bit signed integer? (Even so, why not 'unsigned'?)
The documentation didn't mentioned that 'sample' method has this limit.

This error is caused by 'len(range(n))':
len(range(2 ** 63 - 1)) # OK
len(range(2 ** 63)) # OverflowError
Error:
Traceback (most recent call last): File "c:\Users\...\test.py", line 6, in <module> len(range(2 ** 63)) OverflowError: Python int too large to convert to C ssize_t
Why 'len(range(n))' has this limit?
Is this a bug?
Reply
#4
sys.maxsize():

Quote:An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform.

Look also at PEP237 - Unifying Long Integers and Integers
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Jan-29-2020, 06:07 AM)perfringo Wrote: sys.maxsize():

Quote:An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform.

Look also at PEP237 - Unifying Long Integers and Integers

Thanks for your help. The documentation of range also explained the problem.

Quote:Ranges containing absolute values larger than sys.maxsize are permitted but some features (such as len()) may raise OverflowError.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample random, unique string pairs from a list without repetitions walterwhite 1 446 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  random.choices and random.sample azizrasul 5 2,192 Sep-16-2022, 08:10 PM
Last Post: deanhystad
  open(file, 'rb') raises UnicodeDecodeError binnybit 1 2,464 Sep-28-2020, 06:55 AM
Last Post: Gribouillis
  OverflowError: cannot fit 'int' into an index-sized integer vipinv23 2 9,240 Jan-17-2019, 04:08 AM
Last Post: vipinv23

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020