summaryrefslogtreecommitdiff
path: root/numpy/random/_generator.pyx
diff options
context:
space:
mode:
authorKevin Sheppard <kevin.sheppard@gmail.com>2020-12-04 23:03:23 +0000
committerKevin Sheppard <kevin.k.sheppard@gmail.com>2020-12-11 15:27:13 +0000
commit52ecd5a97ce3ff4dad72935b8bf67ba2cfb6908e (patch)
tree5561b20cf8b0190021275fc395fb52f46d096830 /numpy/random/_generator.pyx
parentba596df4cc23445a117c3785239a75a3cab6b2f2 (diff)
downloadnumpy-52ecd5a97ce3ff4dad72935b8bf67ba2cfb6908e.tar.gz
BUG: Enforce high >= low on uniform number generators
Check that high is weakly larger than low and raise if now closes #17905
Diffstat (limited to 'numpy/random/_generator.pyx')
-rw-r--r--numpy/random/_generator.pyx15
1 files changed, 8 insertions, 7 deletions
diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx
index 7ffa36775..cd951526b 100644
--- a/numpy/random/_generator.pyx
+++ b/numpy/random/_generator.pyx
@@ -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),
@@ -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)
@@ -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)
@@ -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)