Skip to content

BUG: random: Avoid bad behavior of hypergeometric with very large arguments. #11475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4259,8 +4259,12 @@ cdef class RandomState:

if lngood < 0:
raise ValueError("ngood < 0")
if lngood >= 2**30:
raise ValueError("ngood must be less than 2**30")
if lnbad < 0:
raise ValueError("nbad < 0")
if lnbad >= 2**30:
raise ValueError("nbad must be less than 2**30")
if lnsample < 1:
raise ValueError("nsample < 1")
if lngood + lnbad < lnsample:
Expand All @@ -4270,8 +4274,12 @@ cdef class RandomState:

if np.any(np.less(ongood, 0)):
raise ValueError("ngood < 0")
if np.any(np.greater_equal(ongood, 2**30)):
raise ValueError("ngood must be less than 2**30")
if np.any(np.less(onbad, 0)):
raise ValueError("nbad < 0")
if np.any(np.greater_equal(onbad, 2**30)):
raise ValueError("nbad must be less than 2**30")
if np.any(np.less(onsample, 1)):
raise ValueError("nsample < 1")
if np.any(np.less(np.add(ongood, onbad),onsample)):
Expand Down
6 changes: 2 additions & 4 deletions numpy/random/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ def test_hypergeometric_range(self):

# Test for ticket #5623
args = [
(2**20 - 2, 2**20 - 2, 2**20 - 2), # Check for 32-bit systems
(2**20 - 2, 2**20 - 2, 2**20 - 2),
(2**30 - 1, 2**30 - 1, 2**30 - 1),
]
is_64bits = sys.maxsize > 2**32
if is_64bits and sys.platform != 'win32':
args.append((2**40 - 2, 2**40 - 2, 2**40 - 2)) # Check for 64-bit systems
for arg in args:
assert_(np.random.hypergeometric(*arg) > 0)

Expand Down