Python 如何生成隨機數?
Python 包含一個內建的包,即 random 模組,用於生成隨機數。
在本文中,我們將向您展示如何使用不同的方法在 python 中生成隨機數:
使用 random.seed() 方法
使用 random.randrange() 方法
使用 random.randint() 方法
使用 random.random()
使用 random.choice() 方法
使用 random.uniform() 方法
方法 1:使用 random.seed() 方法
隨機數生成器使用 seed() 方法初始化。
為了生成隨機數,隨機數生成器需要一個起始數字值(種子值)。
注意 - 隨機數生成器預設使用當前系統時間。
要更改隨機數生成器的起始數字,請使用 seed() 方法。
如果兩次使用相同的種子值,您將獲得相同的隨機數。
語法
random.seed(x, version)
引數
x(可選) - 生成隨機數所需的種子值。如果它是整數,則直接使用;否則,必須將其轉換為整數。
version - 一個整數,指定如何將“x”引數轉換為整數。預設值為 2。如果值為 None,則生成器使用當前系統時間。
以下程式使用 random.random() 和 seed() 方法從列表中返回一個隨機元素:
import random # Setting the seed value to 5 random.seed(5) print(random.random()) # Setting the same seed value as above i.e 5 random.seed(5) print(random.random())
輸出
執行上述程式將生成以下輸出:
0.62290169489 0.62290169489
我們將種子值設定為 5,然後使用 random 模組的 random() 函式生成一個隨機值。然後我們再次將種子值設定為 5,並使用 random() 函式生成與之前相同的隨機值。這就是 seed() 函式的使用方法。
方法 2:使用 random.randrange() 方法
randrange() 方法從指定的範圍內隨機選擇一個元素並返回它。
語法
random.randrange(start, stop, step)
引數
start(可選) - 一個整數,指示起始位置。預設為 0
stop(必填) - 一個整數,指示結束位置。
step(可選) - 一個整數,指示增量。預設為 1。
以下程式使用 randrange() 函式返回給定範圍內的隨機數:
import random # generating a random number between 10(included) and 20(not included) print("Random Number Generated = ", random.randrange(10, 20))
輸出
執行上述程式將生成以下輸出:
Random Number Generated = 13
此處使用 randrange() 函式從一個範圍內生成一個隨機數。我們傳遞了起始值(下限)和結束值(上限)作為引數,它生成了這兩個範圍之間的隨機數
方法 3:使用 random.randint() 方法
randint() 方法返回一個整數,表示從給定範圍內隨機選擇的元素。
注意 - randint() 方法是 randrange(start, stop+1) 的別名。
語法
random.randint(start, stop)
引數
start(必填) - 一個整數,指示起始位置。
stop(必填) - 一個整數,指示結束位置。
以下程式使用 randint() 函式返回給定範圍內的隨機數:
import random # generating a random number between 10 and 20(both 10 and 20 numbers included) print("Random Number Generated = ", random.randint(10, 20))
輸出
執行上述程式將生成以下輸出:
Random Number Generated = 20
randrange() 和 randint() 之間的區別在於,randint() 包含範圍內的上限,而 randrange() 不包含上限。我們可以向 randrange() 函式新增步長值,但不能向 randint() 函式新增。
方法 4:使用 random.random()
random() 方法生成 0 到 1 之間的隨機浮點數。
語法
random.random()
引數
此 random() 方法不接受任何引數
以下程式使用 random() 函式返回給定範圍內的隨機數:
import random # generating a random floating-point number between 0 and 1 print("Random Number Generated = ", random.random())
輸出
Random Number Generated = 0.15685132230226662
方法 5:使用 random.choice() 方法
choices() 方法返回一個列表,其中包含從提供的序列中隨機選擇的元素。
序列可以是字串、範圍、列表、元組或其他任何內容。
以下程式返回列表中的隨機數:
import random #Input list given_List = [ 1, 6, 3, 9, 10, 24, 475, 483, 2656] print('The first Random Element from the list:',random.choice(given_List)) print('The Second Random Element from the list:',random.choice(given_List))
輸出
The first Random Element from the list: 6 The Second Random Element from the list: 24
方法 6:使用 random.uniform() 方法
uniform() 方法生成兩個輸入值之間的隨機浮點數(包括這兩個數字)。
語法
random.uniform(x, y)
引數
x(必填) - 表示最低可能結果的數字
y(必填) - 表示最高可能結果的數字
以下程式使用 uniform() 函式返回給定範圍內的隨機浮點數:
import random # generating a random number between 10 and 20(both 10 and 20 are also included) print("Random Number Generated = ", random.uniform(10, 20))
輸出
Random Number Generated = 12.845596876772472
在這種情況下,uniform() 函式用於從一個範圍內生成一個隨機浮點數。我們將其起始值(下限)和結束值(上限)作為引數傳遞,它生成了這兩個範圍之間的隨機浮點數/小數。
結論
在本文中,我們學習瞭如何使用六種不同的方法生成隨機數
seed()、randrange()、randint()、choice()、random()、uniform()。我們還學習瞭如何使用 choice() 函式從列表/字串/元組中獲取隨機元素。