numpy.random.RandomState.random_integers#

方法

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

介於 lowhigh (包含) 之間的 numpy.int_ 類型的隨機整數。

從封閉區間 [low, high] 中的「離散均勻」分佈傳回 numpy.int_ 類型的隨機整數。如果 high 為 None (預設值),則結果來自 [1, low]。 numpy.int_ 類型轉換為 C long integer 類型,其精度取決於平台。

此函數已棄用。請改用 randint。

自版本 1.11.0 起已棄用。

參數:
lowint

要從分佈中抽取的最低 (signed) 整數 (除非 high=None,在這種情況下,此參數是最高的整數)。

highint, optional

如果提供,則為要從分佈中抽取的最大 (signed) 整數 (如果 high=None,請參閱上述行為)。

sizeint 或 int 元組, optional

輸出形狀。如果給定的形狀為,例如 (m, n, k),則會抽取 m * n * k 個樣本。預設值為 None,在這種情況下,會傳回單一值。

傳回值:
outint 或 int 的 ndarray

來自適當分佈的 size 形狀的隨機整數陣列,如果未提供 size,則為單一此類隨機整數。

另請參閱

randint

random_integers 類似,僅適用於半開區間 [low, high),如果省略 high,則 0 為最小值。

註解

若要從 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 之間五個均勻間隔的數字集合中選擇五個隨機數,包含 2.5 (,從集合 \({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