numpy.random.randint的用法

numpy.random.randint(low, high=None, size=None, dtype='l')

函数的作用是,返回一个随机整型数,范围从低(包括)到高(不包括),即[low, high),左闭右开。
如果参数high的值是None,则返回[0,low)的值。

参数如下:

low: int
生成的数值最低要大于等于low。
(hign = None时,生成的数值要在[0, low)区间内)
high: int (可选)
如果这个值不为None,则生成的数值在[low, high)区间。
size: int or tuple of ints(可选)
输出随机数的尺寸,比如size = (m * n* k),则输出同规模即m * n* k个随机数。默认是None,仅仅返回满足要求的单一随机数。
dtype: dtype(可选):
想要输出的格式。如int64、int等等
out: int or ndarray of ints
返回一个随机数或随机数数组

例子:

>>> 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])
>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
       [3, 2, 2, 0]])
原创文章 46 获赞 49 访问量 2183

猜你喜欢

转载自blog.csdn.net/qq_41660119/article/details/105836597