Random Password Generator

  • Share this:

Code introduction


This function generates a random password of specified length, consisting of both uppercase and lowercase letters and digits.


Technology Stack : os, json, re, random, string, sys

Code Type : Generate random password

Code Difficulty : Intermediate


                
                    
import os
import json
import re
import random
import string
import sys

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