numpy.random.randint的详细用法 - python

半兽人 发表于: 2019-04-17   最后更新时间: 2023-05-16 20:17:18  
{{totalSubscript}} 订阅, 13,425 游览

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

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

参数如下:

参数 描述
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等等

输出:

返回一个随机数或随机数数组

例子

>>> 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]])
更新于 2023-05-16
在线,1小时前登录

查看TensorFlow更多相关的文章或提一个关于TensorFlow的问题,也可以与我们一起分享文章