8000 BUG: Fix randint when range is 2**32 · numpy/numpy@d9d620c · GitHub
[go: up one dir, main page]

Skip to content

Commit d9d620c

Browse files
bashtagecharris
authored andcommitted
BUG: Fix randint when range is 2**32
Fix randint to use 32-bit path when range is exactly 2**32 closes #14189
1 parent dbbc9b1 commit d9d620c

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
`numpy.random.randint` produced incorrect value when the range was ``2**32``
2+
----------------------------------------------------------------------------
3+
The implementation introduced in 1.17.0 had an incorrect check when
4+
determining whether to use the 32-bit path or the full 64-bit
5+
path that incorrectly redirected random integer generation with a high - low
6+
range of ``2**32`` to the 64-bit generator.

numpy/random/src/distributions/distributions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ uint64_t random_bounded_uint64(bitgen_t *bitgen_state, uint64_t off,
14781478
uint64_t rng, uint64_t mask, bool use_masked) {
14791479
if (rng == 0) {
14801480
return off;
1481-
} else if (rng < 0xFFFFFFFFUL) {
1481+
} else if (rng <= 0xFFFFFFFFUL) {
14821482
/* Call 32-bit generator if range in 32-bit. */
14831483
if (use_masked) {
14841484
return off + buffered_bounded_masked_uint32(bitgen_state, rng, mask, NULL,
@@ -1592,7 +1592,7 @@ void random_bounded_uint64_fill(bitgen_t *bitgen_state, uint64_t off,
15921592
for (i = 0; i < cnt; i++) {
15931593
out[i] = off;
15941594
}
1595-
} else if (rng < 0xFFFFFFFFUL) {
1595+
} else if (rng <= 0xFFFFFFFFUL) {
15961596
uint32_t buf = 0;
15971597
int bcnt = 0;
15981598

numpy/random/tests/test_randomstate_regression.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,14 @@ def test_choice_retun_dtype(self):
181181
assert c.dtype == np.dtype(int)
182182
c = np.random.choice(10, replace=False, size=2)
183183
assert c.dtype == np.dtype(int)
184+
185+
@pytest.mark.skipif(np.iinfo('l').max < 2**32,
186+
reason='Cannot test with 32-bit C long')
187+
def test_randint_117(self):
188+
# GH 14189
189+
random.seed(0)
190+
expected = np.array([2357136044, 2546248239, 3071714933, 3626093760,
191+
2588848963, 3684848379, 2340255427, 3638918503,
192+
1819583497, 2678185683], dtype='int64')
193+
actual = random.randint(2**32, size=10)
194+
assert_array_equal(actual, expected)

0 commit comments

Comments
 (0)
0