diff --git a/benchmarks/benchmarks/bench_random.py b/benchmarks/benchmarks/bench_random.py index 18444b9a1052..6a22e5736bd8 100644 --- a/benchmarks/benchmarks/bench_random.py +++ b/benchmarks/benchmarks/bench_random.py @@ -34,11 +34,11 @@ class Randint(Benchmark): def time_randint_fast(self): """Compare to uint32 below""" - np.random.randint(0, 2**30, size=10**5) + np.random.randint(low=0, high=2**30, size=10**5) def time_randint_slow(self): """Compare to uint32 below""" - np.random.randint(0, 2**30 + 1, size=10**5) + np.random.randint(low=0, high=2**30 + 1, size=10**5) class Randint_dtype(Benchmark): @@ -59,9 +59,9 @@ def setup(self, name): def time_randint_fast(self, name): high = self.high[name] - np.random.randint(0, high, size=10**5, dtype=name) + np.random.randint(low=0, high=high, size=10**5, dtype=name) def time_randint_slow(self, name): high = self.high[name] - np.random.randint(0, high + 1, size=10**5, dtype=name) + np.random.randint(low=0, high=high + 1, size=10**5, dtype=name) diff --git a/doc/release/1.12.0-notes.rst b/doc/release/1.12.0-notes.rst index 09f94141cd0f..7d24824a7cae 100644 --- a/doc/release/1.12.0-notes.rst +++ b/doc/release/1.12.0-notes.rst @@ -104,5 +104,17 @@ Added 'doane' and 'sqrt' estimators to ``histogram`` via the ``bins`` argument. Changes ======= +``np.randint`` can handle more ``low=None`` and ``high=None`` combinations +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +If ``low=None`` and ``high=None``, random numbers will be generated over the +range [``lowbnd``, ``highbnd``), where ``lowbnd`` and ``highbnd`` equal +``np.iinfo(dtype).min`` and ``np.iinfo(dtype).max`` respectively. + +If only ``low=None``, random numbers will be generated over the range [``lowbnd``, +``high``), where ``lowbnd`` is defined as previously. If only ``high=None``, random +numbers will still be generated over the range [``0``, ``low``) provided ``low`` >= 0. +Otherwise, numbers will be generated over [``low``, ``highbnd``), where ``highbnd`` is +defined as previously. + Deprecations ============ diff --git a/numpy/add_newdocs.py b/numpy/add_newdocs.py index 8940f537d508..12e2277756ba 100644 --- a/numpy/add_newdocs.py +++ b/numpy/add_newdocs.py @@ -3680,7 +3680,7 @@ def luf(lamdaexpr, *args, **kwargs): Examples -------- - >>> x = np.random.randint(9, size=(3, 3)) + >>> x = np.random.randint(low=9, size=(3, 3)) >>> x array([[3, 1, 7], [2, 8, 3], diff --git a/numpy/core/tests/test_mem_overlap.py b/numpy/core/tests/test_mem_overlap.py index 82e66db5b798..e131b6b45a44 100644 --- a/numpy/core/tests/test_mem_overlap.py +++ b/numpy/core/tests/test_mem_overlap.py @@ -110,19 +110,19 @@ def test_diophantine_fuzz(): numbers = [] while min(feasible_count, infeasible_count) < min_count: # Ensure big and small integer problems - A_max = 1 + rng.randint(0, 11, dtype=np.intp)**6 - U_max = rng.randint(0, 11, dtype=np.intp)**6 + A_max = 1 + rng.randint(low=0, high=11, dtype=np.intp)**6 + U_max = rng.randint(low=0, high=11, dtype=np.intp)**6 A_max = min(max_int, A_max) U_max = min(max_int-1, U_max) - A = tuple(rng.randint(1, A_max+1, dtype=np.intp) + A = tuple(rng.randint(low=1, high=A_max+1, dtype=np.intp) for j in range(ndim)) - U = tuple(rng.randint(0, U_max+2, dtype=np.intp) + U = tuple(rng.randint(low=0, high=U_max+2, dtype=np.intp) for j in range(ndim)) b_ub = min(max_int-2, sum(a*ub for a, ub in zip(A, U))) - b = rng.randint(-1, b_ub+2, dtype=np.intp) + b = rng.randint(low=-1, high=b_ub+2, dtype=np.intp) if ndim == 0 and feasible_count < min_count: b = 0 @@ -260,9 +260,9 @@ def check_may_share_memory_easy_fuzz(get_max_work, same_steps, min_count): rng = np.random.RandomState(1234) def random_slice(n, step): - start = rng.randint(0, n+1, dtype=np.intp) - stop = rng.randint(start, n+1, dtype=np.intp) - if rng.randint(0, 2, dtype=np.intp) == 0: + start = rng.randint(low=0, high=n+1, dtype=np.intp) + stop = rng.randint(low=start, high=n+1, dtype=np.intp) + if rng.randint(low=0, high=2, dtype=np.intp) == 0: stop, start = start, stop step *= -1 return slice(start, stop, step) @@ -271,14 +271,14 @@ def random_slice(n, step): infeasible = 0 while min(feasible, infeasible) < min_count: - steps = tuple(rng.randint(1, 11, dtype=np.intp) - if rng.randint(0, 5, dtype=np.intp) == 0 else 1 + steps = tuple(rng.randint(low=1, high=11, dtype=np.intp) + if rng.randint(low=0, high=5, dtype=np.intp) == 0 else 1 for j in range(x.ndim)) if same_steps: steps2 = steps else: - steps2 = tuple(rng.randint(1, 11, dtype=np.intp) - if rng.randint(0, 5, dtype=np.intp) == 0 else 1 + steps2 = tuple(rng.randint(low=1, high=11, dtype=np.intp) + if rng.randint(low=0, high=5, dtype=np.intp) == 0 else 1 for j in range(x.ndim)) t1 = np.arange(x.ndim) @@ -378,9 +378,9 @@ def test_internal_overlap_slices(): rng = np.random.RandomState(1234) def random_slice(n, step): - start = rng.randint(0, n+1, dtype=np.intp) - stop = rng.randint(start, n+1, dtype=np.intp) - if rng.randint(0, 2, dtype=np.intp) == 0: + start = rng.randint(low=0, high=n+1, dtype=np.intp) + stop = rng.randint(low=start, high=n+1, dtype=np.intp) + if rng.randint(low=0, high=2, dtype=np.intp) == 0: stop, start = start, stop step *= -1 return slice(start, stop, step) @@ -389,8 +389,8 @@ def random_slice(n, step): min_count = 5000 while cases < min_count: - steps = tuple(rng.randint(1, 11, dtype=np.intp) - if rng.randint(0, 5, dtype=np.intp) == 0 else 1 + steps = tuple(rng.randint(low=1, high=11, dtype=np.intp) + if rng.randint(low=0, high=5, dtype=np.intp) == 0 else 1 for j in range(x.ndim)) t1 = np.arange(x.ndim) rng.shuffle(t1) @@ -474,11 +474,11 @@ def test_internal_overlap_fuzz(): rng = np.random.RandomState(1234) while min(overlap, no_overlap) < min_count: - ndim = rng.randint(1, 4, dtype=np.intp) + ndim = rng.randint(low=1, high=4, dtype=np.intp) - strides = tuple(rng.randint(-1000, 1000, dtype=np.intp) + strides = tuple(rng.randint(low=-1000, high=1000, dtype=np.intp) for j in range(ndim)) - shape = tuple(rng.randint(1, 30, dtype=np.intp) + shape = tuple(rng.randint(low=1, high=30, dtype=np.intp) for j in range(ndim)) a = as_strided(x, strides=strides, shape=shape) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index d57e7c106751..83713c87d24b 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -2017,8 +2017,8 @@ def test_partition_fuzz(self): for i in range(1, j - 2): d = np.arange(j) np.random.shuffle(d) - d = d % np.random.randint(2, 30) - idx = np.random.randint(d.size) + d = d % np.random.randint(low=2, high=30) + idx = np.random.randint(low=d.size) kth = [0, idx, i, i + 1] tgt = np.sort(d)[kth] assert_array_equal(np.partition(d, kth)[kth], tgt, diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index e22a5e193434..4bf289c5854e 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1004,8 +1004,8 @@ class TestIndex(TestCase): def test_boolean(self): a = rand(3, 5, 8) V = rand(5, 8) - g1 = randint(0, 5, size=15) - g2 = randint(0, 8, size=15) + g1 = randint(low=0, high=5, size=15) + g2 = randint(low=0, high=8, size=15) V[g1, g2] = -V[g1, g2] assert_((np.array([a[0][V > 0], a[1][V > 0], a[2][V > 0]]) == a[:, V > 0]).all()) diff --git a/numpy/lib/tests/test_arrayterator.py b/numpy/lib/tests/test_arrayterator.py index 64ad7f4de4b5..9f160d481615 100644 --- a/numpy/lib/tests/test_arrayterator.py +++ b/numpy/lib/tests/test_arrayterator.py @@ -13,13 +13,13 @@ def test(): np.random.seed(np.arange(10)) # Create a random array - ndims = randint(5)+1 - shape = tuple(randint(10)+1 for dim in range(ndims)) + ndims = randint(low=5)+1 + shape = tuple(randint(low=10)+1 for dim in range(ndims)) els = reduce(mul, shape) a = np.arange(els) a.shape = shape - buf_size = randint(2*els) + buf_size = randint(low=2*els) b = Arrayterator(a, buf_size) # Check that each block has at most ``buf_size`` elements @@ -30,9 +30,9 @@ def test(): assert_(list(b.flat) == list(a.flat)) # Slice arrayterator - start = [randint(dim) for dim in shape] - stop = [randint(dim)+1 for dim in shape] - step = [randint(dim)+1 for dim in shape] + start = [randint(low=dim) for dim in shape] + stop = [randint(low=dim)+1 for dim in shape] + step = [randint(low=dim)+1 for dim in shape] slice_ = tuple(slice(*t) for t in zip(start, stop, step)) c = b[slice_] d = a[slice_] diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index cd126fe71141..7339ead5d42e 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2571,7 +2571,7 @@ def test_axis_keyword(self): [0, 1], [6, 7], [4, 5]]) - for a in [a3, np.random.randint(0, 100, size=(2, 3, 4))]: + for a in [a3, np.random.randint(low=0, high=100, size=(2, 3, 4))]: orig = a.copy() np.median(a, axis=None) for ax in range(a.ndim): diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index 989c563d994c..5f6c9cb6d588 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -518,7 +518,7 @@ def test_small_large(self): for s in [5, 20, 51, 200, 1000]: d = np.random.randn(4, s) # Randomly set some elements to NaN: - w = np.random.randint(0, d.size, size=d.size // 5) + w = np.random.randint(low=0, high=d.size, size=d.size // 5) d.ravel()[w] = np.nan d[:,0] = 1. # ensure at least one good value # use normal median without nans to compare diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index ee50dcfa4e62..cf39333bbf95 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -147,13 +147,13 @@ def test_large_fancy_indexing(self, level=rlevel): def dp(): n = 3 a = np.ones((n,)*5) - i = np.random.randint(0, n, size=thesize) + i = np.random.randint(low=0, high=n, size=thesize) a[np.ix_(i, i, i, i, i)] = 0 def dp2(): n = 3 a = np.ones((n,)*5) - i = np.random.randint(0, n, size=thesize) + i = np.random.randint(low=0, high=n, size=thesize) a[np.ix_(i, i, i, i, i)] self.assertRaises(ValueError, dp) diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py index 3f05f80c0b29..f127b6d67c17 100644 --- a/numpy/lib/tests/test_shape_base.py +++ b/numpy/lib/tests/test_shape_base.py @@ -347,7 +347,7 @@ def test_kroncompare(self): reps = [(2,), (1, 2), (2, 1), (2, 2), (2, 3, 2), (3, 2)] shape = [(3,), (2, 3), (3, 4, 3), (3, 2, 3), (4, 3, 2, 4), (2, 2)] for s in shape: - b = randint(0, 10, size=s) + b = randint(low=0, high=10, size=s) for r in reps: a = np.ones(r, b.dtype) large = tile(b, r) diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx index b168bf79c6dd..9d8de85a026d 100644 --- a/numpy/random/mtrand/mtrand.pyx +++ b/numpy/random/mtrand/mtrand.pyx @@ -1159,25 +1159,33 @@ cdef class RandomState: """ return disc0_array(self.internal_state, rk_long, size, self.lock) - def randint(self, low, high=None, size=None, dtype='l'): + def randint(self, low=None, high=None, size=None, dtype='l'): """ - randint(low, high=None, size=None, dtype='l') + randint(low=None, high=None, size=None, dtype='l') Return random integers from `low` (inclusive) to `high` (exclusive). Return random integers from the "discrete uniform" distribution of - the specified dtype in the "half-open" interval [`low`, `high`). If - `high` is None (the default), then results are from [0, `low`). + the specified dtype in the "half-open" interval [`low`, `high`). + + If ``low`` and ``high`` are None (default), the results are from + [`lowbnd`, `highbnd`), where `lowbnd` and `highbnd` are equal to + `np.iinfo(dtype).min` and `np.iinfo(dtype).max`. + + If only ``low`` is None, the results are from [`lowbnd`, `high`), + where `lowbnd` is defined as previously. If only `high` is None, + then if `low` > 0, results are from [0, `low`). This behaviour is + intended to maintain **backwards compatibility**. Otherwise, results + are from [`low`, `highbnd`), where `highbnd` is defined as previously. Parameters ---------- - low : int - Lowest (signed) integer to be drawn from the distribution (unless - ``high=None``, in which case this parameter is the *highest* such - integer). + low : int, optional + If provided, the lowest integer to be drawn from the + distribution (see above for behavior if ``low=None``). high : int, optional - If provided, one above the largest (signed) integer to be drawn - from the distribution (see above for behavior if ``high=None``). + If provided, one above the largest integer to be drawn from + the distribution (see above for behavior if ``high=None``). size : int or tuple of ints, optional Output shape. If the given shape is, e.g., ``(m, n, k)``, then ``m * n * k`` samples are drawn. Default is None, in which case a @@ -1205,27 +1213,80 @@ cdef class RandomState: Examples -------- - >>> np.random.randint(2, size=10) - array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) - >>> np.random.randint(1, size=10) - array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + Generate a random integer between 0 and 4, inclusive: - Generate a 2 x 4 array of ints between 0 and 4, inclusive: + >>> np.random.randint(low=1, high=5) + 1 - >>> np.random.randint(5, size=(2, 4)) - array([[4, 0, 2, 1], - [3, 2, 2, 0]]) + Generate a 2 x 4 array of ints between 1 and 4, inclusive: - """ - if high is None: - high = low - low = 0 + >>> np.random.randint(low=1, high=5, size=(2, 4)) + array([[3, 2, 1, 1], + [1, 2, 1, 4]]) + + Generate a 2 x 4 array of ints between 1 and 4, inclusive, + with 'dtype' equal to 'np.uint8': + + >>> np.random.randint(low=1, high=5, size=(2, 4), dtype=np.uint8) + array([[3, 2, 1, 1], + [1, 2, 1, 4]], dtype=uint8) + + Generate a 2 x 4 array of integers with 'dtype' equal to 'np.uint8': + + >>> np.random.randint(size=(2, 4), dtype=np.uint8) + array([[240, 81, 30, 113], + [160, 69, 60, 128]], dtype=uint8) + Generate a 2 x 4 array of integers with 'dtype' equal to 'np.uint8' + that are at most 9 inclusive: + + >>> np.random.randint(high=10, size=(2, 4), dtype=np.uint8) + array([[6, 8, 7, 4], + [7, 3, 4, 4]], dtype=uint8) + + Generate a 2 x 4 array of integers with 'dtype' equal to 'np.int8' + that are at least -10 inclusive: + + >>> np.random.randint(low=-10, size=(2, 4), dtype=np.int8) + array([[106, 89, -8, 35], + [ 89, 34, 105, 123]], dtype=int8) + + Generate a 2 x 4 array of integers with 'dtype' equal to 'np.int8' + that are between 0 and 4 inclusive: + + >>> np.random.randint(low=5, size=(2, 4), dtype=np.uint8) + array([[3, 0, -8, 35], + [3, 4, 105, 123]], dtype=int8) + + Generate a 2 x 4 array of integers with 'dtype' equal to 'np.int8' + that are at least 10 inclusive: + + >>> np.random.randint(low=10, high=np.iinfo(np.int8).max + 1, + size=(2, 4), dtype=np.int8) + array([[ 45, 54, 25, 106], + [ 30, 23, 109, 91]], dtype=int8) + + """ key = np.dtype(dtype).name if not key in _randint_type: - raise TypeError('Unsupported dtype "%s" for randint' % key) + raise TypeError(("Unsupported dtype '{dtype}' " + "for randint".format(dtype=key))) lowbnd, highbnd, randfunc = _randint_type[key] + if low is None: + low = lowbnd + high = (highbnd + if high is None + else high) + + elif high is None: + if low <= 0: + high = highbnd + + else: + high = low + low = 0 + if low < lowbnd: raise ValueError("low is out of bounds for %s" % (key,)) if high > highbnd: @@ -1732,17 +1793,17 @@ cdef class RandomState: """ if high is None: warnings.warn(("This function is deprecated. Please call " - "randint(1, {low} + 1) instead".format(low=low)), + "randint(low=1, high={low} + 1) instead".format(low=low)), DeprecationWarning) high = low low = 1 else: warnings.warn(("This function is deprecated. Please call " - "randint({low}, {high} + 1) instead".format( + "randint(low={low}, high={high} + 1) instead".format( low=low, high=high)), DeprecationWarning) - return self.randint(low, high + 1, size=size, dtype='l') + return self.randint(low=low, high=high + 1, size=size, dtype='l') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 19950936197d..d38a42669c1d 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -13,19 +13,19 @@ class TestSeed(TestCase): def test_scalar(self): s = np.random.RandomState(0) - assert_equal(s.randint(1000), 684) + assert_equal(s.randint(low=1000), 684) s = np.random.RandomState(4294967295) - assert_equal(s.randint(1000), 419) + assert_equal(s.randint(low=1000), 419) def test_array(self): s = np.random.RandomState(range(10)) - assert_equal(s.randint(1000), 468) + assert_equal(s.randint(low=1000), 468) s = np.random.RandomState(np.arange(10)) - assert_equal(s.randint(1000), 468) + assert_equal(s.randint(low=1000), 468) s = np.random.RandomState([0]) - assert_equal(s.randint(1000), 973) + assert_equal(s.randint(low=1000), 973) s = np.random.RandomState([4294967295]) - assert_equal(s.randint(1000), 265) + assert_equal(s.randint(low=1000), 265) def test_invalid_scalar(self): # seed must be an unsigned 32 bit integer @@ -64,8 +64,8 @@ def test_zero_probability(self): random.multinomial(100, [0.2, 0.8, 0.0, 0.0, 0.0]) def test_int_negative_interval(self): - assert_(-5 <= random.randint(-5, -1) < -1) - x = random.randint(-5, -1, 5) + assert_(-5 <= random.randint(low=-5, high=-1) < -1) + x = random.randint(low=-5, high=-1, size=5) assert_(np.all(-5 <= x)) assert_(np.all(x < -1)) @@ -147,31 +147,31 @@ def test_bounds_checking(self): for dt in self.itype: lbnd = 0 if dt is np.bool else np.iinfo(dt).min ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1 - assert_raises(ValueError, self.rfunc, lbnd - 1, ubnd, dtype=dt) - assert_raises(ValueError, self.rfunc, lbnd, ubnd + 1, dtype=dt) - assert_raises(ValueError, self.rfunc, ubnd, lbnd, dtype=dt) - assert_raises(ValueError, self.rfunc, 1, 0, dtype=dt) + assert_raises(ValueError, self.rfunc, low=lbnd - 1, high=ubnd, dtype=dt) + assert_raises(ValueError, self.rfunc, low=lbnd, high=ubnd + 1, dtype=dt) + assert_raises(ValueError, self.rfunc, low=ubnd, high=lbnd, dtype=dt) + assert_raises(ValueError, self.rfunc, low=1, high=0, dtype=dt) def test_rng_zero_and_extremes(self): for dt in self.itype: lbnd = 0 if dt is np.bool else np.iinfo(dt).min ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1 tgt = ubnd - 1 - assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) + assert_equal(self.rfunc(low=tgt, high=tgt + 1, size=1000, dtype=dt), tgt) tgt = lbnd - assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) + assert_equal(self.rfunc(low=tgt, high=tgt + 1, size=1000, dtype=dt), tgt) tgt = (lbnd + ubnd)//2 - assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) + assert_equal(self.rfunc(low=tgt, high=tgt + 1, size=1000, dtype=dt), tgt) def test_in_bounds_fuzz(self): # Don't use fixed seed np.random.seed() for dt in self.itype[1:]: for ubnd in [4, 8, 16]: - vals = self.rfunc(2, ubnd, size=2**16, dtype=dt) + vals = self.rfunc(low=2, high=ubnd, size=2**16, dtype=dt) assert_(vals.max() < ubnd) assert_(vals.min() >= 2) - vals = self.rfunc(0, 2, size=2**16, dtype=np.bool) + vals = self.rfunc(low=0, high=2, size=2**16, dtype=np.bool) assert_(vals.max() < 2) assert_(vals.min() >= 0) @@ -195,19 +195,51 @@ def test_repeatability(self): # view as little endian for hash if sys.byteorder == 'little': - val = self.rfunc(0, 6, size=1000, dtype=dt) + val = self.rfunc(low=0, high=6, size=1000, dtype=dt) else: - val = self.rfunc(0, 6, size=1000, dtype=dt).byteswap() + val = self.rfunc(low=0, high=6, size=1000, dtype=dt).byteswap() res = hashlib.md5(val.view(np.int8)).hexdigest() assert_(tgt[np.dtype(dt).name] == res) # bools do not depend on endianess np.random.seed(1234) - val = self.rfunc(0, 2, size=1000, dtype=np.bool).view(np.int8) + val = self.rfunc(low=0, high=2, size=1000, dtype=np.bool).view(np.int8) res = hashlib.md5(val).hexdigest() assert_(tgt[np.dtype(np.bool).name] == res) + def test_varying_bounds_args(self): + dt = np.int32 + res = -1324913873 + np.random.seed(1234) + assert_equal(self.rfunc(dtype=dt), res) + + res = -1324913873 + np.random.seed(1234) + assert_equal(self.rfunc(high=1000, dtype=dt), res) + + res = -1324913873 + np.random.seed(1234) + assert_equal(self.rfunc(high=-1000, dtype=dt), res) + + res = 822569774 + np.random.seed(1234) + assert_equal(self.rfunc(low=-1, dtype=dt), res) + + np.random.seed(1234) + assert_equal(self.rfunc(low=1, dtype=dt), 0) + + # Make sure that the order of the keyword arguments + # remains in the order 'low', 'high', 'size', and 'dtype' + # That way, people can continue to call this function + # without necessarily spelling out each keyword + def test_old_arg_invocation(self): + np.random.seed(1234) + expected = np.array([487191, 452283, 166158, 909341]) + + # low = 1000, high = 1000000, size = 4, dtype = np.int64 + assert_equal(self.rfunc(1000, 1000000, 4, np.int64), expected) + class TestRandomDist(TestCase): # Make sure the random distribution returns the correct value for a @@ -234,7 +266,7 @@ def test_randn(self): def test_randint(self): np.random.seed(self.seed) - actual = np.random.randint(-99, 99, size=(3, 2)) + actual = np.random.randint(low=-99, high=99, size=(3, 2)) desired = np.array([[31, 3], [-52, 41], [-48, -66]]) @@ -1450,13 +1482,13 @@ def test_two_arg_funcs(self): # low = np.array([0]) # # for dt in itype: -# out = func(low, high, dtype=dt) +# out = func(low=low, high=high, dtype=dt) # self.assert_equal(out.shape, self.tgtShape) # -# out = func(low[0], high, dtype=dt) +# out = func(low=low[0], high=high, dtype=dt) # self.assert_equal(out.shape, self.tgtShape) # -# out = func(low, high[0], dtype=dt) +# out = func(low=low, high=high[0], dtype=dt) # self.assert_equal(out.shape, self.tgtShape) def test_three_arg_funcs(self): diff --git a/numpy/random/tests/test_regression.py b/numpy/random/tests/test_regression.py index 133a1aa5ad6e..217a4239ba85 100644 --- a/numpy/random/tests/test_regression.py +++ b/numpy/random/tests/test_regression.py @@ -60,7 +60,7 @@ def test_randint_range(self): lmax = np.iinfo('l').max lmin = np.iinfo('l').min try: - random.randint(lmin, lmax) + random.randint(low=lmin, high=lmax) except: raise AssertionError