Random Word Generator Function

  • Share this:

Code introduction


This function uses the random string generator from the Hypothesis library to create a sentence consisting of three random words. The first parameter `min_length` defines the minimum length of the word, with a default value of 5; the second parameter `max_length` defines the maximum length of the word, with a default value of 10.


Technology Stack : Hypothesis

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_word_generator(min_length=5, max_length=10):
    import random
    import string

    def generate_word(length):
        return ''.join(random.choices(string.ascii_lowercase, k=length))

    words = [generate_word(random.randint(min_length, max_length)) for _ in range(3)]
    return ' '.join(words)                
              
Tags: