Random String Generator

  • Share this:

Code introduction


This function is used to generate a random string of a specified length and is suitable for password generation, test data generation, and other scenarios.


Technology Stack : random, string

Code Type : Generate random strings

Code Difficulty : Intermediate


                
                    
import random
import string
import re
import math
import time
import datetime
import cmath
import operator
import inspect
import sys
import os

def generate_random_string(length):
    """
    生成指定长度的随机字符串
    """
    if not isinstance(length, int) or length < 0:
        raise ValueError("Length must be a non-negative integer")
    return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))                
              
Tags: