import random
import string
def generate_password(length=16):
characters = string.ascii_letters + string.digits + string.punctuation
password = random.choice(string.ascii_uppercase)
password += random.choice(string.ascii_lowercase)
password += random.choice(string.digits)
password += random.choice(string.punctuation)
for i in range(length - 4):
password += random.choice(characters)
password_list = list(password)
random.shuffle(password_list)
return ''.join(password_list)
password = generate_password()
print("随机密码为:", password)
# 统计不同类型字符的个数
num_uppercase = sum(1 for c in password if c.isupper())
num_lowercase = sum(1 for c in password if c.islower())
num_digits = sum(1 for c in password if c.isdigit())
num_symbols = sum(1 for c in password if c in string.punctuation)
total = num_uppercase + num_lowercase + num_digits + num_symbols
# 计算比例
ratio_uppercase = num_uppercase / total
ratio_lowercase = num_lowercase / total
ratio_digits = num_digits / total
ratio_symbols = num_symbols / total
print("大写字母比例:", ratio_uppercase)
print("小写字母比例:", ratio_lowercase)
print("数字比例:", ratio_digits)
print("符号比例:", ratio_symbols)
[python]随机生成16位密码,至少包含大小写字母,数字,符号,并打印不同字符出现的概率
于 2023-04-30 14:34:14 首次发布