numpy.random.randint(low, high=None, size=None, dtype='l')
函数的作用是,返回一个随机整型数,范围从低(包括)到高(不包括),即[low, high)。
如果没有写参数high的值,则返回[0,low)的值。
参数如下:
- low: int
生成的数值最低要大于等于low。
(hign = None时,生成的数值要在[0, low)区间内) - high: int (可选)
如果使用这个值,则生成的数值在[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]])
>>>np.random.randint(2, high=10, size=(2,3))
array([[6, 8, 7],
[2, 5, 2]])

本文详细介绍了numpy库中random.randint函数的用法,该函数用于生成指定范围内的随机整数。文章解释了函数参数的意义,包括low、high、size和dtype,并通过实例展示了如何使用此函数生成不同维度和类型的随机数。
2654

被折叠的 条评论
为什么被折叠?



