[issue24567] random.choice IndexError due to double-rounding

STINNER Victor report at bugs.python.org
Tue Jun 26 18:32:21 EDT 2018


STINNER Victor <vstinner at redhat.com> added the comment:

Let me see random_double_round.diff.

In master:

    def shuffle(self, x, random=None):
        """Shuffle list x in place, and return None.

        Optional argument random is a 0-argument function returning a
        random float in [0.0, 1.0); if it is the default None, the
        standard random.random will be used.

        """

This method has a weird API. What is the point of passing a random function, whereas shuffle() is already a method of an object which generates random numbers?! The bug only affects the case when random is set. I proposed to deprecate this argument and remove it later.

                return [population[_min(_int(random() * total), total)]
                        for i in range(k)]

Why not using _randbelow() here? For speed according to:

> some variant of `int(random() * someinteger)` is being used in an inner loop for speed.

Why not optimizing _randbelow() in this case? Like implementing it in C?

----------

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


More information about the Python-bugs-list mailing list