numpy.random.RandomState.random_integers¶
方法
-
RandomState.
random_integers
(low, high=None, size=None)¶ np.int类型的随机整数,介于 low 和 high 包括在内。
从封闭区间的“离散均匀”分布中返回np.int类型的随机整数 [low, high] . 如果 high 为“无”(默认),则结果来自 [1, low] . int类型转换为python 2用于“短”整数的c long类型,其精度取决于平台。
此函数已被弃用。用兰丁代替。
1.11.0 版后已移除.
参数: - low : 利息
从分布中提取的最小(有符号)整数(除非
high=None
,在这种情况下,此参数是 最高 这样的整数)。- high : 可选的
如果提供,则从分布中提取的最大(有符号)整数(请参阅上面的行为if
high=None
)- size : int或int的元组,可选
输出形状。如果给定的形状是,例如,
(m, n, k)
然后m * n * k
取样。默认值为无,在这种情况下返回单个值。
返回: - out : 整数或整数数组
size -来自适当分布的随机整数的整形数组,或单个这样的随机整数,如果 size 未提供。
参见
randint
- 类似
random_integers
,仅适用于半开间隔[low, high ,如果 high 被省略。
笔记
要从A和B之间的N个等间距浮点数采样,请使用:
a + (b - a) * (np.random.random_integers(N) - 1) / (N - 1.)
实例
>>> np.random.random_integers(5) 4 >>> type(np.random.random_integers(5)) <type 'int'> >>> np.random.random_integers(5, size=(3,2)) array([[5, 4], [3, 3], [4, 5]])
从0到2.5之间(含0和2.5)的五个等距数字集中选择五个随机数( i.e. ,从集合 {{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 ])
掷两个六边形骰子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()