numpy.random.RandomState.random_integers#
method
- random.RandomState.random_integers(low, high=None, size=None)#
numpy.int_类型的随机整数,包含 low 和 high ,包括边界值.从闭区间 [ low , high ] 中的“离散均匀”分布返回
numpy.int_类型的随机整数. 如果 high 为 None(默认值),则结果来自 [1, low ].numpy.int_类型转换为 C 语言的长整数类型,其精度与平台有关.此函数已弃用.请改用 randint.
自 1.11.0 版本弃用.
- 参数:
- lowint
要从分布中提取的最低(有符号)整数(除非
high=None,在这种情况下,此参数是最高的此类整数).- high整数,可选
如果提供,则为要从分布中提取的最大的(有符号)整数(有关
high=None时的行为,请参见上文).- sizeint 或 int 的元组,可选.
输出形状.如果给定的形状是,例如
(m, n, k),则抽取m * n * k个样本.默认值为 None,在这种情况下,将返回单个值.
- 返回:
- outint 或 ints 的 ndarray
来自适当分布的 size 形状的随机整数数组,如果未提供 size ,则为单个此类随机 int.
参见
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 和 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()