Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,20 @@ def test_getrandbits(self):
self.assertEqual(self.gen.getrandbits(100),
97904845777343510404718956115)

def test_getrandbits_2G_bits(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be decorated with bigmemtest? Ditto the second test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It requires only 800 MB of memory. Too small for bigmemtest. The second test requires about 1300 MB of memory. This may be too small too, but I'm going to add bigmemtest after testing on 32-bit buildbots.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It requires only 800 MB of memory. Too small for bigmemtest.

Where is the boundary? IIRC, memory limit is configurable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was 1GiB (you could not specify less than 1 GiB for -M option in regrtest). But now it seems is 2 GiB.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it was initially 2.5 GiB, then lowered to 2 GiB. But I add bigmemtest even for some tests that need 1-2 GiB.

self.gen.seed(1234567)
x = self.gen.getrandbits(2**31)
self.assertEqual(x.bit_length(), 2**31)
self.assertEqual(x >> (2**31-100), 1226514312032729439655761284440)
self.assertEqual(x & (2**100-1), 890186470919986886340158459475)

def test_getrandbits_4G_bits(self):
self.gen.seed(1234568)
x = self.gen.getrandbits(2**32)
self.assertEqual(x.bit_length(), 2**32)
self.assertEqual(x >> (2**32-100), 739728759900339699429794460738)
self.assertEqual(x & (2**100-1), 287241425661104632871036099814)

def test_randrange_uses_getrandbits(self):
# Verify use of getrandbits by randrange
# Use same seed as in the cross-platform repeatability test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`random.getrandbits` can now generate more that 2\ :sup:`31` bits.
:func:`random.randbytes` can now generate more that 256 MiB.
20 changes: 9 additions & 11 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,34 +497,32 @@ _random_Random_setstate_impl(RandomObject *self, PyObject *state)
_random.Random.getrandbits

self: self(type="RandomObject *")
k: int
k: uint64
/

getrandbits(k) -> x. Generates an int with k random bits.
[clinic start generated code]*/

static PyObject *
_random_Random_getrandbits_impl(RandomObject *self, int k)
/*[clinic end generated code: output=b402f82a2158887f input=87603cd60f79f730]*/
_random_Random_getrandbits_impl(RandomObject *self, uint64_t k)
/*[clinic end generated code: output=c30ef8435f3433cf input=64226ac13bb4d2a3]*/
{
int i, words;
Py_ssize_t i, words;
uint32_t r;
uint32_t *wordarray;
PyObject *result;

if (k < 0) {
PyErr_SetString(PyExc_ValueError,
"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));

words = (k - 1) / 32 + 1;
if ((k - 1u) / 32u + 1u > PY_SSIZE_T_MAX / 4u) {
PyErr_NoMemory();
return NULL;
}
words = (k - 1u) / 32u + 1u;
wordarray = (uint32_t *)PyMem_Malloc(words * 4);
if (wordarray == NULL) {
PyErr_NoMemory();
Expand Down
10 changes: 5 additions & 5 deletions Modules/clinic/_randommodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading