本文簡要介紹 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。