如何在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中生成不重複的數字。我們學習瞭如何生成不在列表中的不重複數字。我們學習瞭如何確定元素是否包含在列表中以便生成不重複的整數。