詳解Python基礎(chǔ)random模塊隨機(jī)數(shù)的生成
隨機(jī)數(shù)參與的應(yīng)用場(chǎng)景大家一定不會(huì)陌生,比如密碼加鹽時(shí)會(huì)在原密碼上關(guān)聯(lián)一串隨機(jī)數(shù),蒙特卡洛算法會(huì)通過(guò)隨機(jī)數(shù)采樣等等。Python內(nèi)置的random模塊提供了生成隨機(jī)數(shù)的方法,使用這些方法時(shí)需要導(dǎo)入random模塊。
import random
下面介紹下Python內(nèi)置的random模塊的幾種生成隨機(jī)數(shù)的方法。
1、random.random() 隨機(jī)生成 0 到 1 之間的浮點(diǎn)數(shù)[0.0, 1.0) 。
print("random: ", random.random()) #random: 0.5714025946899135
2、random.randint(a , b) 隨機(jī)生成 a 與 b 之間的整數(shù)[a, b]。
print("randint: ", random.randint(6,8)) #randint: 8
3、random.randrange(start,stop,step)按步長(zhǎng)step隨機(jī)在上下限范圍內(nèi)取一個(gè)隨機(jī)數(shù)。
print("randrange: ",random.randrange(20,100,5)) #randrange: 85
4、random.uniform(a, b) 隨機(jī)生成 a 與 b 之間的浮點(diǎn)數(shù)[a, b]。
print("uniform: ",random.uniform(5,10)) #uniform: 5.119790163375776
5、random.choice() 從列表中隨機(jī)取出一個(gè)元素,比如列表、元祖、字符串等。注意的是,該方法需要參數(shù)非空,否則會(huì)拋出 IndexError 的錯(cuò)誤。
print("choice: ",random.choice("www.yuanxiao.net")) #choice: y
6、random.shuffle(items) 把列表 items 中的元素隨機(jī)打亂。注意的是,如果不想修改原來(lái)的列表,可以使用 copy 模塊先拷貝一份原來(lái)的列表。
num = [1, 2, 3, 4, 5] random.shuffle(num) print("shuffle: ",num) #shuffle: [1, 3, 5, 4, 2]
7、random.sample(items, n) 從列表 items 中隨機(jī)取出 n 個(gè)元素。
num = [1, 2, 3, 4, 5] print("sample: ",random.sample(num, 3)) #sample: [4, 1, 5]
Python 的random模塊產(chǎn)生的隨機(jī)數(shù)其實(shí)是偽隨機(jī)數(shù),依賴于特殊算法和指定不確定因素(種子seed)來(lái)實(shí)現(xiàn)。如randint方法生成一定范圍內(nèi)的隨機(jī)數(shù),會(huì)先指定一個(gè)特定的seed,將seed通過(guò)特定的隨機(jī)數(shù)產(chǎn)生算法,得到一定范圍內(nèi)隨機(jī)分布的隨機(jī)數(shù)。因此對(duì)于同一個(gè)seed值的輸入產(chǎn)生的隨機(jī)數(shù)會(huì)相同,省略參數(shù)則意味著使用當(dāng)前系統(tǒng)時(shí)間秒數(shù)作為種子值,達(dá)到每次運(yùn)行產(chǎn)生的隨機(jī)數(shù)都不一樣。
random.seed(2) print("random: ", random.random()) #random: 0.9560342718892494 random.seed(3) print("random: ", random.random()) #random: 0.23796462709189137 random.seed(3)#同一個(gè)種子值,產(chǎn)生的隨機(jī)數(shù)相同 print("random: ", random.random()) #random: 0.23796462709189137
numpy庫(kù)也提供了random模塊,用于生成多維度數(shù)組形式的隨機(jī)數(shù)。使用時(shí)需要導(dǎo)入numpy庫(kù)。
import numpy as np
下面介紹下numpy庫(kù)的random模塊的幾種生成隨機(jī)數(shù)的方法。
1、numpy.random.rand(d0,d1,…,dn)
- rand函數(shù)根據(jù)給定維度生成[0,1]之間的數(shù)據(jù),包含0,不包含1
- dn表格每個(gè)維度
- 返回值為指定維度的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)
- randn函數(shù)返回一個(gè)或一組樣本,具有標(biāo)準(zhǔn)正態(tài)分布。
- dn表格每個(gè)維度
- 返回值為指定維度的array
- 標(biāo)準(zhǔn)正態(tài)分布—-standard normal distribution
- 標(biāo)準(zhǔn)正態(tài)分布又稱為u分布,是以0為均值、以1為標(biāo)準(zhǔn)差的正態(tài)分布,記為N(0,1)。
print("np.random.randn:\n {}".format(np.random.randn())) # 當(dāng)沒(méi)有參數(shù)時(shí),返回單個(gè)數(shù)據(jù) """ 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')
- 返回隨機(jī)整數(shù),范圍區(qū)間為[low,high),包含low,不包含high
- 參數(shù):low為最小值,high為最大值,size為數(shù)組維度大小,dtype為數(shù)據(jù)類型,默認(rèn)的數(shù)據(jù)類型是np.int
- high沒(méi)有填寫時(shí),默認(rèn)生成隨機(jī)數(shù)的范圍是[0,low]
print("np.random.randint:\n {}".format(np.random.randint(1,size=5)))# 返回[0,1)之間的整數(shù),所以只有0 """ np.random.randint: [0 0 0 0 0] """ print("np.random.randint:\n {}".format(np.random.randint(1,5)))# 返回1個(gè)[1,5)時(shí)間的隨機(jī)整數(shù) """ 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()
- np.random.seed()的作用:使得隨機(jī)數(shù)據(jù)可預(yù)測(cè)。
- 當(dāng)我們?cè)O(shè)置相同的seed,每次生成的隨機(jī)數(shù)相同。如果不設(shè)置seed,則每次會(huì)生成不同的隨機(jī)數(shù)
以上所述是小編給大家介紹的Python基礎(chǔ)random模塊隨機(jī)數(shù)的生成詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Python偽隨機(jī)數(shù)模塊random詳解
- Python使用random模塊生成隨機(jī)數(shù)操作實(shí)例詳解
- Python內(nèi)置random模塊生成隨機(jī)數(shù)的方法
- Python隨機(jī)數(shù)用法實(shí)例詳解【基于random模塊】
- Python隨機(jī)數(shù)random模塊使用指南
- Python中random模塊生成隨機(jī)數(shù)詳解
- Python random模塊(獲取隨機(jī)數(shù))常用方法和使用例子
- Python如何生成隨機(jī)數(shù)及random隨機(jī)數(shù)模塊應(yīng)用
相關(guān)文章
python3.4 將16進(jìn)制轉(zhuǎn)成字符串的實(shí)例
今天小編就為大家分享一篇python3.4 將16進(jìn)制轉(zhuǎn)成字符串的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06linux系統(tǒng)下pip升級(jí)報(bào)錯(cuò)的解決方法
這篇文章主要給大家介紹了關(guān)于linux系統(tǒng)下pip升級(jí)報(bào)錯(cuò)的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python的Scrapy爬蟲框架簡(jiǎn)單學(xué)習(xí)筆記
這篇文章主要介紹了Python的Scrapy爬蟲框架簡(jiǎn)單學(xué)習(xí)筆記,從基本的創(chuàng)建項(xiàng)目到CrawlSpider的使用等都有涉及,需要的朋友可以參考下2016-01-01Pandas中的unique()和nunique()區(qū)別詳解
Pandas中Series和DataFrame的兩種數(shù)據(jù)類型中都有nunique()和unique()方法,本文詳細(xì)的介紹了兩者的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2022-08-08詳解django的serializer序列化model幾種方法
序列化是將對(duì)象狀態(tài)轉(zhuǎn)換為可保持或傳輸?shù)母袷降倪^(guò)程。這篇文章主要介紹了詳解django的serializer序列化model幾種方法。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10python實(shí)現(xiàn)對(duì)arxml文件的操作方法
本篇文章給大家介紹python實(shí)現(xiàn)對(duì)arxml文件的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12Python程序設(shè)計(jì)入門(3)數(shù)組的使用
這篇文章主要介紹了Python數(shù)組的使用方法,需要的朋友可以參考下2014-06-06