[issue40388] random.choice integer overflow v3.5.2

Mark Dickinson report at bugs.python.org
Sat Apr 25 06:17:10 EDT 2020


Mark Dickinson <dickinsm at gmail.com> added the comment:

I suspect that the OP is doing something like `random.choice(range(10**40))`:

    Python 3.8.2 (default, Feb 27 2020, 19:56:42) 
    [Clang 10.0.1 (clang-1001.0.46.4)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import random
    >>> random.choice(range(10**40))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/random.py", line 288, in choice
        i = self._randbelow(len(seq))
    OverflowError: Python int too large to convert to C ssize_t

The advice in this case would be to use something like random.randrange(10**40) instead.

The actual bug here, if there is a bug, has nothing to do with random, but instead has to do with "virtual" sequences whose length exceeds 2**63 - 1. There are other issues in the tracker for this, for example issue 12159.

I suggest closing as "not a bug".

----------
nosy: +mark.dickinson

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40388>
_______________________________________


More information about the Python-bugs-list mailing list