在 Python 中生成隨機 ID
我們在專案中使用隨機數生成樣本資料,這些資料稍後可用於測試、填充空列或用於許多其他目的,關鍵是我們需要生成隨機資料。在 Python 中,有許多方法可以生成隨機資料,我們將在本文中探討其中的一些方法。
Python random() 模組
Python 自帶的一個重要庫是 random,我們將在程式碼中始終使用它。
要在程式碼中使用此模組,您只需匯入它,就是這樣,我們就可以使用它了。
import random
讓我們看看如何使用它:
import random print("What i will get, no idea as i'm using random.random()") print(random.random())
輸出
What i will get, no idea as i'm using random.random() 0.5306626723173611
如果我第二次嘗試執行相同的程式,您將獲得不同的輸出:
What i will get, no idea as i'm using random.random() 0.5504289430397661
關於 random 模組的一些要點
- random() 是 random 模組的基本函式
- random 模組幾乎所有函式都使用 random() 函式。
- Random() 函式將生成 [0.0 到 1.0) 之間的任意數字。
在 Python 中生成隨機整數
下面我們使用兩個函式來生成隨機整數:
- randint()
- randrange()
from random import randint, randrange print("Printing random integer ", randint(0, 20)) print("Printing random integer ", randrange(0, 20, 2))
輸出
Printing random integer 15 Printing random integer 4
從列表中隨機選擇一個專案
假設我們有一個公司名稱列表,我們想從該列表中檢索一個專案(公司名稱)。我們可以透過以下方式實現:
import random companies = ['RELIANCE', 'TCS', 'INFY', 'SBI', 'PNB','HDFC'] print('Randomly selecting company from a list: ', random.choice(companies))
輸出
Randomly selecting company from a list: INFY
從列表中隨機選擇多個專案
考慮上面的例子,但我們想從列表中隨機選擇多個專案(公司),而不是一個專案(公司),我們可以透過 random.sample() 函式實現:
import random companies = ['RELIANCE', 'TCS', 'INFY', 'SBI', 'PNB','HDFC'] print('Randomly selecting 3 companies from a list: ', random.sample(companies,3))
輸出
Randomly selecting 3 companies from a list: ['TCS', 'RELIANCE', 'INFY']
但是,如果我們嘗試選擇的專案數量超過列表中的專案數量,我們會遇到 ValueError:
輸入:
random.sample(companies,20)
輸出:
ValueError: Sample larger than population or is negative
從列表中選擇多個隨機專案的另一種方法是 – random.choices()。
import random companies = ['RELIANCE', 'TCS', 'INFY', 'SBI', 'PNB','HDFC'] print('Randomly selecting 3 companies from a list: ', random.choices(companies,k=6))
輸出
Randomly selecting 3 companies from a list: ['TCS', 'TCS', 'INFY', 'HDFC', 'INFY', 'TCS']
從上面的輸出可以看出,使用 random.choices() 方法可能會從列表中獲得重複的專案。
Python 中的偽隨機數生成器
偽隨機數生成器透過對某個值執行某些操作來工作。通常,此值是生成器先前生成的數字。但是,第一次使用生成器時,沒有先前的值。
import random print("Seed value 10: ") # Initialize seed value random.seed(10) for i in range(5): print(random.randint(1,100)) print() print("Seed Value 5: ") # this time we'll get different values random.seed(5) for i in range(5): print(random.randint(1,100)) print() print("Seed value: 10") # will get the same result, what we got initially random.seed(10) for i in range(5): print(random.randint(1,100))
輸出
Seed value 10: 74 5 55 62 74 Seed Value 5: 80 33 95 46 89 Seed value: 10 74 5 55 62 74
從上面的例子可以看出,如果種子相同,它會生成第一個先前的值。對於給定的隨機數生成器,每個種子值對應於一個固定的生成值序列。
在 Python 中生成密碼學安全的隨機數
我們可以在 Python 3.x 中生成密碼學安全的隨機數。如果我們使用 Python 3.6 或更高版本,我們可以使用新的 secrets 模組和下面的 rand 函式。它將在指定的值以下生成一個隨機數。
import secrets #generate 10 secure random numbers between 10 and 500 for x in range(0,10): secV =10+ secrets.randbelow(500) print(secV)
輸出
464 406 184 293 399 332 495 292 118 134
對於 Python 3.5 或更低版本,另一種方法是使用 random 模組和 SystemRandom 類來生成密碼學安全的隨機數,方法是:
import random randGen = random.SystemRandom() for x in range(0,10): secV = 10+ randGen.randint(0,499) print(secV)
輸出
374 211 425 264 217 97 210 39 319 52
另一種方法是使用 random 和 secrets(用於保護資料)模組。
import secrets import random secNum = random.SystemRandom().random() print("secure number is ", secNum) print("Secure byte token", secrets.token_bytes(16))
輸出
secure number is 0.5205307353786663 Secure byte token b'\x05T>\xacsqn0\x08\xc4\xf4\x8aU\x13\x9f\xcf'