mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
bpo-40282: Allow random.getrandbits(0) (GH-19539)
This commit is contained in:
parent
d7c657d4b1
commit
75a3378810
5 changed files with 42 additions and 44 deletions
|
|
@ -474,12 +474,15 @@ _random_Random_getrandbits_impl(RandomObject *self, int k)
|
|||
uint32_t *wordarray;
|
||||
PyObject *result;
|
||||
|
||||
if (k <= 0) {
|
||||
if (k < 0) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"number of bits must be greater than zero");
|
||||
"number of bits must be non-negative");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (k == 0)
|
||||
return PyLong_FromLong(0);
|
||||
|
||||
if (k <= 32) /* Fast path */
|
||||
return PyLong_FromUnsignedLong(genrand_uint32(self) >> (32 - k));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue