您的位置:首頁技術文章
文章詳情頁

Python 中random 庫的詳細使用

瀏覽:75日期:2022-06-17 14:57:11

Random庫主要包含返回隨機數的函數,主要用于普通的隨機數生成的程序,如果對隨機性有特殊要求,比如加密等,可以用函數os.urandom()或者random模塊內部的SystemRandom類,這些可以讓數據接近真正的隨機性。

前言 為啥突然寫這個?因為用到就寫唄,感覺對生成數據很有用,之前都是百度別人的,今天來對著官方文檔寫,超級標準! 這邊只講常用的,看了下文檔還有什么數學方法,太高級好像用不上返回整數

random.randrange語法格式

兩種寫法

random.randrange(stop)random.randrange(start, stop[, step]) start:起始數字,包含(取得到 start 這個值) stop:末尾數字,不包含(取不到 stop 這個值) step:步長

實際栗子

# 栗子一for i in range(5): print(random.randrange(20))####174774# 栗子二for i in range(5): print(random.randrange(10, 20))####1314111717# 栗子三for i in range(5): print(random.randrange(10, 20, 2))####1212141410

random.randint

語法格式

返回隨機整數N滿足a<=N<=b 相當于randrange(a,b+1)

random.randint(a, b)

實際栗子

for i in range(5): print(random.randint(0,20))####19201163

a、b 都可以取得到哦

返回浮點數

random.random()語法格式

返回 [0.0, 1.0) 范圍內的下一個隨機浮點數

random.random()

實際栗子

# 栗子一for i in range(5): print(random.random())####0.98294922431653350.434735064301057240.51987091872430760.64378843058207360.7216771961168909# 栗子二for i in range(5): print(math.ceil(random.random() * 1000))####77235232162127

random.uniform(a, b)

語法格式

返回一個隨機浮點數N 當a<=b時,a<=N<=b 當b<a時,b<=N<=a

random.uniform(a, b)

實際栗子

# 栗子一for i in range(5): print(random.uniform(1, 10))####2.62002620897545939.2205069114692353.02068967040147839.6709053303391741.170694187192196# 栗子二for i in range(5): print(random.uniform(8, 2))####2.6968427579542656.0587949351102757.5676312200151442.20576982022580744.454083664106361傳遞列表作為參數

random.choice

語法格式

從非空序列 seq 返回一個隨機元素 如果 seq 為空,會拋出 IndexError

random.choice(seq)

實際栗子

# 數字數組print(random.choice([1, 2, 3, 4, 5]))# 字母數組print(random.choice(['a', 'b', 'c']))# 字母元組print(random.choice(('a', 'b', 'c')))# 字符串print(random.choice('abcdef'))# string 模塊返回的大小寫字母字符串print(random.choice(string.ascii_letters))# string 模塊返回的數字字符串print(random.choice(string.digits))# string 模塊返回的數字字符串+大小寫字母字符串print(random.choice(string.digits + string.ascii_uppercase))####5ccel2Frandom.choices

語法格式

populaiton:序列 weights:普通權重 cum_weights:累加權重 k:選擇次數 weights 和 cum_weights 不能同時傳,只能選擇一個來傳

random.choices(population, weights=None, *, cum_weights=None, k=1)

看的迷迷糊糊啥意思。。?來看栗子。。

不帶參數的栗子

a = [1,2,3,4,5]print(random.choices(a,k=5))# 結果[5, 5, 3, 1, 5]

可以重復取元素

帶 weight 的栗子一

a = [1, 2, 3, 4, 5]print(random.choices(a, weights=[0, 0, 1, 0, 0], k=5))# 結果[3,3,3,3,3] 序列有多長,weights 對應的序列就得多長,每個位置都是一一對應 像這里,3 的權重是 1,其他是 0 ,所以每次都取 3,因為它的權重最高,其他元素沒有權重

帶 weight 的栗子二

a = [1, 2, 3, 4, 5]print(random.choices(a, weights=[0, 2, 1, 0, 0], k=5))# 結果[2, 2, 2, 2, 3]

2 的權重更大,所以取到它的概率更高

帶 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))# 結果[1, 1, 1, 1, 1][2, 2, 1, 2, 1][5, 5, 1, 4, 2]

是不是看不懂?我也看不懂,但其實就是普通權重相加而已

cum_weights=[1, 1, 1, 1, 1]

等價于 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]

等價于 weights=[1, 3, 0, 0, 0] [1,1+3,1+3+0,1+3+0+0,1+3+0+0+0]

random.shuffle

語法格式

將序列 x 隨機打亂位置只能是列表[],元組、字符串會報錯哦random 暫時沒找到有什么用,可以忽略

random.shuffle(x[, random])

實際栗子

# 數字數組a = [1, 2, 3, 4, 5]random.shuffle(a)print(a)# 字母數組b = ['a', 'b', 'c']random.shuffle(b)print(b)####[3, 5, 2, 4, 1][’a’, ’c’, ’b’]

random.sample

語法格式

從 population 中取 k 個元素,組成新的列表并返回 每次取元素都是不重復的,所以 population 的長度必須 ≥ k,否則會報錯

random.sample(population, k)實際栗子

全都是 k=3

# 數字數組print(random.sample([1, 2, 3, 4, 5], 3))# 字母數組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 模塊返回的數字字符串print(random.sample(string.digits, 3))# string 模塊返回的數字字符串+大小寫字母字符串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 庫的詳細使用的詳細內容,更多關于Python random 庫的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
国产综合久久一区二区三区