Random.Randint 函数--Numpy

    科技2022-07-13  131

    Numpy.Random.Randint

    函数功能: Return random integers from low (inclusive) to high (exclusive)。 Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low)。 返回最小值(包含)与最大值(不包含)之间的随机整数。 具体是:返回半开区间(前闭后开)上指定数据类型的服从离散均匀分布的随机整数。若最大值为空,默认返回[0,low)之间服从均匀分布的随机数

    函数语法:

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

    函数参数: low: int or array-like of ints。Lowest (signed) integers to be drawn from the distribution (unless high=None, in which case this parameter is one above the highest such integer). 最小值:整数或整数形式的数组。分布中的最小整数值(含正负号。若high为空则,该值比分布中产生的最大值大1。

    high: int or array-like of ints, optional。If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None). If array-like, must contain integer values **最大值:**可选参数,整数或整数形式的数组。若函数中给定,则比分布函数中产生的最大值大1。若为整数形式的数组,则必须包含整数值。(不理解)

    size: int or tuple of ints, optional。Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned. **大小:**可选参数,整数或者整数元组,若以(m,n,k)的形式给定大小则从分布函数中产生 m ∗ n ∗ k m*n*k mnk个样本值。默认为空,此时返回单个样本值。

    dtype: dtype, optional。Desired dtype of the result. Byteorder must be native. The default value is int. 数据类型: 可选参数,所需要的结果数据类型,默认整数型。

    当只有一个参数low传入,无最大值high,无数据大小size,默认返回[0,low)之间的随机整数,每次运行结果不同 指定返回数据大小 产生指定大小的数组 m ∗ n m*n mn 传入数组,只传入最小值low的数组,则默认产生相同维度的0到相应数组值(不包含)之间的随机整数 传入参数最小值low为负值时,必须同时输入最大值,因为缺失最大值默认产生[0,输入参数)之间的随机整数,而参数为负值小于0,会报错。

    import numpy as np # 只有一个low参数传入,无最大值high,无数据大小size,默认返回[0,low)之间的随机整数 a = np.random.randint(9) print(a) # 指定返回数据大小,整数 b = np.random.randint(9, size=2) print(b) # 指定返回数据大小,数组 c = np.random.randint(9, size=(2, 2)) print(c) # 传入参数low为数组 d = np.random.randint([2,5,3], size=[10, 3]) print(d) # 传入参数为负数 e = np.random.randint(-5, 0, size=[10, 3]) print(e)

    参考文献: [1] https://numpy.org/doc/stable/reference/random/generated/numpy.random.randint.html

    Processed: 0.011, SQL: 8