8000 BUG: Better check for invalid bounds in np.random.uniform. · numpy/numpy@f5bb42f · GitHub
[go: up one dir, main page]

Skip to content

Commit f5bb42f

Browse files
committed
BUG: Better check for invalid bounds in np.random.uniform.
Also check for invalid bounds when low= and high= are arraylike rather than scalar (closes #8226)
1 parent 6ae8420 commit f5bb42f

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

numpy/random/mtrand/mtrand.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,10 @@ cdef class RandomState:
12951295
Py_INCREF(temp) # needed to get around Pyrex's automatic reference-counting
12961296
# rules because EnsureArray steals a reference
12971297
odiff = <ndarray>PyArray_EnsureArray(temp)
1298+
1299+
if not np.all(np.isfinite(odiff)):
1300+
raise OverflowError('Range exceeds valid bounds')
1301+
12981302
return cont2_array(self.internal_state, rk_uniform, size, olow, odiff,
12991303
self.lock)
13001304

numpy/random/tests/test_random.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ def test_uniform_range_bounds(self):
809809
assert_raises(OverflowError, func, -np.inf, 0)
810810
assert_raises(OverflowError, func, 0, np.inf)
811811
assert_raises(OverflowError, func, fmin, fmax)
812+
assert_raises(OverflowError, func, [-np.inf], [0])
813+
assert_raises(OverflowError, func, [0], [np.inf])
812814

813815
# (fmax / 1e17) - fmin is within range, so this should not throw
814816
np.random.uniform(low=fmin, high=fmax / 1e17)

0 commit comments

Comments
 (0)
0