Random Password Generator

  • Share this:

Code introduction


This function is used to generate a random password of specified length, which includes both uppercase and lowercase letters, numbers, and special characters.


Technology Stack : Built-in libraries: string, random

Code Type : Generate random password

Code Difficulty : Intermediate


                
                    
import math
import random
import string
import sys
import os
import json
import datetime

def generate_random_password(length=8):
    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 + "!@#$%^&*()_+"
    password = ''.join(random.choice(characters) for i in range(length))
    return password