溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Python中使用random模塊生成隨機數

發布時間:2021-03-17 17:07:45 來源:億速云 閱讀:276 作者:Leah 欄目:開發技術

怎么在Python中使用random模塊生成隨機數?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

導入random模塊。

import random

1、random.random() 隨機生成 0 到 1 之間的浮點數[0.0, 1.0) 。

print("random: ", random.random())
#random: 0.5714025946899135

2、random.randint(a , b) 隨機生成 a 與 b 之間的整數[a, b]。

print("randint: ", random.randint(6,8))
#randint: 8

3、random.randrange(start,stop,step)按步長step隨機在上下限范圍內取一個隨機數。

print("randrange: ",random.randrange(20,100,5))
#randrange: 85

4、random.uniform(a, b) 隨機生成 a 與 b 之間的浮點數[a, b]。

print("uniform: ",random.uniform(5,10))
#uniform: 5.119790163375776

5、random.choice() 從列表中隨機取出一個元素,比如列表、元祖、字符串等。注意的是,該方法需要參數非空,否則會拋出 IndexError 的錯誤。

print("choice: ",random.choice("www.yuanxiao.net"))
#choice: y

6、random.shuffle(items) 把列表 items 中的元素隨機打亂。注意的是,如果不想修改原來的列表,可以使用 copy 模塊先拷貝一份原來的列表。

num = [1, 2, 3, 4, 5]
random.shuffle(num)
print("shuffle: ",num)
#shuffle: [1, 3, 5, 4, 2]

7、random.sample(items, n) 從列表 items 中隨機取出 n 個元素。

num = [1, 2, 3, 4, 5]
print("sample: ",random.sample(num, 3))
#sample: [4, 1, 5]

Python 的random模塊產生的隨機數其實是偽隨機數,依賴于特殊算法和指定不確定因素(種子seed)來實現。如randint方法生成一定范圍內的隨機數,會先指定一個特定的seed,將seed通過特定的隨機數產生算法,得到一定范圍內隨機分布的隨機數。因此對于同一個seed值的輸入產生的隨機數會相同,省略參數則意味著使用當前系統時間秒數作為種子值,達到每次運行產生的隨機數都不一樣。

random.seed(2)
print("random: ", random.random())
#random: 0.9560342718892494

random.seed(3)
print("random: ", random.random())
#random: 0.23796462709189137

random.seed(3)#同一個種子值,產生的隨機數相同
print("random: ", random.random())
#random: 0.23796462709189137

numpy庫也提供了random模塊,用于生成多維度數組形式的隨機數。使用時需要導入numpy庫。

import numpy as np

下面介紹下numpy庫的random模塊的幾種生成隨機數的方法。

1、numpy.random.rand(d0,d1,…,dn)

  1. rand函數根據給定維度生成[0,1]之間的數據,包含0,不包含1

  2. dn表格每個維度

  3. 返回值為指定維度的array

print("np.random.rand:\n {}".format(np.random.rand(4,2))) # shape: 4*3
"""
np.random.rand:
 [[0.5488135 0.71518937]
 [0.60276338 0.54488318]
 [0.4236548 0.64589411]
 [0.43758721 0.891773 ]]
"""

print("np.random.rand:\n {}".format(np.random.rand(4,3,2))) # shape: 4*3*2
"""
np.random.rand:
 [[[0.96366276 0.38344152]
 [0.79172504 0.52889492]
 [0.56804456 0.92559664]]

 [[0.07103606 0.0871293 ]
 [0.0202184 0.83261985]
 [0.77815675 0.87001215]]

 [[0.97861834 0.79915856]
 [0.46147936 0.78052918]
 [0.11827443 0.63992102]]

 [[0.14335329 0.94466892]
 [0.52184832 0.41466194]
 [0.26455561 0.77423369]]]
"""

 2、numpy.random.randn(d0,d1,…,dn)

  1. randn函數返回一個或一組樣本,具有標準正態分布。

  2. dn表格每個維度

  3. 返回值為指定維度的array

  4. 標準正態分布—-standard normal distribution

  5. 標準正態分布又稱為u分布,是以0為均值、以1為標準差的正態分布,記為N(0,1)。

print("np.random.randn:\n {}".format(np.random.randn())) # 當沒有參數時,返回單個數據
"""
np.random.randn:
 2.2697546239876076
"""
print("np.random.randn:\n {}".format(np.random.randn(2,4)))
"""
np.random.randn:
 [[-1.45436567 0.04575852 -0.18718385 1.53277921]
 [ 1.46935877 0.15494743 0.37816252 -0.88778575]]
"""
print("np.random.randn:\n {}".format(np.random.randn(4,3,2)))
"""
np.random.randn:
 [[[-1.98079647 -0.34791215]
 [ 0.15634897 1.23029068]
 [ 1.20237985 -0.38732682]]

 [[-0.30230275 -1.04855297]
 [-1.42001794 -1.70627019]
 [ 1.9507754 -0.50965218]]

 [[-0.4380743 -1.25279536]
 [ 0.77749036 -1.61389785]
 [-0.21274028 -0.89546656]]

 [[ 0.3869025 -0.51080514]
 [-1.18063218 -0.02818223]
 [ 0.42833187 0.06651722]]]
"""

3、numpy.random.randint(low, high=None, size=None, dtype='l')

  1. 返回隨機整數,范圍區間為[low,high),包含low,不包含high

  2. 參數:low為最小值,high為最大值,size為數組維度大小,dtype為數據類型,默認的數據類型是np.int

  3. high沒有填寫時,默認生成隨機數的范圍是[0,low]

print("np.random.randint:\n {}".format(np.random.randint(1,size=5)))# 返回[0,1)之間的整數,所以只有0
"""
np.random.randint:
 [0 0 0 0 0]
"""
print("np.random.randint:\n {}".format(np.random.randint(1,5)))# 返回1個[1,5)時間的隨機整數
"""
np.random.randint:
 2
"""
print("np.random.randint:\n {}".format(np.random.randint(-5,5,size=(2,2))))
"""
np.random.randint:
 [[-5 -3]
 [ 2 -3]]
"""

4、numpy.random.seed()

  1. np.random.seed()的作用:使得隨機數據可預測。

  2. 當我們設置相同的seed,每次生成的隨機數相同。如果不設置seed,則每次會生成不同的隨機數

關于怎么在Python中使用random模塊生成隨機數問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女