python生成隨機密碼:random庫
阿新 • • 發佈:2018-11-02
要求:
(1)使用random庫,採用0x1010作為隨機種子。
(2)密碼由26個字母的大小寫,10個數字字元和!@#¥%……&*等8個字元組成。
(3)每個密碼長度固定在10個字元。
(4)程式執行每次長生20個密碼,每個密碼一行。
(5)每次長生的20個密碼儲存在“隨機密碼.txt”檔案中
import random random.seed(0x1010) #設定隨機種子數 #設定種子選擇空間 s = "[email protected]#$%^&*" ls = [] #存取密碼的列表 FirstPsw = "" #存取第一個密碼的字元 while len(ls)<20: #十個隨機密碼 pwd = "" for i in range(10): pwd += s[random.randint(0,len(s)-1)] if pwd[0] in FirstPsw: continue else: ls.append(pwd) FirstPsw +=pwd[0] fo = open("隨機密碼.txt","w",encoding ="utf-8") fo.write("\n".join(ls)) fo.close()
執行結果: