Random Password Generator

  • Share this:

Code introduction


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


Technology Stack : random, string, re

Code Type : Generate random password

Code Difficulty : Intermediate


                
                    
import random
import string
import re
import math
import cmath
import collections
import datetime
import unittest
import hashlib
import json

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