Random String Generator

  • Share this:

Code introduction


This function generates a random string of a specified length, used for generating unique identifiers or random passwords, etc.


Technology Stack : os, re, json, time, math, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import re
import json
import time
import math
import random

def generate_random_string(length):
    """
    生成一个指定长度的随机字符串。
    
    :param length: 字符串长度
    :return: 随机字符串
    """
    letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    return ''.join(random.choice(letters) for i in range(length))