1. 程式人生 > >numpy.random.randint用法

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