numpy.random.RandomState.randint

方法

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

从返回随机整数 low (包括) high (排他性)。

从“半开”间隔中指定数据类型的“离散均匀”分布返回随机整数[“低”, high )如果 high 为“无”(默认),则结果来自[0, low

注解

新代码应该使用 integers A方法 default_rng() 请参阅 快速启动 .

参数
lowint或类似数组的int

从分布中提取的最小(有符号)整数(除非 high=None ,在这种情况下,此参数是 最高 这样的整数)。

highint或类似数组的int,可选

如果提供,将从分布中提取的最大(有符号)整数上方的一个(请参见上面的行为,如果 high=None ). 如果类似于数组,则必须包含整数值

sizeint或int的元组,可选

输出形状。如果给定的形状是,例如, (m, n, k) 然后 m * n * k 取样。默认值为无,在这种情况下返回单个值。

dtype可选类型

结果的所需数据类型。字节顺序必须是本机的。默认值为int。

1.11.0 新版功能.

返回
out整数或整数数组

size -来自适当分布的随机整数的整形数组,或单个这样的随机整数,如果 size 未提供。

参见

random_integers

类似 randint ,仅限于闭合间隔 [low, high] ,如果 high 被省略。

Generator.integers

应该用于新代码。

实例

>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random
>>> np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

生成介于0和4之间的2 x 4整数数组,包括:

>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1], # random
       [3, 2, 2, 0]])

生成具有3个不同上界的1x3数组

>>> np.random.randint(1, [3, 5, 10])
array([2, 2, 9]) # random

生成具有3个不同下界的1×3数组

>>> np.random.randint([1, 5, 7], 10)
array([9, 8, 7]) # random

使用数据类型为uint8的广播生成2×4数组

>>> np.random.randint([1, 3, 5, 7], [[10], [20]], dtype=np.uint8)
array([[ 8,  6,  9,  7], # random
       [ 1, 16,  9, 12]], dtype=uint8)