maxframe.tensor.random.random_integers#
- maxframe.tensor.random.random_integers(low, high=None, size=None, chunk_size=None, gpu=None)[源代码]#
在 low 和 high 之间(包括两者)生成类型为 mt.int 的随机整数。
从闭区间 [low, high] 内的“离散均匀”分布中返回类型为 mt.int 的随机整数。如果 high 为 None(默认值),则结果来自 [1, low]。np.int 类型会转换为 Python 2 中用于“短整数”的 C long 类型,其精度取决于平台。
此函数已被弃用。请改用 randint。
- 参数:
low (int) -- 从分布中抽取的最小(有符号)整数(除非
high=None,在这种情况下此参数是最大的此类整数)。high (int, optional) -- 如果提供此参数,则是从分布中抽取的最大(有符号)整数(如果
high=None,请参见上文的行为说明)。size (int or tuple of ints, optional) -- 输出形状。如果给定的形状是例如
(m, n, k),则抽取m * n * k个样本。默认值为 None,这种情况下返回单个值。chunk_size (int or tuple of int or tuple of ints, optional) -- 每个维度上期望的块大小
gpu (bool, optional) -- 如果为 True,则在 GPU 上分配张量,默认为 False
- 返回:
out -- 来自适当分布的 size 形状的随机整数数组,如果未提供 size,则返回单个这样的随机整数。
- 返回类型:
int or Tensor of ints
参见
random.randint与 random_integers 类似,但用于半开区间 [low, high),如果省略 high,则 0 是最小值。
备注
要从 a 和 b 之间的 N 个等间距浮点数中采样,请使用:
a + (b - a) * (np.random.random_integers(N) - 1) / (N - 1.)
示例
>>> import maxframe.tensor as mt
>>> mt.random.random_integers(5).execute() 4 >>> type(mt.random.random_integers(5).execute()) <type 'int'> >>> mt.random.random_integers(5, size=(3,2)).execute() array([[5, 4], [3, 3], [4, 5]])
从 0 到 2.5 之间(包括两者)五个等间距数字的集合中选择五个随机数(即从集合 \({0, 5/8, 10/8, 15/8, 20/8}\) 中选择):
>>> (2.5 * (mt.random.random_integers(5, size=(5,)) - 1) / 4.).execute() array([ 0.625, 1.25 , 0.625, 0.625, 2.5 ])
掷两次六面骰子共 1000 次,并将结果相加:
>>> d1 = mt.random.random_integers(1, 6, 1000) >>> d2 = mt.random.random_integers(1, 6, 1000) >>> dsums = d1 + d2
将结果以直方图形式显示:
>>> import matplotlib.pyplot as plt >>> count, bins, ignored = plt.hist(dsums.execute(), 11, normed=True) >>> plt.show()