C# Random Paragraphs and Sentences

The C# programming language

You want to generate completely random paragraphs and sentences of variable lengths in your C# program. The text must look like regular English writing, but is word salad. This is for testing or for spam detection.

This C# article implements a class that randomizes sentences and paragraphs.

Usage

First, you must create the class instance with the constructor, which accepts the list of strings that comprise the words you want to insert into the text. For my simple demo here, I use very simple words and few of them. You can use any string array you want.

Program that uses random text generator [C#]

using System;

class Program
{
    static void Main()
    {
	string[] words = { "anemone", "wagstaff", "man", "the", "for",
	    "and", "a", "with", "bird", "fox" };
	RandomText text = new RandomText(words);
	text.AddContentParagraphs(2, 2, 4, 5, 12);
	string content = text.Content;
	Console.WriteLine(content);
    }
}

Output

And the a anemone for bird. Anemone with man and man the and with fox fox the.

With the for man the. Anemone with the a wagstaff and man for fox. A anemone
a bird anemone anemone anemone bird wagstaff a bird.

Description. The above code will generate two paragraphs that contain between two and four sentences. Each sentence will contain between 5 and 12 words. This is the random output it generated for me.

Notes

Note

First, the required output here is very similar to spamming programs, but it is entirely implemented in C# and simple for you to use. This code is not for spamming but for generating testcases for your software.

Paragraphs. The class developed here outputs text that has a certain number of paragraphs, separated by newlines. Within those paragraphs, there are random numbers of sentences. And within those sentences, there is a random number of words.

How to specify the length. The code accepts a first parameter that indicates the desired number of paragraphs. The next two int parameters indicate the minimum number of sentences and the maximum number of sentences in the paragraphs. The final two parameters specify the maximum number and minimum number of words in each sentence.

Class implementation

Here I show the class that contains the logic for the random text generator. You can see that it has an AddContentParagraphs method and you can fetch the resulting string from the Content property. It could be enhanced for your purposes by adding additional logic. Add it to the "RandomText.cs" file.

Class for generating text [RandomText.cs C# file]

public class RandomText
{
    static Random _random = new Random();
    StringBuilder _builder;
    string[] _words;

    public RandomText(string[] words)
    {
	_builder = new StringBuilder();
	_words = words;
    }

    public void AddContentParagraphs(int numberParagraphs, int minSentences,
	int maxSentences, int minWords, int maxWords)
    {
	for (int i = 0; i < numberParagraphs; i++)
	{
	    AddParagraph(_random.Next(minSentences, maxSentences + 1),
			 minWords, maxWords);
	    _builder.Append("\n\n");
	}
    }

    void AddParagraph(int numberSentences, int minWords, int maxWords)
    {
	for (int i = 0; i < numberSentences; i++)
	{
	    int count = _random.Next(minWords, maxWords + 1);
	    AddSentence(count);
	}
    }

    void AddSentence(int numberWords)
    {
	StringBuilder b = new StringBuilder();
	// Add n words together.
	for (int i = 0; i < numberWords; i++) // Number of words
	{
	    b.Append(_words[_random.Next(_words.Length)]).Append(" ");
	}
	string sentence = b.ToString().Trim() + ". ";
	// Uppercase sentence
	sentence = char.ToUpper(sentence[0]) + sentence.Substring(1);
	// Add this sentence to the class
	_builder.Append(sentence);
    }

    public string Content
    {
	get
	{
	    return _builder.ToString();
	}
    }
}

Above class. You will specify the options first in the constructor, which accepts the list of words you want to use. Your program could extract these words from a web page or database.

AddContentParagraphs. This function is where you must specify the numeric ranges for your desired output. Internally, it uses the Random instance's Next method, and then adds the paragraph. After the paragraph, it inserts two line breaks.

Private methods. The two private methods in the class simply add a variable number of sentences and then the sentence itself.

Private Method

Sentence logic. The AddSentence method is the most interesting. It loops through the number of words you want to generate. It randomly selects each word from the words array you specified. It appends a space after each word.

More sentence logic. The AddSentence method then trims the output and adds a period at the end of your sentence. Then, it uses the char.ToUpper method to capitalize the first letter of the sentence. It appends the sentence to the internal buffer.

Summary

We saw how you can generate random text with the C# programming language. The class uses the object-oriented design methodology and outputs variable-length sentences and paragraphs. You can specify any number of possible words to use. The example screenshot shows how I simulated a spam generator, which is trivial to do with this code.

Algorithms
.NET