Random Password Generator

  • Share this:

Code introduction


This function is used to generate a random password of a specified length, composed of letters, digits, and special characters.


Technology Stack : datetime, os, re, random, string, sys

Code Type : Generate random password

Code Difficulty : Intermediate


                
                    
import datetime
import os
import re
import random
import string
import sys

def generate_random_password(length=12):
    if not isinstance(length, int) or length < 8:
        raise ValueError("Password length must be an integer greater than or equal to 8")
    
    characters = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.choice(characters) for i in range(length))
    return password