maxframe.tensor.random.randint#
- maxframe.tensor.random.randint(low, high=None, size=None, dtype='l', density=None, chunk_size=None, gpu=None)[源代码]#
从 `low`(包含)到 `high`(不包含)返回随机整数。
从指定 dtype 的“离散均匀”分布中,在“半开”区间 [low, high) 内返回随机整数。如果 high 为 None(默认值),则结果来自 [0, low)。
- 参数:
low (int) -- 从分布中抽取的最小(有符号)整数(除非
high=None,在这种情况下该参数是 最大 整数加一)。high (int, optional) -- 如果提供该参数,则表示从分布中抽取的最大(有符号)整数加一(如果
high=None,请参见上文描述)。size (int or tuple of ints, optional) -- 输出形状。如果给定形状例如
(m, n, k),则抽取m * n * k个样本。默认为 None,此时返回单个值。dtype (data-type, optional) -- 结果的期望数据类型。所有数据类型都由其名称决定,例如 'int64'、'int' 等,因此不支持字节顺序,且特定精度在不同平台上可能有不同的 C 类型。默认值为 'np.int'。
density (float, optional) -- 如果指定了 density,将创建一个稀疏张量
chunk_size (int or tuple of int or tuple of ints, optional) -- 每个维度上期望的块大小
gpu (bool, optional) -- 如果为 True,则在 GPU 上分配张量,默认为 False
dtype -- 返回张量的数据类型。
- 返回:
out -- 来自适当分布的 size 形状的随机整数张量,如果未提供 size,则返回单个这样的随机整数。
- 返回类型:
int or Tensor of ints
参见
random.random_integers与 randint 类似,但用于闭区间 [low, high],如果省略 high,则 1 是最小值。特别是,这个函数用于生成均匀分布的离散非整数。
示例
>>> import maxframe.tensor as mt
>>> mt.random.randint(2, size=10).execute() array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) >>> mt.random.randint(1, size=10).execute() array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
生成一个 2 x 4 的整数张量,数值在 0 到 4 之间(包含 0 和 4):
>>> mt.random.randint(5, size=(2, 4)).execute() array([[4, 0, 2, 1], [3, 2, 2, 0]])