This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Mathis Hammel
Recipients Mathis Hammel, mark.dickinson, rhettinger
Date 2019-05-21.20:50:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1558471816.52.0.343821776026.issue37000@roundup.psfhosted.org>
In-reply-to
Content
In case _randbelow_with_getrandbits is called with a power of two as its argument (say 2^k), the function will consume k+1 random bits instead of k.

Instead of never being rejected, the sampled number will be rejected on average once per call, causing a computational overhead in addition to wasting k+2 bits on average.

This is due to taking n.bit_size() instead of (n-1).bit_size() for the size of the random candidates. Using n instead of n-1 is apparently deliberate in order to save the case where n = 1, but this could be avoided by directly returning 0 if n == 1.
History
Date User Action Args
2019-05-21 20:50:16Mathis Hammelsetrecipients: + Mathis Hammel, rhettinger, mark.dickinson
2019-05-21 20:50:16Mathis Hammelsetmessageid: <1558471816.52.0.343821776026.issue37000@roundup.psfhosted.org>
2019-05-21 20:50:16Mathis Hammellinkissue37000 messages
2019-05-21 20:50:16Mathis Hammelcreate