Home
C#
Random Paragraphs and Sentences
Updated Feb 9, 2025
Dot Net Perls
Random paragraphs. It is possible to generate random paragraphs and sentences. The text must look like regular English writing. A C# method can generate language that appears natural.
This code can be used for testing or debugging purposes. It is also an interesting exercise in how to create random sequences of data.
Random String
Random Lowercase Letter
Example. This class contains the logic for the random text generator. It has an AddContentParagraphs method. And we can fetch the resulting string from the Content property.
Property
Part 1 We specify the options first in the constructor, which accepts the list of words to use.
Part 2 This uses the Random instance's Next method. It adds the paragraph. After the paragraph, it inserts 2 line breaks.
Random
StringBuilder
Part 3 The AddParagraph method calls AddSentence once per each sentence required.
Part 4 AddSentence iterates up to the word count. It randomly selects each word from the words array and appends it.
StringBuilder Append
Part 5 We create an instance of the RandomText class, and then call AddContentParagraphs to generate output.
using System; using System.Text; class RandomText { static Random _random = new Random(); StringBuilder _builder; string[] _words; // Part 1: the constructor. public RandomText(string[] words) { _builder = new StringBuilder(); _words = words; } public void AddContentParagraphs(int numberParagraphs, int minSentences, int maxSentences, int minWords, int maxWords) { // Part 2: call Next() on random number generator for each paragraph. 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) { // Part 3: call AddSentence repeatedly in AddParagraph. for (int i = 0; i < numberSentences; i++) { int count = _random.Next(minWords, maxWords + 1); AddSentence(count); } } void AddSentence(int numberWords) { // Part 4: generate a single sentence. 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)]); b.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(); } } } class Program { static void Main() { // Part 5: create a RandomText instance and generate text with it. string[] words = { "cat", "house", "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); } }
For bird house a house fox cat. House the the man a a bird cat a man for cat. Bird fox for for the a with a cat man. A with the fox cat with bird house.
Summary. The class uses object-oriented design methodology and outputs variable-length sentences and paragraphs. We can specify any number of possible words.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Feb 9, 2025 (rewrite).
Home
Changes
© 2007-2025 Sam Allen