Python生成隨機數(shù)組的方法小結(jié)
本文實例講述了Python生成隨機數(shù)組的方法。分享給大家供大家參考,具體如下:
研究排序問題的時候常常需要生成隨機數(shù)組來驗證自己排序算法的正確性和性能,今天把Python生成隨機數(shù)組的方法稍作總結(jié),以備以后查看使用。
一、使用random模塊生成隨機數(shù)組
python的random模塊中有一些生成隨機數(shù)字的方法,例如random.randint, random.random, random.uniform, random.randrange,這些函數(shù)大同小異,均是在返回指定范圍內(nèi)的一個整數(shù)或浮點數(shù),下邊簡單解釋一下這幾個函數(shù)。
1、random.randint(low, hight) -> 返回一個位于[low,hight]之間的整數(shù)
該函數(shù)接受兩個參數(shù),這兩個參數(shù)必須是整數(shù)(或者小數(shù)位是0的浮點數(shù)),并且第一個參數(shù)必須不大于第二個參數(shù)
>>> import random >>> random.randint(1,10) 5 >>> random.randint(1.0, 10.0) 5
2、random.random() -> 不接受參數(shù),返回一個[0.0, 1.0)之間的浮點數(shù)
>>> random.random() 0.9983625479554628
3、random.uniform(val1, val2) -> 接受兩個數(shù)字參數(shù),返回兩個數(shù)字區(qū)間的一個浮點數(shù),不要求val1小于等于val2
>>> random.uniform(1,5.0) 2.917249424176132 >>> random.uniform(9.9, 2) 3.4288029275359024
*4、random.randrange(start, stop, step) -> 返回以start開始,stop結(jié)束,step為步長的列表中的隨機整數(shù),同樣,三個參數(shù)均為整數(shù)(或者小數(shù)位為0),若start大于stop時 ,setp必須為負(fù)數(shù).step不能是0.*
>>> random.randrange(1, 100, 2) #返回[1,100]之間的奇數(shù) 95 >>> random.randrange(100, 1, -2) #返回[100,1]之間的偶數(shù) 46
運行效果圖如下:
5、生成隨機數(shù)組
下邊我們用random.randint來生成一個隨機數(shù)組
import random def random_int_list(start, stop, length): start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start)) length = int(abs(length)) if length else 0 random_list = [] for i in range(length): random_list.append(random.randint(start, stop)) return random_list
接下來我們就可以用這個函數(shù)來生成一個隨機的整數(shù)序列了
>>> random_int_list(1,100,10) [54, 13, 6, 89, 87, 39, 60, 2, 63, 61]
二、使用numpy.random模塊來生成隨機數(shù)組
1、np.random.rand 用于生成[0.0, 1.0)之間的隨機浮點數(shù), 當(dāng)沒有參數(shù)時,返回一個隨機浮點數(shù),當(dāng)有一個參數(shù)時,返回該參數(shù)長度大小的一維隨機浮點數(shù)數(shù)組,參數(shù)建議是整數(shù)型,因為未來版本的numpy可能不支持非整形參數(shù)。
import numpy as np >>> np.random.rand(10) array([ 0.56911206, 0.99777291, 0.18943144, 0.19387287, 0.75090637, 0.18692814, 0.69804514, 0.48808425, 0.79440667, 0.66959075])
當(dāng)然該函數(shù)還可以用于生成多維數(shù)組,這里不做詳述。
2、np.random.randn該函數(shù)返回一個樣本,具有標(biāo)準(zhǔn)正態(tài)分布。
>>> np.random.randn(10) array([-1.6765704 , 0.66361856, 0.04029481, 1.19965741, -0.57514593, -0.79603968, 1.52261545, -2.17401814, 0.86671727, -1.17945975])
3、np.random.randint(low[, high, size]) 返回隨機的整數(shù),位于半開區(qū)間 [low, high)。
>>> np.random.randint(10,size=10) array([4, 1, 4, 3, 8, 2, 8, 5, 8, 9])
4、random_integers(low[, high, size]) 返回隨機的整數(shù),位于閉區(qū)間 [low, high]。
>>> np.random.random_integers(5) 4
5、np.random.shuffle(x) 類似洗牌,打亂順序;np.random.permutation(x)返回一個隨機排列
>>> arr = np.arange(10) >>> np.random.shuffle(arr) >>> arr [1 7 5 2 9 4 3 6 0 8] >>>> np.random.permutation(10) array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
PS:這里再為大家提供兩款相關(guān)在線工具供大家參考使用:
在線隨機數(shù)字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
高強度密碼生成器:
http://tools.jb51.net/password/CreateStrongPassword
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python操作MySQL數(shù)據(jù)庫9個實用實例
這篇文章主要介紹了Python操作MySQL數(shù)據(jù)庫9個實用實例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-12-12windows下python虛擬環(huán)境virtualenv安裝和使用詳解
這篇文章主要介紹了windows下python虛擬環(huán)境virtualenv安裝和使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-07-07如何用python將文件夾內(nèi)多個excel表格合并成總表
前幾天遇見這么一個問題,手上有很多張表格,這些表格中都只有一個sheet,需要把這些表匯總到一張表,下面這篇文章主要給大家介紹了關(guān)于如何用python將文件夾內(nèi)多個excel表格合并成總表的相關(guān)資料,需要的朋友可以參考下2023-06-06python實現(xiàn)數(shù)據(jù)可視化超詳細(xì)講解
Python的數(shù)據(jù)可視化是將數(shù)據(jù)以圖形或圖表的形式呈現(xiàn),使復(fù)雜的信息更易于理解和分析,本文給大家詳細(xì)介紹了python數(shù)據(jù)可視化的實現(xiàn),文中通過圖文結(jié)合的方式介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06深度學(xué)習(xí)入門之Pytorch 數(shù)據(jù)增強的實現(xiàn)
這篇文章主要介紹了深度學(xué)習(xí)入門之Pytorch 數(shù)據(jù)增強的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02