Skip to content

Commit

Permalink
Merge pull request #14515 from charris/backport-14501
Browse files Browse the repository at this point in the history
BUG: Fix randint when range is 2**32
  • Loading branch information
charris committed Sep 15, 2019
2 parents dbbc9b1 + d9d620c commit 706deec
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions doc/release/upcoming_changes/14501.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
`numpy.random.randint` produced incorrect value when the range was ``2**32``
----------------------------------------------------------------------------
The implementation introduced in 1.17.0 had an incorrect check when
determining whether to use the 32-bit path or the full 64-bit
path that incorrectly redirected random integer generation with a high - low
range of ``2**32`` to the 64-bit generator.
4 changes: 2 additions & 2 deletions numpy/random/src/distributions/distributions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ uint64_t random_bounded_uint64(bitgen_t *bitgen_state, uint64_t off,
uint64_t rng, uint64_t mask, bool use_masked) {
if (rng == 0) {
return off;
} else if (rng < 0xFFFFFFFFUL) {
} else if (rng <= 0xFFFFFFFFUL) {
/* Call 32-bit generator if range in 32-bit. */
if (use_masked) {
return off + buffered_bounded_masked_uint32(bitgen_state, rng, mask, NULL,
Expand Down Expand Up @@ -1592,7 +1592,7 @@ void random_bounded_uint64_fill(bitgen_t *bitgen_state, uint64_t off,
for (i = 0; i < cnt; i++) {
out[i] = off;
}
} else if (rng < 0xFFFFFFFFUL) {
} else if (rng <= 0xFFFFFFFFUL) {
uint32_t buf = 0;
int bcnt = 0;

Expand Down
11 changes: 11 additions & 0 deletions numpy/random/tests/test_randomstate_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,14 @@ def test_choice_retun_dtype(self):
assert c.dtype == np.dtype(int)
c = np.random.choice(10, replace=False, size=2)
assert c.dtype == np.dtype(int)

@pytest.mark.skipif(np.iinfo('l').max < 2**32,
reason='Cannot test with 32-bit C long')
def test_randint_117(self):
# GH 14189
random.seed(0)
expected = np.array([2357136044, 2546248239, 3071714933, 3626093760,
2588848963, 3684848379, 2340255427, 3638918503,
1819583497, 2678185683], dtype='int64')
actual = random.randint(2**32, size=10)
assert_array_equal(actual, expected)

0 comments on commit 706deec

Please sign in to comment.