Python random模塊使用詳解
一、介紹random模塊
1.1、random模塊簡介
random模塊是Python標準庫中用于生成偽隨機數(shù)的模塊,偽隨機數(shù)是通過算法生成的數(shù)列,在一定范圍內(nèi)表現(xiàn)出隨機性,雖然這些數(shù)列在一定程度上是可預(yù)測的,但對于大多數(shù)應(yīng)用來說已經(jīng)足夠。
二、random模塊的基本功能
2.1、整數(shù)用函數(shù)
2.1.1、random.randrange()
random.randrange(start, stop[, step])
返回從 range(start, stop, step) 隨機選擇的一個元素。
支持設(shè)置步長,可以生成按固定間隔的隨機數(shù),這對于需要特定模式或間隔的隨機數(shù)生成非常有用。注意:不包括右邊界
# 返回 0 <= N < 100 范圍內(nèi)的隨機整數(shù) print(random.randrange(100)) # 返回 100 <= N < 200 范圍內(nèi)的隨機整數(shù) print(random.randrange(100, 200)) # 返回 100 <= N < 200 范圍內(nèi)的隨機整數(shù),步長為5 print(random.randrange(100, 200, 5))
2.1.2、random.randint()
random.randint(a, b)
返回隨機整數(shù) N 滿足 a <= N <= b。相當于 randrange(a, b+1)。
不支持設(shè)置步長,只能生成指定范圍內(nèi)的任意整數(shù)。注意:包括左邊界和右邊界
# 返回 100 <= N <= 200 范圍內(nèi)的隨機整數(shù) print(random.randint(100, 200))
2.1.3、random.getrandbits()
random.getrandbits(k)
返回隨機整數(shù) N 滿足 0 <= N <= 2^k-1,例如,getrandbits(16)將生成一個0到65535之間的隨機整數(shù)。這個函數(shù)特別適用于需要生成大范圍隨機數(shù)的情況。
# 返回 0 <= N <= 65535(2的16次方再減去1) 范圍內(nèi)的隨機整數(shù) print(random.getrandbits(16))
2.2、序列用函數(shù)
2.2.1、random.choice()
random.choice(seq)
從非空序列 seq 返回一個隨機元素。 如果 seq 為空,則引發(fā) IndexError。
a = ['alice', 'bob', 'helen', 'jack', 'sue'] b = ('alice', 'bob', 'helen', 'jack', 'sue') c = {'alice', 'bob', 'helen', 'jack', 'sue'} d = {'alice': 16, 'bob': 18, 'helen': 19, 'jack': 20, 'sue': 22} e = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # 從列表中隨機選取一個元素 print(random.choice(a)) # 從元組中隨機選取一個元素 print(random.choice(b)) # 從字符串中隨機選取一個元素 print(random.choice(e)) # ——————————————————————————————————————————— # 不支持從集合中隨機選取,會報錯 # print(random.choice(c)) # 不支持從字典中隨機選取,會報錯,但可以使用 random.choice(list(d.items())) 的方法來實現(xiàn) # print(random.choice(d))
2.2.2、random.choices()
random.choices(population, weights=None, *, cum_weights=None, k=1)
- population:必需參數(shù),指定要進行選擇的序列(可以是列表、元組、字符串等)。
- weights:可選參數(shù),指定每個元素的權(quán)重(概率)。如果不指定,則默認每個元素的權(quán)重相等。
- cum_weights:可選參數(shù),指定累計權(quán)重。如果指定了cum_weights,則必需省略weights參數(shù)。
- k:可選參數(shù),指定要選擇的元素個數(shù)。默認為1,即只選擇一個元素。
從序列中有放回地隨機抽取k個元素,并返回對應(yīng)的數(shù)組,weights參數(shù)代表每個元素被選到的相對權(quán)重,而cum_weights代表的是累積權(quán)重,它們的長度需和待抽取序列population長度一致。若無設(shè)置權(quán)重,則每個元素被抽到的權(quán)重是一樣的。
注意:權(quán)重不能為負值,否則會報錯。
import random fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] weights = [0.1, 0.2, 0.3, 0.2, 0.2] cum_weights = [0.1, 0.4, 0.7, 0.9, 1.0] # 從列表中隨機選擇一個元素 chosen_fruit = random.choices(fruits) # 選擇多個元素 chosen_fruits = random.choices(fruits, k=3) # 按照概率從列表中隨機選擇一個元素 chosen_fruit = random.choices(fruits, weights=weights) # 利用cum_weights參數(shù)選擇元素 chosen_fruit = random.choices(fruits, cum_weights=cum_weights)
選擇多個元素并計算選擇的次數(shù)
import random fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] chosen_fruits = random.choices(fruits, k=1000) fruit_counts = {} for fruit in chosen_fruits: if fruit in fruit_counts: fruit_counts[fruit] += 1 else: fruit_counts[fruit] = 1 print(fruit_counts)
2.2.3、random.shuffle()
random.shuffle(x)
就地將序列 x 隨機打亂位置。
import random fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] random.shuffle(fruits) # 注意:改變的是原列表 print(fruits) # ['orange', 'watermelon', 'grape', 'banana', 'apple']
2.2.4、random.sample()
random.sample(population, k, *, counts=None)
從序列中無放回地隨機抽取k個元素。用于無重復(fù)的隨機抽樣。
import random fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] # 返回一個新的隨機打亂序列, 注意:原列表是沒有改變的 print(random.sample(fruits,len(fruits))) # ['orange', 'watermelon', 'apple', 'banana', 'grape'] print(fruits) # ['apple', 'banana', 'orange', 'grape', 'watermelon']
重復(fù)的元素可以一個個地直接列出,或使用可選的僅限關(guān)鍵字形參 counts 來指定。 例如,sample(['red', 'blue'], counts=[4, 2], k=5) 等價于 sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)。
要從一系列整數(shù)中選擇樣本,請使用 range() 對象作為參數(shù)。 對于從大量人群中采樣,這種方法特別快速且節(jié)省空間:sample(range(10000000), k=60) 。
2.3、離散分布
2.3.1、random.binomialvariate()
random.binomialvariate(n=1, p=0.5)
二項式分布。 返回 n 次獨立試驗在每次試驗的成功率為 p 時的成功次數(shù)
2.4、實值分布
2.4.1、random.random()
random.random()
返回 0.0 <= X < 1.0 范圍內(nèi)的隨機浮點數(shù)
2.4.2、random.uniform(a, b)
random.uniform(a, b)
返回一個隨機浮點數(shù) N ,a <= N <= b。
import random print(random.uniform(60, 100))
2.4.2.1、保留小數(shù)點后n位的方法
2.4.2.1.1、使用round()函數(shù)
num = 3.14159 # 這種方式是進行的四舍五入 rounded_num = round(num, 2) # 輸出: 3.14
2.4.2.1.2、使用format()函數(shù)
num = 3.14159 # 這種方式是進行的四舍五入,注意:得到的是字符串形式的浮點數(shù) formatted_num = "{:.2f}".format(num) # 輸出: '3.14'
2.4.2.1.3、使用decimal模塊進行精確控制
from decimal import Decimal, ROUND_HALF_UP num = Decimal('3.14159') context = Decimal(1) / Decimal(10) ** 2 # 這種方式是進行的四舍五入 rounded_num = num.quantize(context, rounding=ROUND_HALF_UP) # 輸出: Decimal('3.14')
2.4.2.1.4、使用f-string(Python 3.6+)
num = 3.14159 formatted_num = f"{num:.2f}" # 輸出: '3.14'
三、與random相關(guān)的模塊
3.1、secrets模塊
secrets 模塊用于生成高度加密的隨機數(shù),適于管理密碼、賬戶驗證、安全憑據(jù)及機密數(shù)據(jù)。
3.1.1、secrets.choice()
secrets.choice(seq)
返回一個從非空序列中隨機選取的元素。
import secrets # 假設(shè)我們有一個元素列表 fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] # 使用 secrets.choice 來安全地選擇列表中的一個隨機元素 secure_random_element = secrets.choice(fruits) print(secure_random_element)
3.1.2、secrets.randbelow()
secrets.randbelow(exclusive_upper_bound)
返回 [0, exclusive_upper_bound) 范圍內(nèi)的隨機整數(shù)。
import secrets # 生成一個安全的隨機整數(shù),范圍在 0 <= N < 10之間 secure_int = secrets.randbelow(10) print(f"安全隨機整數(shù):{secure_int}")
3.1.3、secrets.randbits()
secrets.randbits(k)
返回一個具有k個隨機位的非負整數(shù)。如 secrets.randbits(3),表示取值為 0 <= N < 2^3,也就是 [0, 8) 之間的隨機整數(shù)。
3.1.4、secrets.token_bytes()
secrets.token_bytes([nbytes=None])
如果指定了 nbytes, secrets.token_bytes(nbytes) 會返回一個包含 nbytes 個隨機字節(jié)的 bytes 對象。 如果未提供 nbytes 參數(shù),它會返回一個合適用于安全令牌的默認長度的隨機字節(jié)序列(通常為 32 個字節(jié))。
import secrets # 生成默認長度(通常為32個字節(jié))的隨機字節(jié)序列 random_token = secrets.token_bytes() print(len(random_token)) # 輸出:32 print(random_token) # 輸出隨機字節(jié)序列:b'<\xa1\xc2\xb9k\xf4\x9e$\xd9\x03\xd5H\xb8\x1e\xe2d\xe0\x82x\xe9)\x11\xe9\x04l\xda\x95\xe3\xf0C\x88\xba' # 生成指定長度的隨機字節(jié)序列 specified_length_token = secrets.token_bytes(16) print(specified_length_token) # 輸出隨機字節(jié)序列:b'\xa2C\x98H\xb2\xca5\n\xb69h{\xfa\xb3n\xc5'
3.1.5、secrets.token_hex()
secrets.token_hex([nbytes=None])
如果提供了 nbytes 參數(shù),則會生成長度為 nbytes*2 的十六進制字符串(每個字節(jié)轉(zhuǎn)換為兩個十六進制字符)。 如果未提供 nbytes 參數(shù),則會生成一個適用于安全令牌的默認長度的十六進制字符串(通常是 32 個字符)。
import secrets # 生成默認長度(通常為32個字符)的隨機十六進制字符串 random_hex_token = secrets.token_hex() print(random_hex_token) # 53063bd5d38f7f032e146d769567254748dcbc34de37f716043a21d4b9ef0575 # 生成指定長度的隨機十六進制字符串 specified_length_hex_token = secrets.token_hex(16) print(specified_length_hex_token) # bcce543dee60747639895e2fcffcfaf8
3.1.6、secrets.token_urlsafe()
secrets.token_urlsafe([nbytes=None])
返回安全的 URL 隨機文本字符串,包含 nbytes 個隨機字節(jié)。文本用 Base64 編碼,平均來說,每個字節(jié)對應(yīng) 1.3 個結(jié)果字符。 未提供 nbytes 或為 None 時,它會生成一個適合于安全令牌的默認長度隨機 URL 安全字符串。 這個函數(shù)生成的返回值是一個只包含 URL 安全字符(字母、數(shù)字、下劃線和短橫線)的字符串。
import secrets # 生成默認長度的隨機 URL 安全字符串 random_urlsafe_token = secrets.token_urlsafe() print(len(random_urlsafe_token)) # 輸出:43 print(random_urlsafe_token) # 輸出:t6GrND8P32pL763R36VQIaB70jf8r_uruvSa0wgQYrY # 生成指定長度的隨機 URL 安全字符串 specified_length_urlsafe_token = secrets.token_urlsafe(16) print(specified_length_urlsafe_token) # 輸出:dUA2mPJbbWtB6rLj8hoC1g
3.1.7、secrets.compare_digest()
secrets.compare_digest(a, b)
用于比較兩個字符串 a 和 b,并且在字符串匹配時具有防止時間側(cè)信道攻擊的特性。在密碼學(xué)和安全相關(guān)的場景中,比較兩個敏感字符串時,使用 secrets.compare_digest 要優(yōu)于簡單的 == 操作符。 該函數(shù)返回一個布爾值,如果 a 和 b 匹配,返回 True,否則返回 False。這種比較在比較時間上更加均勻,不易受到時間側(cè)信道攻擊的影響。在比較敏感數(shù)據(jù)時,尤其是在密碼驗證或令牌比對時使用這個函數(shù),可以提高系統(tǒng)的安全性。
生成系統(tǒng)密碼示例
import string import random import secrets def generate_password(min_length, max_repeat, max_class_repeat, *char_credits): characters = [string.digits, string.ascii_uppercase, '!@#$%^&*(){}<>,?`~+-=[]', string.ascii_lowercase] all_characters = ''.join(characters) while True: # 生成每個字符類別的字符 password = [secrets.choice(char) for char, count in zip(characters, char_credits) for _ in range(count)] # 添加字符以滿足最小密碼長度要求 remaining_len = min_length - sum(char_credits) password += [secrets.choice(all_characters) for _ in range(remaining_len)] random.shuffle(password) if is_valid_password(min_length, password, characters, max_repeat, max_class_repeat): return ''.join(password) def is_valid_password(min_length, password, characters, max_repeat, max_class_repeat): # 判斷是否以=開頭 if ''.join(password).startswith("="): return False # 檢查字符和類別是否連續(xù)重復(fù)超過了規(guī)定次數(shù) repeat_count = max(password.count(char) for char in set(password)) if repeat_count > min_length // 4: return False for char_class in characters: char_class_positions = [idx for idx, char in enumerate(password) if char in char_class] for idx in char_class_positions: if idx < len(password) - max_class_repeat: if all(password[idx + i] in char_class for i in range(0, max_class_repeat + 1)): return False # 判定同一個字符的索引 char_repeat = [char for char in set(password) if password.count(char) > max_repeat] char_repeat_positions = {} for idx, char in enumerate(password): if char in char_repeat: if char not in char_repeat_positions: char_repeat_positions[char] = [idx] else: char_repeat_positions[char].append(idx) # 判定相同字符是否連續(xù) for _, index in char_repeat_positions.items(): for idx in index: if idx < len(password) - max_repeat: if all(password[idx + i] == password[idx] for i in range(0, max_repeat + 1)): return False return True if __name__ == '__main__': # 設(shè)置密碼生成的參數(shù) min_length = 16 char_credits = (3, 3, 3, 3) # 數(shù)字、大寫字母、特殊字符、小寫字母 # 同一個字符不能連續(xù)重復(fù)的數(shù)量 max_repeat = 2 # 同一類字符不能連續(xù)重復(fù)的數(shù)量 max_class_repeat = 3 # 生成和打印密碼 for _ in range(100): generated_password = generate_password(min_length, max_repeat, max_class_repeat, *char_credits) print(generated_password)
生成密碼重置URL示例
import secrets import string # 生成安全令牌,用于密碼恢復(fù)應(yīng)用程序 security_token = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(16)) # 構(gòu)建臨時密保URL temp_url = f"https://example.com/password-recovery?token={security_token}" print("生成的臨時密保URL:", temp_url) # 輸出:生成的臨時密保URL: https://example.com/password-recovery?token=90HOEUVFroaJIO5D
到此這篇關(guān)于Python random模塊使用詳解的文章就介紹到這了,更多相關(guān)Python random模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Mixin設(shè)計模式進行Python編程的方法講解
Mixin模式也可以看作是一種組合模式,綜合多個類的功能來產(chǎn)生一個類而不通過繼承來實現(xiàn),下面就來整理一下使用Mixin設(shè)計模式進行Python編程的方法講解:2016-06-06對Python中實現(xiàn)兩個數(shù)的值交換的集中方法詳解
今天小編就為大家分享一篇對Python中實現(xiàn)兩個數(shù)的值交換的集中方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01