numpy.random.RandomState.random_integers#

方法

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

类型为 numpy.int_ 的随机整数,范围在 lowhigh 之间(包括两者)。

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

此函数已被弃用。请使用 randint 代替。

自版本 1.11.0 起已弃用。

参数:
lowint

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

highint, optional

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

sizeint 或 int 的元组,可选

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

返回:
outint or ndarray of ints

来自适当分布的随机整数的 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