8000 Revert changes to mtrand · numpy/numpy@a3bb19d · GitHub
[go: up one dir, main page]

Skip to content

Commit a3bb19d

Browse files
Kevin Sheppardbashtage
authored andcommitted
Revert changes to mtrand
This doesn't qualify for fixing under the NEP.
1 parent 52ecd5a commit a3bb19d

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Validate input values in ``Generator.uniform``
2+
----------------------------------------------
3+
Checked that ``high - low >= 0`` in ``np.random.Generator.uniform``. Raises
4+
``ValueError`` if ``low > high``. Previously out-of-order inputs were accepted
5+
and silently swapped, so that if ``low > high``, the value generated was
6+
``high + (low - high) * random()``.

numpy/random/_generator.pyx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,8 @@ cdef class Generator:
859859
greater than or equal to low. The default value is 0.
860860
high : float or array_like of floats
861861
Upper boundary of the output interval. All values generated will be
862-
less than high. The default value is 1.0. high - low must be
863-
non-negative.
862+
less than high. high - low must be non-negative. The default value
863+
is 1.0.
864864
size : int or tuple of ints, optional
865865
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
866866
``m * n * k`` samples are drawn. If size is ``None`` (default),
@@ -886,10 +886,6 @@ cdef class Generator:
886886
anywhere within the interval ``[a, b)``, and zero elsewhere.
887887
888888
When ``high`` == ``low``, values of ``low`` will be returned.
889-
If ``high`` < ``low``, the results are officially undefined
890-
and may eventually raise an error, i.e. do not rely on this
891-
function to behave when passed arguments satisfying that
892-
inequality condition.
893889
894890
Examples
895891
--------

numpy/random/mtrand.pyx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,7 @@ cdef class RandomState:
10261026
greater than or equal to low. The default value is 0.
10271027
high : float or array_like of floats
10281028
Upper boundary of the output interval. All values generated will be
1029-
less than or equal to high. The default value is 1.0. high - low must be
1030-
non-negative.
1029+
less than or equal to high. The default value is 1.0.
10311030
size : int or tuple of ints, optional
10321031
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
10331032
``m * n * k`` samples are drawn. If size is ``None`` (default),
@@ -1096,7 +1095,7 @@ cdef class RandomState:
10961095
"""
10971096
cdef bint is_scalar = True
10981097
cdef np.ndarray alow, ahigh, arange
1099-
cdef double _low, _high, rng
1098+
cdef double _low, _high, range
11001099
cdef object temp
11011100

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

11121111
return cont(&random_uniform, &self._bitgen, size, self.lock, 2,
11131112
_low, '', CONS_NONE,
1114-
rng, 'high - low', CONS_NON_NEGATIVE,
1113+
range, '', CONS_NONE,
11151114
0.0, '', CONS_NONE,
11161115
None)
11171116

@@ -1124,7 +1123,7 @@ cdef class RandomState:
11241123
raise OverflowError('Range exceeds valid bounds')
11251124
return cont(&random_uniform, &self._bitgen, size, self.lock, 2,
11261125
alow, '', CONS_NONE,
1127-
arange, 'high - low', CONS_NON_NEGATIVE,
1126+
arange, '', CONS_NONE,
11281127
0.0, '', CONS_NONE,
11291128
None)
11301129

numpy/random/tests/test_generator_mt19937.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,15 @@ def test_uniform_range_bounds(self):
16661666
# DBL_MAX by increasing fmin a bit
16671667
random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17)
16681668

1669+
def test_uniform_zero_range(self):
1670+
func = random.uniform
1671+
result = func(1.5, 1.5)
1672+
assert_allclose(result, 1.5)
1673+
result = func([0.0, np.pi], [0.0, np.pi])
1674+
assert_allclose(result, [0.0, np.pi])
1675+
result = func([[2145.12], [2145.12]], [2145.12, 2145.12])
1676+
assert_allclose(result, 2145.12 + np.zeros((2, 2)))
1677+
16691678
def test_uniform_neg_range(self):
16701679
func = random.uniform
16711680
assert_raises(ValueError, func, 2, 1)

numpy/random/tests/test_random.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -916,12 +916,6 @@ def test_uniform_range_bounds(self):
916916
# account for i386 extended precision DBL_MAX / 1e17 + DBL_MAX >
917917
# DBL_MAX by increasing fmin a bit
918918
np.random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17)
919-
920-
def test_uniform_neg_range(self):
921-
func = np.random.uniform
922-
assert_raises(ValueError, func, 2, 1)
923-
assert_raises(ValueError, func, [1, 2], [1, 1])
924-
assert_raises(ValueError, func, [[0, 1],[2, 3]], 2)
925919

926920
def test_scalar_exception_propagation(self):
927921
# Tests that exceptions are correctly propagated in distributions

0 commit comments

Comments
 (0)
0