本文简要介绍 python 语言中 numpy.random.Generator.integers
的用法。
用法:
random.Generator.integers(low, high=None, size=None, dtype=np.int64, endpoint=False)
返回随机整数低的(含)至高的(独占),或者如果端点=True,低的(含)至高的(包括的)。取代RandomState.randint(端点=False)和随机状态.random_integers(端点=True)
从指定数据类型的 “discrete uniform” 分布中返回随机整数。如果高的为 None(默认值),则结果从 0 到低的.
- low: int 或类似整数的数组
要从分布中提取的最低(有符号)整数(除非
high=None
,在这种情况下该参数为 0 并且该值用于高的)。- high: int 或类似整数的数组,可选
如果提供,则为从分布中提取的最大(有符号)整数之上的一个(请参阅上面
high=None
的行为)。如果是类似数组,则必须包含整数值- size: int 或整数元组,可选
输出形状。例如,如果给定的形状是
(m, n, k)
,则绘制m * n * k
样本。默认为无,在这种情况下返回单个值。- dtype: dtype,可选
结果的所需数据类型。字节序必须是原生的。默认值为 np.int64。
- endpoint: 布尔型,可选
如果为真,则从区间 [low, high] 中采样,而不是默认的 [low, high) 默认为 False
- out: int 或整数数组
size
- 形状的随机整数数组,来自适当的分布,如果未提供size
,则为单个此类随机 int。
参数:
返回:
注意:
当使用具有 uint64 dtypes 的广播时,最大值 (2**64) 不能表示为标准整数类型。 high 数组(如果 high 为 None,则为 low)必须具有对象 dtype,例如,array([2**64])。
参考:
Daniel Lemire.,“区间内的快速随机整数生成”,ACM Transactions on Modeling and Computer Simulation 29 (1),2019,http://arxiv.org/abs/1805.10941。
1:
例子:
>>> rng = np.random.default_rng() >>> rng.integers(2, size=10) array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random >>> rng.integers(1, size=10) array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
生成 0 到 4 之间的 2 x 4 整数数组,包括:
>>> rng.integers(5, size=(2, 4)) array([[4, 0, 2, 1], [3, 2, 2, 0]]) # random
生成具有 3 个不同上限的 1 x 3 数组
>>> rng.integers(1, [3, 5, 10]) array([2, 2, 9]) # random
生成具有 3 个不同下限的 1 x 3 数组
>>> rng.integers([1, 5, 7], 10) array([9, 8, 7]) # random
使用 dtype 为 uint8 的广播生成 2 x 4 数组
>>> rng.integers([1, 3, 5, 7], [[10], [20]], dtype=np.uint8) array([[ 8, 6, 9, 7], [ 1, 16, 9, 12]], dtype=uint8) # random
相关用法
- Python numpy Generator.multivariate_normal用法及代码示例
- Python numpy Generator.standard_normal用法及代码示例
- Python numpy Generator.bytes用法及代码示例
- Python numpy Generator.shuffle用法及代码示例
- Python numpy Generator.choice用法及代码示例
- Python numpy Generator.random用法及代码示例
- Python numpy Generator.logseries用法及代码示例
- Python numpy Generator.uniform用法及代码示例
- Python numpy Generator.standard_t用法及代码示例
- Python numpy Generator.standard_cauchy用法及代码示例
- Python numpy Generator.normal用法及代码示例
- Python numpy Generator.poisson用法及代码示例
- Python numpy Generator.power用法及代码示例
- Python numpy Generator.geometric用法及代码示例
- Python numpy Generator.laplace用法及代码示例
- Python numpy Generator.vonmises用法及代码示例
- Python numpy Generator.noncentral_f用法及代码示例
- Python numpy Generator.gamma用法及代码示例
- Python numpy Generator.multivariate_hypergeometric用法及代码示例
- Python numpy Generator.weibull用法及代码示例
- Python numpy Generator.gumbel用法及代码示例
- Python numpy Generator.pareto用法及代码示例
- Python numpy Generator.noncentral_chisquare用法及代码示例
- Python numpy Generator.negative_binomial用法及代码示例
- Python numpy Generator.binomial用法及代码示例
注:本文由纯净天空筛选整理自numpy.org大神的英文原创作品 numpy.random.Generator.integers。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。