Random Password Generator

  • Share this:

Code introduction


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


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

Code Type : Function

Code Difficulty : Intermediate


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

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