[issue37000] _randbelow_with_getrandbits function inefficient with powers of two

Raymond Hettinger report at bugs.python.org
Thu May 23 11:22:36 EDT 2019


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

> it's a bit surprising all on its own that `getrandbits(0)` 
> raises an exception.

Given that there would be no randomness in the result, it makes sense to me that getrandbits(0) is documented to raise an exception.

Related:
    `randrange(0)` raises an exception
    `choice([])` raises an exception


> In any case, I'd leave _randbelow_with_getrandbits alone.

That makes sense to me as well. I'll mark this as closed.

There's one other bright side.  If someone really cares about the speed of the power-of-two case, they can already call `getrandbits(10)` instead of `randrange(1024)`.  The former is about 7x faster.

Mathis, thank you for taking the time to look at this code.

----------
resolution:  -> rejected
stage: patch review -> resolved
status: open -> closed

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


More information about the Python-bugs-list mailing list