如何在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模組更可取。
示例
以下程式使用randbelow()函式和for迴圈生成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模組。我們還討論了這些函式在有和沒有迴圈的情況下的使用方法。