Random Password Generator

  • Share this:

Code introduction


This function uses the random module from the Passlib library to generate a random password of specified length. The password consists of uppercase and lowercase letters and digits.


Technology Stack : Passlib, random, string

Code Type : Password generating function

Code Difficulty : Intermediate


                
                    
def generate_random_password(length=8):
    from random import choice
    from string import ascii_letters, digits

    if not isinstance(length, int) or length < 6:
        raise ValueError("Password length should be an integer greater than or equal to 6")

    characters = ascii_letters + digits
    password = ''.join(choice(characters) for i in range(length))
    return password