numpy.random 模塊- 隨機數

numpy中有一些經常使用的用來產生隨機數的函數。
1. rand(d0, d1, …, dn)的隨機樣本位於[0, 1)中。 python

>>> np.random.rand(2,2)
array([[ 0.03707949,  0.41972219],
       [ 0.76721763,  0.92190316]])

2. randn(d0, d1, …, dn)是從標準正態分佈中返回一個或多個樣本值。dom

>>> np.random.randn()
-0.23973479740743608

     # N(3,6.25)     =>    2.5 * np.random.randn(2,4)+3 函數

>>> 2.5* np.random.randn(2,4)+3
array([[ 0.01273735,  1.2869618 ,  2.46575794,  3.00575844],
       [ 2.0903537 ,  3.45027914,  1.81021444,  2.23118217]])

3. randint(low[, high, size])  得到隨機的整數 位於半開區間[low, high)code

>>> np.random.randint(3, size = 10)
array([1, 2, 1, 2, 2, 1, 1, 1, 0, 0])
>>> np.random.randint(1,5,size =10)
array([3, 1, 3, 3, 3, 4, 1, 2, 4, 3])
>>> np.random.randint(1,10,size=(2,3))
array([[6, 9, 1],
       [6, 4, 7]])

4. random_integers(low[,high, size])  得到隨機的整數,位於閉區間[low, high]class

>>> np.random.random_integers(5)
1
>>> np.random.random_integers(5, size=(3,2))
array([[2, 1],
       [5, 2],
       [4, 4]])
>>> 2 * (np.random.random_integers(5, size=(5,)) - 1) / 4
array([ 0.5,  0.5,  1. ,  0.5,  0.5])

5. random_sample([size]) 獲取隨機的浮點數, 在半開區間[0.0, 1.0)隨機數

>>> np.random.random_sample()
0.64083390603798
>>> np.random.random_sample((5))
array([ 0.08202041,  0.62794823,  0.63852735,  0.17191555,  0.56619236])
>>> np.random.random_sample((3,4))
array([[ 0.96598298,  0.05680509,  0.75908237,  0.55356011],
       [ 0.20096591,  0.67791321,  0.52313978,  0.09776218],
       [ 0.01183812,  0.93489007,  0.61070159,  0.10541408]])

6. random([size])  獲取隨機的浮點數,在半開區間[0.0, 1.0)numpy

7. ranf([size])  獲取隨機的浮點數,在半開區間[0.0, 1.0)di

8. sample([size])  獲取隨機的浮點數,在半開區間[0.0, 1.0)co

9. bytes(length) 獲取隨機字節360

>>> np.random.bytes(10)
b'\x99\n\xc5\x95\xe7xG\xea~
相關文章
相關標籤/搜索