Random String Generator

  • Share this:

Code introduction


The function generates a random string of a specified length, consisting of letters and digits.


Technology Stack : String (string), random choice (random.choices)

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import string
import datetime
import os
import sys
import re
import json
import math

def generate_random_string(length=10):
    if not isinstance(length, int) or length <= 0:
        raise ValueError("Length must be a positive integer")
    return ''.join(random.choices(string.ascii_letters + string.digits, k=length))