8000 ENH: Expand Argument Range of random_integers by gfyoung · Pull Request #6885 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Expand Argument Range of random_integers #6885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions numpy/random/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test looks iffy. Since we are just testing if the maximum is obtainable and the range is closed, maybe make both high and low equal to np.iinfo('l').max. Also needs a comment as to what is being tested for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, of course! That's much simpler! Will do.

# 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))
Expand Down
0