Random String Generator

  • Share this:

Code introduction


This function generates a random string of a specified length, which is used in cryptography, random testing, and other scenarios.


Technology Stack : os, re, sys, time, math, random, string

Code Type : Function

Code Difficulty : Beginner


                
                    
import os
import re
import sys
import time
import math
import random
import string

def generate_random_string(length):
    """生成指定长度的随机字符串。

    Args:
        length (int): 字符串的长度。

    Returns:
        str: 生成的随机字符串。
    """
    return ''.join(random.choices(string.ascii_letters + string.digits, k=length))