- Python進階編程:編寫更高效、優雅的Python代碼
- 劉宇宙 謝東 劉艷
- 747字
- 2021-04-30 12:39:35
1.7 隨機數
隨機數處理也是實際應用中比較常見的操作,如從一個序列中隨機抽取若干元素,或者生成幾個隨機數,以及生成隨機數驗證碼等。
random模塊中有大量的函數用來產生隨機數和隨機選擇元素。如想從一個序列中隨機抽取一個元素,可以使用random.choice()函數:
import random values = [1, 2, 3, 4, 5, 6] print(f'random choice from {values} is {random.choice(values)}') print(f'random choice from {values} is {random.choice(values)}') print(f'random choice from {values} is {random.choice(values)}')
執行py文件,輸出結果如下:
random choice from [1, 2, 3, 4, 5, 6] is 2 random choice from [1, 2, 3, 4, 5, 6] is 5 random choice from [1, 2, 3, 4, 5, 6] is 3
如果想提取N個不同元素的樣本做進一步操作,可使用random.sample()函數:
print(f'random sample 2 from {values} is {random.sample(values, 2)}') print(f'random sample 2 from {values} is {random.sample(values, 2)}') print(f'random sample 3 from {values} is {random.sample(values, 3)}') print(f'random sample 3 from {values} is {random.sample(values, 3)}')
執行py文件,輸出結果如下:
random sample 2 from [1, 2, 3, 4, 5, 6] is [5, 6] random sample 2 from [1, 2, 3, 4, 5, 6] is [2, 1] random sample 3 from [1, 2, 3, 4, 5, 6] is [6, 3, 4] random sample 3 from [1, 2, 3, 4, 5, 6] is [3, 4, 6]
如果只想打亂序列中元素的順序,可以使用random.shuffle()函數:
random.shuffle(values) print(f'random shuffle is:{values}') random.shuffle(values) print(f'random shuffle is:{values}')
執行py文件,輸出結果如下:
random shuffle is:[2, 4, 6, 1, 3, 5] random shuffle is:[2, 6, 5, 3, 1, 4]
如果想生成隨機整數,可以使用random.randint()函數:
print(f'random.randint(0,10) = {random.randint(0,10)}') print(f'random.randint(0,10) = {random.randint(0,10)}') print(f'random.randint(0,10) = {random.randint(0,10)}')
執行py文件,輸出結果如下:
random.randint(0,10) = 9 random.randint(0,10) = 8 random.randint(0,10) = 10
如果想生成0到1范圍內均勻分布的浮點數,可以使用random.random()函數:
print(f'random.random() = {random.random()}') print(f'random.random() = {random.random()}') print(f'random.random() = {random.random()}')
執行py文件,輸出結果如下:
random.random() = 0.3392503938211737 random.random() = 0.625725029316508 random.random() = 0.3843832669520403
如果要獲取N位隨機位(二進制)的整數,可以使用random.getrandbits()函數:
print(f'random.getrandbits(200) = {random.getrandbits(200)}')
執行py文件,輸出結果如下:
random.getrandbits(200) = 1012257713162841215793585318570079770213053719376074162565721
random模塊使用Mersenne Twister算法來生成隨機數。這是一個確定性算法,可以通過random.seed()函數修改初始化種子,示例如下:
print(f'random.seed() = {random.seed()}') print(f'random.seed(123) = {random.seed(123)}') print(f"random.seed(b'bytedata') = {random.seed(b'bytedata')}")
執行py文件,輸出結果如下:
random.seed() = None random.seed(123) = None random.seed(b'bytedata') = None
除了上述介紹的功能,random模塊還包含基于均勻分布、高斯分布和其他分布的隨機數生成函數。如random.uniform()函數用于計算均勻分布隨機數,random.gauss()函數用于計算正態分布隨機數。
random模塊中的函數不應該用在和密碼學相關的程序中。如果確實需要類似的功能,可以使用ssl模塊中相應的函數。如ssl.RAND_bytes()函數可以用來生成一個安全的隨機字節序列。
推薦閱讀
- UI設計基礎培訓教程
- Network Automation Cookbook
- Working with Odoo
- Mastering Xamarin.Forms(Second Edition)
- Django 3.0入門與實踐
- Unity 2018 Augmented Reality Projects
- Cocos2d-x Game Development Blueprints
- Android Sensor Programming By Example
- 跟戴銘學iOS編程:理順核心知識點
- 深入實踐C++模板編程
- 軟件測試
- 零基礎學Java(升級版)
- Prezi Cookbook
- Unity3D高級編程:主程手記
- Implementing OpenShift