summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/random/mtrand/mtrand.pyx66
-rw-r--r--numpy/random/tests/test_random.py11
2 files changed, 46 insertions, 31 deletions
diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx
index e12c7669d..5120857d0 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 = <unsigned long>hi - <unsigned long>lo - 1UL
- if size is None:
- with self.lock:
- rv = lo + <long>rk_interval(diff, self. internal_state)
- return rv
- else:
- array = <ndarray>np.empty(size, int)
- length = PyArray_SIZE(array)
- array_data = <long *>PyArray_DATA(array)
- with self.lock, nogil:
- for i from 0 <= i < length:
- rv = lo + <long>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 = <unsigned long>hi - <unsigned long>lo
+ if size is None:
+ with self.lock:
+ rv = lo + <long>rk_interval(diff, self. internal_state)
+ return rv
+ else:
+ array = <ndarray>np.empty(size, int)
+ length = PyArray_SIZE(array)
+ array_data = <long *>PyArray_DATA(array)
+ with self.lock, nogil:
+ for i from 0 <= i < length:
+ rv = lo + <long>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 193844030..0ce341ead 100644
--- a/numpy/random/tests/test_random.py
+++ b/numpy/random/tests/test_random.py
@@ -167,6 +167,17 @@ class TestRandomDist(TestCase):
[-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))