Random String Generator

  • Share this:

Code introduction


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))                
              
Tags: