用簡單的方法生成隨機(jī)性較大的密碼
僅用20行代碼隨機(jī)生成密碼
核心思路:利用random模塊
random模塊隨機(jī)生成數(shù)字,大小寫字母,循環(huán)次數(shù)
while循環(huán)+隨機(jī)生成的循環(huán)次數(shù)——>隨機(jī)plus++
大寫字母ASKII碼在65-90之間
小寫字母Askll碼在97-122之間
最終效果: x個(gè)大寫字母+y個(gè)數(shù)字+z個(gè)小寫字母(x,y,z均隨機(jī))
隨機(jī)性相較于以往單調(diào)的 小寫+數(shù)字+大寫+小寫+數(shù)字+大寫… 循環(huán)有所提升
import randomprint("隨機(jī)數(shù)生成”)time=random.randint(1,2) while time: time1=random.randint(1, 3) time2=random.randint(1, 2) time3=random.randint(1, 3) while time1: a= random.randint(65,90) print("%c"%a,end="") time1-=1 while time 2: c= random.randint(0,99) print("%d"%c,end="") time2-=1 while time3: b= random.randint(97,122) print("%c"%b,end="") time 3-=1 time-=1
補(bǔ)充:用Python隨機(jī)生成一個(gè)六位驗(yàn)證碼(驗(yàn)證碼由數(shù)字和字母組成(大小寫字母))
import random 、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、這里要用到random函數(shù)中的隨機(jī)生成一個(gè)區(qū)間的整數(shù) randint 函數(shù)模塊 第一次知道循環(huán)可以這樣用 for _ in range():hhh、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、def generate_code(code_len = 6): all_char = '0123456789qazwsxedcrfvtgbyhnujmikolpQAZWSXEDCRFVTGBYHNUJIKOLP' index = len(all_char) + 1 code = '' for _ in range(code_len): num = random.randint(0,index) code += all_char[num] return code print(generate_code())
總結(jié)
以上所述是小編給大家介紹的python隨機(jī)生成大小寫字母數(shù)字混合密碼(僅20行代碼),希望對(duì)大家有所幫助!
新聞熱點(diǎn)
疑難解答
圖片精選