numpy.random.RandomState.random_integers

方法

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

类型的随机整数 np.int_ 之间 lowhigh 包括在内。

返回类型为的随机整数 np.int_ 从封闭区间的“离散均匀”分布 [low, high] . 如果 high 为“无”(默认),则结果来自 [1, low] . 这个 np.int_ 类型转换为C长整数类型,其精度取决于平台。

此函数已被弃用。用兰丁代替。

1.11.0 版后已移除.

参数
low利息

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

high可选的

如果提供,则从分布中提取的最大(有符号)整数(请参阅上面的行为if high=None

sizeint或int的元组,可选

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

返回
out整数或整数数组

size -来自适当分布的随机整数的整形数组,或单个这样的随机整数,如果 size 未提供。

参见

randint

类似 random_integers ,仅适用于半开间隔[lowhigh ,如果 high 被省略。

笔记

要从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)的五个等距数字集中选择五个随机数( 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  ]) # 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