Random String Generator

  • Share this:

Code introduction


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


Technology Stack : random, string

Code Type : Generate random strings

Code Difficulty :


                
                    
import random
import string
import math
import os
import time
import datetime
import re

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