Skip to content
Open
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
11 changes: 11 additions & 0 deletions quantecon/random/tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ def test_return_types(self):
out = func(self.cdf, size)
assert_(out.shape == (size,))

def test_numpy_integer_size_returns_array(self):
size = np.int64(10)
for func in self.draw_funcs:
out = func(self.cdf, size)
assert_(out.shape == (size,))

def test_bool_size_is_treated_as_scalar(self):
for func in self.draw_funcs:
out = func(self.cdf, True)
assert_(isinstance(out, numbers.Integral))

def test_return_values(self):
for func in self.draw_funcs:
out = func(self.cdf)
Expand Down
11 changes: 8 additions & 3 deletions quantecon/random/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,11 @@ def draw(cdf, size=None, rng=None):
"""
if rng is None:
rng = np.random
if isinstance(size, int):
integer_size = (
isinstance(size, (int, np.integer))
and not isinstance(size, (bool, np.bool_))
)
if integer_size:
rs = rng.random(size)
out = np.searchsorted(cdf, rs, side='right')
return out
Expand Down Expand Up @@ -287,8 +291,9 @@ def _is_no_rng(numba_type):
# holds the two paths together.
@overload(draw)
def ol_draw(cdf, size=None, rng=None):
is_integer_size = isinstance(size, types.Integer) and not isinstance(size, types.Boolean)
if isinstance(rng, types.NumPyRandomGeneratorType):
if isinstance(size, types.Integer):
if is_integer_size:
def draw_impl(cdf, size=None, rng=None):
rs = rng.random(size)
out = np.empty(size, dtype=np.int_)
Expand All @@ -300,7 +305,7 @@ def draw_impl(cdf, size=None, rng=None):
r = rng.random()
return np.searchsorted(cdf, r, side='right')
elif _is_no_rng(rng):
if isinstance(size, types.Integer):
if is_integer_size:
def draw_impl(cdf, size=None, rng=None):
rs = np.random.random(size)
out = np.empty(size, dtype=np.int_)
Expand Down