Random Password Generator

  • Share this:

Code introduction


Generates a random password with options to include uppercase letters, numbers, and special characters.


Technology Stack : random, string, ValueError

Code Type : Password generation

Code Difficulty : Intermediate


                
                    
import random
import string
import math
import os
import re
import datetime
import hashlib

def generate_random_password(length, use_uppercase=False, use_numbers=False, use_special_chars=False):
    if length < 1:
        raise ValueError("Password length must be at least 1")
    
    characters = string.ascii_lowercase
    if use_uppercase:
        characters += string.ascii_uppercase
    if use_numbers:
        characters += string.digits
    if use_special_chars:
        characters += string.punctuation
    
    return ''.join(random.choice(characters) for i in range(length))