第一步,先来看看两者的help信息是如何定义:
help(np.random.randint):
randint(low, high=None, size=None, dtype=’l’) (return a range of intergers with low or high bounds)
Return random integers from low
(inclusive) to high
(exclusive). the specified dtype in the “half-open” interval [low
, high
). If
high
is None (the default), then results are from [0, low
).
注:range范围前闭后开 [low
, high
) 或 [0, low
)
help(random.randint)
Help on method randint in module random:
randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.(return an integer)
注:range范围 前闭后闭 [a, b]
help(np.random.randint)
Help on built-in function randint:
randint(...) method of mtrand.RandomState instance
randint(low, 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`).
Parameters
----------
low : int
Lowest (signed) integer to be drawn from the distribution (unless
``high=None``, in which case this parameter is one above the
*highest* such integer).
high : int, optional
If provided, one above the largest (signed) 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
single value is returned.
dtype : dtype, optional
Desired dtype of the result. All dtypes are determined by their
name, i.e., 'int64', 'int', etc, so byteorder is not available
and a specific precision may have different C types depending
on the platform. The default value is 'np.int'.
.. versionadded:: 1.11.0
Returns
-------
out : int or ndarray of ints
`size`-shaped array of random integers from the appropriate
distribution, or a single such random int if `size` not provided.
See Also
--------
random.random_integers : similar to `randint`, only for the closed
interval [`low`, `high`], and 1 is the lowest value if `high` is
omitted. In particular, this other one is the one to use to generate
uniformly distributed discrete non-integers.
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 2 x 4 array of ints between 0 and 4, inclusive:
>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
>>> help(random.randint)
Help on method randint in module random:
randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.
第二步 举例说明:
>>> random.randint(1,20)
13
>>> np.random.randint(1,20)
1
>>> np.random.randint(1,20,size=3)
array([19, 18, 4])
>>> np.random.randint(1,20,size=(3,4))
array([[ 4, 8, 7, 13],
[ 8, 19, 16, 1],
[ 4, 4, 2, 18]])
>>> np.random.randint(20,size=5)
array([ 4, 19, 17, 17, 8])
>>> np.random.randint(1,size=5)
array([0, 0, 0, 0, 0])
>>> np.random.randint(2,size=5)
array([0, 0, 0, 1, 0])
说明:
np.random.randint() 作为numpy库的一种方法,参数更丰富,相比于ranom.randint() 具备size参数 可指定outpud的元素的shape属性