Random Word Generator with Length Range

  • Share this:

Code introduction


This function accepts two parameters, min_length and max_length, to generate a random lowercase letter word within a specified length range. The function uses the random and string modules internally to generate a random word, and logs the generated word using the Loguru library.


Technology Stack : Python, random, string, loguru

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_word_generator(min_length, max_length):
    import random
    import string
    import loguru

    logger = loguru.getLogger()
    
    def generate_word(length):
        return ''.join(random.choices(string.ascii_lowercase, k=length))
    
    word_length = random.randint(min_length, max_length)
    random_word = generate_word(word_length)
    
    logger.info(f"Generated random word: {random_word}")
    
    return random_word

# JSON representation