Category Archives: Testing

Generating random phrases

Introduction

Some times when I am testing applications I have need random sentences or text files to be generated on the fly of various lengths.  This post will show how one way to do that.

Generating the Sentences

To generate a sentence the following steps need to be taken.

  1. Split a list of words
  2. Take a word at random from the list and put in collection.
  3. Repeat step 2 until you have enough words for you sentence.
  4. Repeat steps 2 and 3 for number of sentences and paragraphs that you require.

A more detailed description generating test sentences can be found at http://www.dotnetperls.com/random-paragraphs-sentences

Usage

I have published a package on NUGET for ease of use at https://www.nuget.org/packages/System.Diagnostics.TestData/

var target = new RandomText();
const int numberParagraphs = 1;
const int minSentences = 4;
const int maxSentences = minSentences;
const int minWords = 4;
const int maxWords = minWords;
target.AddContentParagraphs(numberParagraphs, minSentences, maxSentences, minWords, maxWords);
//Get your randomly generated phrase from here
string randomPhrase = target.Content;
References

http://www.dotnetperls.com/random-paragraphs-sentences – where the core part of the code came from.

 

 

Advertisements