diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx index e12c7669d13c..5120857d04b2 100644 --- a/numpy/random/mtrand/mtrand.pyx +++ b/numpy/random/mtrand/mtrand.pyx @@ -936,37 +936,14 @@ cdef class RandomState: [3, 2, 2, 0]]) """ - cdef long lo, hi, rv - cdef unsigned long diff - cdef long *array_data - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + if high is not None and low >= high: + raise ValueError("low >= high") if high is None: - lo = 0 - hi = low - else: - lo = low - hi = high - - if lo >= hi : - raise ValueError("low >= high") + high = low + low = 0 - diff = hi - lo - 1UL - if size is None: - with self.lock: - rv = lo + rk_interval(diff, self. internal_state) - return rv - else: - array = np.empty(size, int) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) - with self.lock, nogil: - for i from 0 <= i < length: - rv = lo + rk_interval(diff, self. internal_state) - array_data[i] = rv - return array + return self.random_integers(low, high - 1, size) def bytes(self, npy_intp length): """ @@ -1449,10 +1426,37 @@ cdef class RandomState: >>> plt.show() """ + if high is not None and low > high: + raise ValueError("low > high") + + cdef long lo, hi, rv + cdef unsigned long diff + cdef long *array_data + cdef ndarray array "arrayObject" + cdef npy_intp length + cdef npy_intp i + if high is None: - high = low - low = 1 - return self.randint(low, high+1, size) + lo = 1 + hi = low + else: + lo = low + hi = high + + diff = hi - lo + if size is None: + with self.lock: + rv = lo + rk_interval(diff, self. internal_state) + return rv + else: + array = np.empty(size, int) + length = PyArray_SIZE(array) + array_data = PyArray_DATA(array) + with self.lock, nogil: + for i from 0 <= i < length: + rv = lo + rk_interval(diff, self. internal_state) + array_data[i] = rv + return array # Complicated, continuous distributions: def standard_normal(self, size=None): diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 193844030efd..0ce341eadb6b 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -167,6 +167,17 @@ def test_random_integers(self): [-48, -66]]) np.testing.assert_array_equal(actual, desired) + def test_random_integers_max_int(self): + # Tests whether random_integers can generate the + # maximum allowed Python int that can be converted + # into a C long. Previous implementations of this + # method have thrown an OverflowError when attemping + # to generate this integer. + actual = np.random.random_integers(np.iinfo('l').max, + np.iinfo('l').max) + desired = np.iinfo('l').max + np.testing.assert_equal(actual, desired) + def test_random_sample(self): np.random.seed(self.seed) actual = np.random.random_sample((3, 2))