Random Sentence Generator

  • Share this:

Code introduction


Generates a random sentence by selecting random words from the given text.


Technology Stack : Python Standard Library

Code Type : Function

Code Difficulty : Beginner


                
                    
import random

def generate_random_sentence(text, n):
    """
    Generates a random sentence from the given text by selecting random words.
    """
    words = text.split()
    sentence = ' '.join(random.sample(words, n))
    return sentence