numpy.random.RandomState.random_integers#
method
- random.RandomState.random_integers(low, high=None, size=None)#
low 和 high 之间的
numpy.int_类型的随机整数,包括 low 和 high .从闭区间
[low , ` high] `` 中的"离散均匀"分布返回 `` numpy.int_ ` 类型的随机整数.如果 ` high` 为 None (默认值),则结果来自 ``[1, `` low`] ``. `` numpy.int_` 类型转换为 C 长整数类型,其精度取决于平台.此函数已弃用.请改用 randint.
自 1.11.0 版本弃用.
- 参数:
- lowint
从分布中抽取的最小(有符号)整数(除非
high=None,在这种情况下,此参数是最大整数).- highint, optional
如果提供,则从分布中抽取的最大[带符号]整数(如果
high=None,请参见上面的行为).- sizeint 或 int 元组,可选
输出形状. 如果给定形状,例如
(m, n, k),则抽取m * n * k个样本. 默认为 None,在这种情况下,返回单个值.
- 返回:
- outint 或整数的 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]])
Choose five random numbers from the set of five evenly-spaced numbers between 0 and 2.5, inclusive (i.e., from the set \({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()