如何在 Python 中生成不重複的隨機數?
在這篇文章中,我們將向您展示如何在 Python 中生成不重複的隨機數。以下是完成此任務的方法
使用 randint() 和 append() 函式
使用給定列表的 random.sample() 方法
使用數字範圍的 random.sample() 方法
使用 random.choices() 方法
使用 randint() 和 append() 函式
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用 import 關鍵字匯入 random 模組。
建立一個空列表,作為結果隨機數列表。
使用 for 迴圈 遍歷迴圈 15 次。
使用 random 模組的 randint() 函式(在指定範圍內返回一個隨機數),在指定範圍內生成一個隨機數,即從 1 到 100。
使用 if 條件語句 檢查生成的隨機數是否不在結果 randomList 中,使用 not 和 in 運算子 (Python 成員運算子)。
如果條件為 True,則使用 append() 函式(在列表末尾新增元素)將隨機數追加到結果列表。
列印結果 隨機數列表
示例
以下程式使用 randint()、append() 函式返回不重複的隨機數:
# importing random module import random # resultant random numbers list randomList=[] # traversing the loop 15 times for i in range(15): # generating a random number in the range 1 to 100 r=random.randint(1,100) # checking whether the generated random number is not in the # randomList if r not in randomList: # appending the random number to the resultant list, if the condition is true randomList.append(r) # printing the resultant random numbers list print("non-repeating random numbers are:") print(randomList)
輸出
執行上述程式後,將生成以下輸出:
non-repeating random numbers are: [84, 86, 90, 94, 59, 33, 58, 36, 62, 50, 26, 38, 4, 89]
使用給定列表的 random.sample() 方法
random.sample() 方法返回一個列表,其中包含從序列中隨機選擇的指定數量的元素。
語法
random.sample(sequence, k)
引數
sequence(必需) - 任何序列,如列表、元組等
k(可選) - 返回列表的長度,表示為整數
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用 import 關鍵字匯入 random 模組。
建立一個變數來儲存輸入列表。
使用 set() 函式(返回可迭代物件中所有不同的項,並將可迭代物件轉換為集合),從輸入列表中刪除重複的元素。
使用 list() 函式(將序列/可迭代物件轉換為列表),將上述集合轉換為列表。現在列表中只有唯一元素。
透過將包含唯一項的列表和 k 值作為引數傳遞給 sample() 函式,列印列表中 k(此處為 4)個不重複的隨機數。
示例
以下程式使用 random.sample() 函式返回 4 個不重複的隨機數:
# importing random module import random # input list inputList = [1, 2, 3, 1, 4, 3, 5, 7, 9, 8, 2, 3] # removing repeating elements from the list using the set() function resultSet=set(inputList) # converting the set into a list(now the list has only unique elements) uniqueList =list(resultSet) # printing 4 random numbers from the list which is non-repeating print("4 non-repeating random numbers from the list are:") print(random.sample(uniqueList, 4))
輸出
執行上述程式後,將生成以下輸出:
4 non-repeating random numbers from the list are: [7, 2, 4, 8]
使用數字範圍的 random.sample() 方法
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用 import 關鍵字匯入 random 模組。
使用 range() 函式(range() 函式返回一個從 0 開始,增量為 1(預設)並在給定數字之前停止的數字序列)獲取指定範圍內的數字,例如,此處為 1 到 100。
透過將給定的數字範圍列表和 k 值作為引數傳遞給 sample() 函式,列印列表中 k(此處為 4)個不重複的隨機數。
示例
以下程式使用 random.sample() 函式返回 4 個不重複的隨機數:
# importing random module import random # getting numbers from 0 to 100 inputNumbers =range(0,100) # printing 4 random numbers from the given range of numbers which are # non-repeating using sample() function print("4 non-repeating random numbers are:") print(random.sample(inputNumbers, 4))
輸出
執行上述程式後,將生成以下輸出:
4 non-repeating random numbers are: [67, 50, 61, 47]
使用 random.choices() 方法
random 模組包含 random.choices() 方法。它可用於從列表中選擇多個項或從特定序列中選擇單個項。
語法
random.choices(sequence, k)
引數
示例
以下程式使用 random.choices() 函式返回 4 個不重複的隨機數:
# importing random module import random # getting numbers from 0 to 100 inputNumbers =range(0,100) # printing 4 random numbers from the given range of numbers which are # non-repeating using choices() function print("4 non-repeating random numbers are:") print(random.choices(inputNumbers, k=4))
輸出
執行上述程式後,將生成以下輸出:
4 non-repeating random numbers are: [71, 4, 12, 21]
結論
在本教程中,我們學習瞭如何使用四種不同的方法在 Python 中生成不重複的數字。我們學習瞭如何生成不在列表中的不重複數字。我們學習瞭如何確定元素是否包含在列表中,以便生成不重複的整數。