[issue41860] random.choices() raises "int too large" error while random.randint does not

2020-09-25 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

This is a known limitation and there isn't much we can do about it. 

The root cause is that for performance reasons the len() function doesn't 
handle sizes larger than a C ssize_t:

>>> len(range(2**100))
Traceback (most recent call last):
... 
OverflowError: Python int too large to convert to C ssize_t

For the same reason, you would also see the same error for random.choice():

>>> random.choice(range(2**100))
Traceback (most recent call last):
... 
OverflowError: Python int too large to convert to C ssize_t

Given that we can't get the size of the population, there isn't much that 
choice() or choices() can do about the situation without special casing range 
objects and reconstructing what len() would have returned had it not been 
restricted.  Given that this hasn't seemed to have ever been a problem in 
practice, I recommend just using randrange() in a loop.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41860] random.choices() raises "int too large" error while random.randint does not

2020-09-25 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

This is virtually a duplicate of issue40388.

--
nosy: +mark.dickinson, rhettinger, serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41860] random.choices() raises "int too large" error while random.randint does not

2020-09-24 Thread Heng Sun


New submission from Heng Sun :

If I run this one line of code:

random.choices(range(2**100), k=5)

I would get error:

OverflowError: Python int too large to convert to C ssize_t

But I can run equivalent line to achieve this without error:

[random.randint(0, 2**100-1) for j in range(5)]

With the understanding of the issue coming from len(), ref 
https://bugs.python.org/issue12159, I still think random.choices() should be 
able to handle large integers.

--
components: Library (Lib)
messages: 377480
nosy: mathtester
priority: normal
severity: normal
status: open
title: random.choices() raises "int too large" error while random.randint does 
not
type: enhancement
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com