如何使用 Python 生成 0 到 9 之間的隨機整數?
在本文中,我們將向您展示如何在 Python 中生成**0**到**9**之間的隨機整數。以下是完成此任務的各種方法
使用 randint() 方法
使用 randrange() 方法
使用 secrets 模組
為了在特定範圍內生成隨機整數,Python 支援多種函式。我們將討論 Python 的內建 random 模組來生成隨機數。
random 模組提供的兩個主要函式用於生成隨機整數是**randint()**和**randrange()**。為了生成隨機值,還會使用名為**secrets**的第二個模組。
使用 randint() 方法
**random.randint()**方法用於返回指定範圍內的隨機整數。
注意
randint() 方法不能使用浮點數。如果輸入的值不是整數,則會引發 ValueError: non-integer arg 1 錯誤。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
使用 import 關鍵字匯入 random 模組。
使用 random 模組的**randint()**函式生成 0 到 9 之間的隨機整數。
列印生成的 0 到 9 之間的隨機整數。
示例
以下程式使用 random.randint() 方法生成 0 到 9 之間的隨機整數:
# importing random module import random # generating a random integer between 0 and 9 without using a loop randomNumber = random.randint(0, 9) # printing the random integer between 0 and 9 print("The generated random integer between 0 and 9 = ", randomNumber)
輸出
執行上述程式後,將生成以下輸出:
The generated random integer between 0 and 9 = 4
使用 for 迴圈和 randint() 生成 0 到 9 之間的一些隨機數
演算法(步驟)
以下是執行所需任務的演算法/步驟。
使用 import 關鍵字匯入**random**模組。
使用 for 迴圈遍歷使用**range()**函式指定的範圍(返回一個從 0 開始、以 1(預設)遞增並在給定數字之前停止的數字序列)。這裡我們執行迴圈 5 次。
使用 random 模組的**randint()**函式生成 0 到 9 之間的隨機整數。
列印生成的 5 個 0 到 9 之間的隨機整數。
示例
以下程式使用 random.randint() 方法和 for 迴圈生成一些 0 到 9 之間的隨機整數:
# importing random module import random print("The 5 generated random integers between 0 and 9 using for loop: ") # traversing in the range from 0 to 5(excluded) # (running the loop 5 times) for i in range(5): # generating a random integer between 0 and 9 randomNumber = random.randint(0, 9) # printing the generated random integer print(randomNumber)
輸出
執行上述程式後,將生成以下輸出:
The 5 generated random integers between 0 and 9 using for loop: 7 2 4 8 5
使用 randrange() 方法
**random.randrange()**方法用於返回指定範圍內的隨機整數。
語法
random.randrange(start, stop, step)
引數
**start(可選)** **-** 表示起始位置的整數。預設為 0。
**stop(必需)** **-** 表示結束位置的整數
**step(可選)** **-** 表示增量的整數。預設為 1。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
使用 import 關鍵字匯入 random 模組。
透過僅傳遞**stop**引數,使用 random 模組的**randrange()**函式生成 0 到 9 之間的隨機整數。
列印生成的 0 到 9 之間的隨機整數。
示例
以下程式使用 random.randrange() 方法生成 0 到 9 之間的單個隨機整數:
# importing random module import random # generating a random integer between 0 and 9 without using loop # we are only passing the stop value here randomNumber = random.randrange(9) # printing the random integer between 0 and 9 print("The generated random integer between 0 and 9 = ", randomNumber)
輸出
執行上述程式後,將生成以下輸出:
The generated random integer between 0 and 9 = 2
使用 for 迴圈和 randrange() 生成 0 到 9 之間的一些隨機數
示例
以下程式使用 random.randrange() 方法和 for 迴圈生成一些 0 到 9 之間的隨機整數:
# importing random module import random print("The 5 generated random integers between 0 and 9 using for loop: ") # traversing in the range from 0 to 5(excuded) # (running the loop 5 times) for i in range(5): # generating random integer between 0 and 9 # we are passing the start, stop parameters here randomNumber = random.randrange(0, 9) # printing the generated random integer print(randomNumber)
輸出
執行上述程式後,將生成以下輸出:
The 5 generated random integers between 0 and 9 using for loop: 6 1 7 2 2
使用 secrets 模組
要生成隨機整數,我們可以使用 secrets 模組中的**randbelow()**函式。它生成具有高密碼安全性的隨機數。
在 Python 3.6 中,**secrets**模組是一個新的模組。對於涉及安全或加密的目的,它優於 random 模組。
示例
以下程式使用 for 迴圈和 randbelow() 函式生成 5 個 0 到 9 之間的隨機整數:
# importing randbelow function from secrets module from secrets import randbelow # traversing in the range from 0 to 5(excluded) using for loop # (running the loop 5 times) for i in range(5): # generating a random integer between 0 and 9 using randbelow() function print(randbelow(9))
輸出
執行上述程式後,將生成以下輸出:
0 5 4 4 8
結論
我們學習了兩種不同的模組來生成 0 到 9 之間的隨機整數。為了生成 0 到 9 之間的隨機整數,我們使用了 Python random 模組的兩個函式 randint() 和 randrange()。我們還了解了在 Python 3.x 中引入的新 secrets 模組。我們還討論了這些函式在有和沒有迴圈的情況下的使用。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP