| by Arround The Web | No comments

Random Sentence Generator in Python

Python can be utilized for many applications like web development, data analysis, ML/Machine Learning, and more. It can also be used to generate random sentences using various modules such as random module, secret module, etc. We can also select/choose random elements from a list, tuple, or set in Python and shuffle them to create a completely random sentence.

This post presents a detailed guide on generating random sentences in Python.

How to Generate Random Sentences in Python?

To generate/construct random sentences, the following methods are utilized in Python:

Method 1: Generate Random Sentences Using “random.randint()” Method

The “random.randint()” method of the “random” module is used to generate/create a random integer number from the specified range. This method is used along with square bracket notation to generate random sentences.

Syntax

random.randint(start, stop)

In the above syntax, the “start” and “stop” parameters indicate the starting and ending range.

Example

To demonstrate the working of this method, take the following example:

from random import randint

l1=['Python', 'Java', 'C++', 'JavaScript']

l2=['Language', 'Code', 'Program', 'Syntax']

l3=["is easy.", "is difficult", "is hard", "is complex"]

data1 = l1[randint(0,len(l1)-1)]

data2 = l2[randint(0,len(l2)-1)]

data3 = l3[randint(0,len(l3)-1)]

print(data1 +" "+ data2 +" "+ data3)

In the above code:

  • The “randint” method is imported from the built-in “random” module.
  • The Python list named “l1”, “l2”, and “l3”, containing user-defined random strings data, are initialized in the program.
  • The “randint()” method takes the start and end range of the given lists as an argument and returns the random value.
  • The “randint()” method is used multiple times to select random data and assign it to a variable named “data1”, “data2”, and “data3”.
  • The random strings are concatenated at the end and display the random sentence.

Output

Method 2: Generate Random Sentences Using “essential-generators” Module

The “essential-generators” is a built-in Python “module” that is utilized to create random sentences. It can also be employed to create/construct random words, paragraphs, etc.

To install this module, type the below command in the CMD terminal:

pip install essential_generators

Executing the above command will generate the following output:

Example

Here is an example code that demonstrates the working of this method:

from essential_generators import DocumentGenerator

generate = DocumentGenerator()

print(generate.sentence())

In the above code:

  • The “DocumentGenerator()” method is used to generate random documents containing words, paragraphs, and sentences.
  • The “generate.sentence()” is used to get the random sentence from the randomly generated document.

Output

Method 3: Generate Random Sentences Using “random.choice()” Method

The “random.choice()” method is used to select a random item/element from the input sequence. This method is utilized to generate random sentences in Python.

Syntax

random.choice(sequence)

Here, the sequence parameter specifies any sequence object such as a list, tuple, or set.

Example

The below example is used to generate random sentences using the “choice()” method:

import random

l1=['Python', 'Java', 'C++', 'JavaScript']

l2=['Language', 'Code', 'Program', 'Syntax']

l3=["is easy.", "is difficult", "is hard", "is complex"]

print(random.choice(l1) +" "+ random.choice(l2) +" "+ random.choice(l3))

In this code:

  • The “random” module is imported.
  • The list of strings containing random data is initialized in the program.
  • The “random.choice()” method is used multiple times to get random elements from the specified list.
  • Lastly, all the random elements are concatenated using the “+” operator to generate the sentence.

Output

Method 4: Generate Random Sentences Using “secret.choice()” Method

The “secret.choice()” method takes the sequence as an argument and selects the random item/element from the sequence. This method is utilized to generate random sentences in Python.

Syntax

secret.choice(sequence)

In the above syntax, the “sequence” parameter specifies the list, set, or tuple.

Example

The following example generates the random sentences using the “secret.choice()” method:

import secrets

l1=['Python', 'Java', 'C++', 'JavaScript']

l2=['Language', 'Code', 'Program', 'Syntax']

l3=["is easy.", "is difficult", "is hard", "is complex"]

print(secrets.choice(l1) +" "+ secrets.choice(l2) +" "+ secrets.choice(l3))

In the above code:

  • The “secrets” module is imported.
  • The “secrets.choice()” method of the “secret” module is used multiple times to select random items from the specified list.
  • The random elements are concatenated at the end to generate the random sentence.

Output

That’s all about generating random sentences in Python.

Conclusion

The “random.randint()”, “essential-generators”, “random.choice()”, and “secret.choice()” methods are used to generate random sentences in Python. The inbuilt “essential-generators” module is used to generate random sentences by using the “DocumentGenerator()” method along with the “sentence()” method. The “random.randint()”, “random.choice()”, and “secret.choice()” methods take the random element from the sequence and concatenate them to generate random sentences. This guide presented a complete overview of generating random sentences using several examples.

Share Button

Source: linuxhint.com

Leave a Reply