Random Password Generator

  • Share this:

Code introduction


This function generates a random password of a specified length, composed of letters, digits, and special characters.


Technology Stack : math, random, re, string, sys

Code Type : Function

Code Difficulty : Intermediate


                
                    
import math
import random
import re
import string
import sys

def generate_random_password(length):
    if not isinstance(length, int) or length <= 0:
        raise ValueError("Password length must be a positive integer")
    
    characters = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.choice(characters) for i in range(length))
    return password