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 : random, string

Code Type : Password generator

Code Difficulty : Intermediate


                
                    
import random
import string
import datetime
import re
import math
import operator
import sys
import os
import shutil

def generate_random_password(length=8):
    if not isinstance(length, int) or length < 1:
        raise ValueError("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                
              
Tags: