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, which can include letters and digits.
Technology Stack : random, string
Code Type : Function
Code Difficulty : Intermediate
import random
import string
import math
def generate_random_string(length, characters=string.ascii_letters + string.digits):
"""
Generate a random string of given length.
Args:
length (int): Length of the random string to generate.
characters (str): String of characters to choose from, default is alphanumeric.
Returns:
str: Randomly generated string.
"""
return ''.join(random.choice(characters) for _ in range(length))