You can download this code by clicking the button below.
This code is now available for download.
This function generates a random string of specified length, with an option to specify the character set.
Technology Stack : random, string
Code Type : Function
Code Difficulty : Intermediate
import random
import os
import json
import math
import re
import sys
import time
import hashlib
import locale
def generate_random_string(length, chars=string.ascii_letters + string.digits):
"""生成指定长度的随机字符串。
Args:
length (int): 随机字符串的长度。
chars (str): 随机字符串中可用的字符集。
Returns:
str: 生成的随机字符串。
"""
return ''.join(random.choice(chars) for _ in range(length))