Python 中random 庫的詳細(xì)使用
Random庫主要包含返回隨機(jī)數(shù)的函數(shù),主要用于普通的隨機(jī)數(shù)生成的程序,如果對隨機(jī)性有特殊要求,比如加密等,可以用函數(shù)os.urandom()或者random模塊內(nèi)部的SystemRandom類,這些可以讓數(shù)據(jù)接近真正的隨機(jī)性。
前言
- 為啥突然寫這個(gè)?因?yàn)橛玫骄蛯憜h,感覺對生成數(shù)據(jù)很有用,之前都是百度別人的,今天來對著官方文檔寫,超級標(biāo)準(zhǔn)!
- 這邊只講常用的,看了下文檔還有什么數(shù)學(xué)方法,太高級好像用不上
返回整數(shù)
random.randrange語法格式
兩種寫法
random.randrange(stop) random.randrange(start, stop[, step])
- start:起始數(shù)字,包含(取得到 start 這個(gè)值)
- stop:末尾數(shù)字,不包含(取不到 stop 這個(gè)值)
- step:步長
實(shí)際栗子
# 栗子一 for i in range(5): print(random.randrange(20)) #### 17 4 7 7 4 # 栗子二 for i in range(5): print(random.randrange(10, 20)) #### 13 14 11 17 17 # 栗子三 for i in range(5): print(random.randrange(10, 20, 2)) #### 12 12 14 14 10
random.randint
語法格式
- 返回隨機(jī)整數(shù)N滿足
a<=N<=b
- 相當(dāng)于
randrange(a,b+1)
random.randint(a, b)
實(shí)際栗子
for i in range(5): print(random.randint(0,20)) #### 19 20 11 6 3
a、b 都可以取得到哦
返回浮點(diǎn)數(shù)
random.random()語法格式
返回 [0.0, 1.0) 范圍內(nèi)的下一個(gè)隨機(jī)浮點(diǎn)數(shù)
random.random()
實(shí)際栗子
# 栗子一 for i in range(5): print(random.random()) #### 0.9829492243165335 0.43473506430105724 0.5198709187243076 0.6437884305820736 0.7216771961168909 # 栗子二 for i in range(5): print(math.ceil(random.random() * 1000)) #### 772 352 321 62 127
random.uniform(a, b)
語法格式
- 返回一個(gè)隨機(jī)浮點(diǎn)數(shù)N
- 當(dāng)
a<=b
時(shí),a<=N<=b
- 當(dāng)
b<a
時(shí),b<=N<=a
random.uniform(a, b)
實(shí)際栗子
# 栗子一 for i in range(5): print(random.uniform(1, 10)) #### 2.6200262089754593 9.220506911469235 3.0206896704014783 9.670905330339174 1.170694187192196 # 栗子二 for i in range(5): print(random.uniform(8, 2)) #### 2.696842757954265 6.058794935110275 7.567631220015144 2.2057698202258074 4.454083664106361
傳遞列表作為參數(shù)
random.choice
語法格式
- 從非空序列 seq 返回一個(gè)隨機(jī)元素
- 如果 seq 為空,會(huì)拋出 IndexError
random.choice(seq)
實(shí)際栗子
# 數(shù)字?jǐn)?shù)組 print(random.choice([1, 2, 3, 4, 5])) # 字母數(shù)組 print(random.choice(["a", "b", "c"])) # 字母元組 print(random.choice(("a", "b", "c"))) # 字符串 print(random.choice("abcdef")) # string 模塊返回的大小寫字母字符串 print(random.choice(string.ascii_letters)) # string 模塊返回的數(shù)字字符串 print(random.choice(string.digits)) # string 模塊返回的數(shù)字字符串+大小寫字母字符串 print(random.choice(string.digits + string.ascii_uppercase)) #### 5 c c e l 2 F
random.choices
語法格式
- populaiton:序列
- weights:普通權(quán)重
- cum_weights:累加權(quán)重
- k:選擇次數(shù)
- weights 和 cum_weights 不能同時(shí)傳,只能選擇一個(gè)來傳
random.choices(population, weights=None, *, cum_weights=None, k=1)
看的迷迷糊糊啥意思。。?來看栗子。。
不帶參數(shù)的栗子
a = [1,2,3,4,5] print(random.choices(a,k=5)) # 結(jié)果 [5, 5, 3, 1, 5]
可以重復(fù)取元素
帶 weight 的栗子一
a = [1, 2, 3, 4, 5] print(random.choices(a, weights=[0, 0, 1, 0, 0], k=5)) # 結(jié)果 [3,3,3,3,3]
- 序列有多長,weights 對應(yīng)的序列就得多長,每個(gè)位置都是一一對應(yīng)
- 像這里,3 的權(quán)重是 1,其他是 0 ,所以每次都取 3,因?yàn)樗臋?quán)重最高,其他元素沒有權(quán)重
帶 weight 的栗子二
a = [1, 2, 3, 4, 5] print(random.choices(a, weights=[0, 2, 1, 0, 0], k=5)) # 結(jié)果 [2, 2, 2, 2, 3]
2 的權(quán)重更大,所以取到它的概率更高
帶 cum_weights 的栗子
a = [1, 2, 3, 4, 5] print(random.choices(a, cum_weights=[1, 1, 1, 1, 1], k=5)) print(random.choices(a, cum_weights=[1, 4, 4, 4, 4], k=5)) print(random.choices(a, cum_weights=[1, 2, 3, 4, 5], k=5)) # 結(jié)果 [1, 1, 1, 1, 1] [2, 2, 1, 2, 1] [5, 5, 1, 4, 2]
是不是看不懂?我也看不懂,但其實(shí)就是普通權(quán)重相加而已
cum_weights=[1, 1, 1, 1, 1]
- 等價(jià)于 weights=[1, 0, 0, 0, 0]
- [1,1+0,1+0+0,1+0+0+0,1+0+0+0+0]
- 看懂了沒,太反人類了。。
cum_weights=[1, 4, 4, 4, 4]
- 等價(jià)于 weights=[1, 3, 0, 0, 0]
- [1,1+3,1+3+0,1+3+0+0,1+3+0+0+0]
random.shuffle
語法格式
將序列 x 隨機(jī)打亂位置
只能是列表[],元組、字符串會(huì)報(bào)錯(cuò)哦
random 暫時(shí)沒找到有什么用,可以忽略
random.shuffle(x[, random])
實(shí)際栗子
# 數(shù)字?jǐn)?shù)組 a = [1, 2, 3, 4, 5] random.shuffle(a) print(a) # 字母數(shù)組 b = ["a", "b", "c"] random.shuffle(b) print(b) #### [3, 5, 2, 4, 1] ['a', 'c', 'b']
random.sample
語法格式
- 從 population 中取 k 個(gè)元素,組成新的列表并返回
- 每次取元素都是不重復(fù)的,所以 population 的長度必須 ≥ k,否則會(huì)報(bào)錯(cuò)
random.sample(population, k)
實(shí)際栗子
全都是 k=3
# 數(shù)字?jǐn)?shù)組 print(random.sample([1, 2, 3, 4, 5], 3)) # 字母數(shù)組 print(random.sample(["a", "b", "c"], 3)) # 字母元組 print(random.sample(("a", "b", "c"), 3)) # 字符串 print(random.sample("abcdef", 3)) # string 模塊返回的大小寫字母字符串 print(random.sample(string.ascii_letters, 3)) # string 模塊返回的數(shù)字字符串 print(random.sample(string.digits, 3)) # string 模塊返回的數(shù)字字符串+大小寫字母字符串 print(random.sample(string.digits + string.ascii_uppercase, 3)) #### [2, 1, 3] ['b', 'c', 'a'] ['a', 'b', 'c'] ['a', 'f', 'b'] ['M', 'w', 'W'] ['7', '1', '5'] ['R', '8', 'O']
以上就是Python random 庫的詳細(xì)使用的詳細(xì)內(nèi)容,更多關(guān)于Python random 庫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實(shí)現(xiàn)的文件同步服務(wù)器實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)的文件同步服務(wù)器,實(shí)例分析了文件同步服務(wù)器的原理及客戶端、服務(wù)端的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-06-06Python?OpenCV實(shí)現(xiàn)3種濾鏡效果實(shí)例
opencv是一個(gè)很強(qiáng)大的庫,支持多個(gè)編程語言,下面這篇文章主要給大家介紹了關(guān)于Python?OpenCV實(shí)現(xiàn)3種濾鏡效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Pytorch實(shí)現(xiàn)WGAN用于動(dòng)漫頭像生成
這篇文章主要介紹了Pytorch實(shí)現(xiàn)WGAN用于動(dòng)漫頭像生成,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03淺談Python2.6和Python3.0中八進(jìn)制數(shù)字表示的區(qū)別
下面小編就為大家?guī)硪黄獪\談Python2.6和Python3.0中八進(jìn)制數(shù)字表示的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04python3模塊smtplib實(shí)現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了python3模塊smtplib實(shí)現(xiàn)發(fā)送郵件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05pycharm不在cmd中運(yùn)行卻在python控制臺(tái)運(yùn)行問題解決
這篇文章主要介紹了pycharm不在cmd中運(yùn)行卻在python控制臺(tái)運(yùn)行問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08