8000 BUG: Enforce high >= low on uniform number generators by bashtage · Pull Request #17921 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Enforce high >= low on uniform number generators #17921

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

Merged
merged 2 commits into from
Dec 14, 2020
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
Next Next commit
BUG: Enforce high >= low on uniform number generators
Check that high is weakly larger than low and raise if now

closes #17905
  • Loading branch information
Kevin Sheppard authored and bashtage committed Dec 11, 2020
commit 52ecd5a97ce3ff4dad72935b8bf67ba2cfb6908e
15 changes: 8 additions & 7 deletions numpy/random/_generator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,8 @@ cdef class Generator:
greater than or equal to low. The default value is 0.
high : float or array_like of floats
Upper boundary of the output interval. All values generated will be
less than high. The default value is 1.0.
less than high. The default value is 1.0. high - low must be
non-negative.
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. If size is ``None`` (default),
Expand Down Expand Up @@ -914,7 +915,7 @@ cdef class Generator:
"""
cdef bint is_scalar = True
cdef np.ndarray alow, ahigh, arange
cdef double _low, _high, range
cdef double _low, _high, rng
cdef object temp

alow = <np.ndarray>np.PyArray_FROM_OTF(low, np.NPY_DOUBLE, np.NPY_ALIGNED)
Expand All @@ -923,13 +924,13 @@ cdef class Generator:
if np.PyArray_NDIM(alow) == np.PyArray_NDIM(ahigh) == 0:
_low = PyFloat_AsDouble(low)
_high = PyFloat_AsDouble(high)
range = _high - _low
if not np.isfinite(range):
raise OverflowError('Range exceeds valid bounds')
rng = _high - _low
if not np.isfinite(rng):
raise OverflowError('high - low range exceeds valid bounds')

return cont(&random_uniform, &self._bitgen, size, self.lock, 2,
_low, '', CONS_NONE,
range, '', CONS_NONE,
rng, 'high - low', CONS_NON_NEGATIVE,
0.0, '', CONS_NONE,
None)

Expand All @@ -943,7 +944,7 @@ cdef class Generator:
raise OverflowError('Range exceeds valid bounds')
return cont(&random_uniform, &self._bitgen, size, self.lock, 2,
alow, '', CONS_NONE,
arange, '', CONS_NONE,
arange, 'high - low', CONS_NON_NEGATIVE,
0.0, '', CONS_NONE,
None)

Expand Down
15 changes: 8 additions & 7 deletions numpy/random/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,8 @@ cdef class RandomState:
greater than or equal to low. The default value is 0.
high : float or array_like of floats
Upper boundary of the output interval. All values generated will be
less than or equal to high. The default value is 1.0.
less than or equal to high. The default value is 1.0. high - low must be
non-negative.
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. If size is ``None`` (default),
Expand Down Expand Up @@ -1095,7 +1096,7 @@ cdef class RandomState:
"""
cdef bint is_scalar = True
cdef np.ndarray alow, ahigh, arange
cdef double _low, _high, range
cdef double _low, _high, rng
cdef object temp

alow = <np.ndarray>np.PyArray_FROM_OTF(low, np.NPY_DOUBLE, np.NPY_ALIGNED)
Expand All @@ -1104,13 +1105,13 @@ cdef class RandomState:
if np.PyArray_NDIM(alow) == np.PyArray_NDIM(ahigh) == 0:
_low = PyFloat_AsDouble(low)
_high = PyFloat_AsDouble(high)
range = _high - _low
if not np.isfinite(range):
raise OverflowError('Range exceeds valid bounds')
rng = _high - _low
if not np.isfinite(rng):
raise OverflowError('High - low range exceeds valid bounds')

return cont(&random_uniform, &self._bitgen, size, self.lock, 2,
_low, '', CONS_NONE,
range, '', CONS_NONE,
rng, 'high - low', CONS_NON_NEGATIVE,
0.0, '', CONS_NONE,
None)

Expand All @@ -1123,7 +1124,7 @@ cdef class RandomState:
raise OverflowError('Range exceeds valid bounds')
return cont(&random_uniform, &self._bitgen, size, self.lock, 2,
alow, '', CONS_NONE,
arange, '', CONS_NONE,
arange, 'high - low', CONS_NON_NEGATIVE,
0.0, '', CONS_NONE,
None)

Expand Down
6 changes: 6 additions & 0 deletions numpy/random/tests/test_generator_mt19937.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,12 @@ def test_uniform_range_bounds(self):
# DBL_MAX by increasing fmin a bit
random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17)

def test_uniform_neg_range(self):
func = random.uniform
assert_raises(ValueError, func, 2, 1)
assert_raises(ValueError, func, [1, 2], [1, 1])
assert_raises(ValueError, func, [[0, 1],[2, 3]], 2)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert_raises(ValueError, func, [[0, 1],[2, 3]], 2)
assert_raises(ValueError, func, [[0, 1], [2, 3]], 2)


def test_scalar_exception_propagation(self):
# Tests that exceptions are correctly propagated in distributions
# when called with objects that throw exceptions when converted to
Expand Down
6 changes: 6 additions & 0 deletions numpy/random/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,12 @@ def test_uniform_range_bounds(self):
# account for i386 extended precision DBL_MAX / 1e17 + DBL_MAX >
# DBL_MAX by increasing fmin a bit
np.random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17)

def test_uniform_neg_range(self):
func = np.random.uniform
assert_raises(ValueError, func, 2, 1)
assert_raises(ValueError, func, [1, 2], [1, 1])
assert_raises(ValueError, func, [[0, 1],[2, 3]], 2)

def test_scalar_exception_propagation(self):
# Tests that exceptions are correctly propagated in distributions
Expand Down
0