numpy.random.RandomState.random_integers#

方法

random.RandomState.random_integers(low, high=None, size=None)#

lowhigh(包含两者)之间生成 numpy.int_ 类型的随机整数。

在闭区间 [low, high] 中返回类型为 numpy.int_ 的服从“离散均匀”分布的随机整数。如果 high 为 None(默认值),则结果来自 [1, low]。numpy.int_ 类型转换为 C 语言的 long integer 类型,其精度取决于平台。

此函数已被弃用。请改用 randint。

自版本 1.11.0 起已弃用。

参数:
lowint

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

highint, 可选

如果提供,则为从分布中提取的最大(有符号)整数(有关 high=None 时的行为,请参见上文)。

sizeint 或 int 元组,可选

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

返回:
outint 或 int 的 ndarray

size 形状的随机整数数组,来自适当的分布,如果未提供 size,则为单个随机整数。

另请参阅

randint

random_integers 类似,但适用于半开区间 [low, high),如果省略 high,则最低值为 0。

备注

要从 a 和 b 之间 N 个等间距浮点数中采样,请使用

a + (b - a) * (np.random.random_integers(N) - 1) / (N - 1.)

示例

>>> np.random.random_integers(5)
4 # random
>>> type(np.random.random_integers(5))
<class 'numpy.int64'>
>>> np.random.random_integers(5, size=(3,2))
array([[5, 4], # random
       [3, 3],
       [4, 5]])

从 0 到 2.5 之间五个等间距数字(包含两者)的集合中选择五个随机数(即,从集合 \({0, 5/8, 10/8, 15/8, 20/8}\) 中选择)

>>> 2.5 * (np.random.random_integers(5, size=(5,)) - 1) / 4.
array([ 0.625,  1.25 ,  0.625,  0.625,  2.5  ]) # random

掷两个六面骰子 1000 次并对结果求和

>>> d1 = np.random.random_integers(1, 6, 1000)
>>> d2 = np.random.random_integers(1, 6, 1000)
>>> dsums = d1 + d2

将结果显示为直方图

>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(dsums, 11, density=True)
>>> plt.show()
../../../_images/numpy-random-RandomState-random_integers-1.png