numpy.random.randint用法

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(可选):
    想要输出的格式。如int64int等等

输出:

  • 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]])

猜你喜欢

转载自blog.csdn.net/u011851421/article/details/83544853