// python projects

Random Password Generator in Python

This program is a Random Password Generator designed to enhance online security. By specifying the desired password length, users can create strong passwords that combine lowercase and uppercase letters, numbers, and special symbols.

2021 1 min read pythonsecurity

This program is a Random Password Generator designed to enhance online security. By specifying the desired password length, users can create strong passwords that combine lowercase and uppercase letters, numbers, and special symbols.

import random
import math

alpha = "abcdefghijklmnopqrstuvwxyz"
num = "0123456789"
special = "@#$%&*"

# pass_len = random.randint(8, 13)  # without user input
pass_len = int(input("Enter Password Length"))

# length of password by 50-30-20 formula
alpha_len = pass_len // 2
num_len = math.ceil(pass_len * 30 / 100)
special_len = pass_len - (alpha_len + num_len)


password = []


def generate_pass(length, array, is_alpha=False):
    for i in range(length):
        index = random.randint(0, len(array) - 1)
        character = array[index]
        if is_alpha:
            case = random.randint(0, 1)
            if case == 1:
                character = character.upper()
        password.append(character)


# alpha password
generate_pass(alpha_len, alpha, True)

# numeric password
generate_pass(num_len, num)

# special character password
generate_pass(special_len, special)

# shuffle the generated password list
random.shuffle(password)

# convert list to string
gen_password = ""
for i in password:
    gen_password = gen_password + str(i)
print(gen_password)