Random Password Generator

  • Share this:

Code introduction


This function generates a random password of a specified length consisting of letters and digits.


Technology Stack : math, random, string

Code Type : Function

Code Difficulty : Intermediate


                
                    
import math
import random
import string
import time

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