As a developer I am sure you have come across the need to generate some random text – most likely for testing load or something similar. In this short article I am going to show you how you can generate random text and random sentences quite quickly.
To start off I am going to create a class called RandomText
. I am going to add a variable of type Random
to the class and initialise it in the constructor. The code for this is below:
using System; using System.Text; namespace RandomTextGenerator { public class RandomText { // Random object to be used for generating random text private Random random; // Constructor public RandomText() { this.random = new Random(); } } }
Random Uppercase Text
Now I am going to add a method called GetRandomTextUpper
to the class. This method accepts an integer parameter which will be used to indicate the size of the word to be generated. Capital letters start at ASCII code 65, and there are 26 letters in the English alphabet, so we need to generate a random number greater or equal to 64 and less or equal to 90. This random number will then be used to get the corresponding ASCII character, and this way we would only be generating random uppercase letters. The code is below:
public string GetRandomTextUpper(int size) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < size; i++) { // Uppercase letters start at ASCII code 65 and there are 26 letters in the alphabet builder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)))); } return builder.ToString(); }
Random Lowercase Text
Now to generate random lowercase letters, we can use the same process as above but start at ASCII character 97, which is a small ‘a’, or we could call the GetRandomTextUpper
method and convert the result to lowercase. I personally prefer the second method :). The code for this is below:
public string GetRandomTextLower(int size) { return this.GetRandomTextUpper(size).ToLower(); }
Random Mixed Text
Below I created a method which generates a random string using both lowercase and uppercase letters. It uses a string of allowed characters to generate the random text, so technically you could put whatever letters, numbers, and symbols you want within that string and the code will still work.
public string GetRandomText(int size) { StringBuilder builder = new StringBuilder(); string legalCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; char character; for (int i = 0; i < size; i++) { character = legalCharacters[random.Next(0, legalCharacters.Length)]; builder.Append(character); } return builder.ToString(); }
Random Sentences
The final method I created for this RandomText
class is the GetRandomSentence
method. This method contains an array ar English words which is then used to build the sentences. The method also capitalises the first letter of the first word in each sentence to make the sentences look more realistic. The code is below:
public string GetRandomSentence(int wordCount) { string[] words = { "an", "automobile", "or", "motor", "car", "is", "a", "wheeled", "motor", "vehicle", "used", "for", "transporting", "passengers", "which", "also", "carries", "its", "own", "engine", "or" }; StringBuilder builder = new StringBuilder(); for (int i = 0; i < wordCount; i++) { // Select a random word from the array builder.Append(words[random.Next(words.Length)]).Append(" "); } string sentence = builder.ToString().Trim() + ". "; // Set the first letter of the first word in the sentenece to uppercase sentence = char.ToUpper(sentence[0]) + sentence.Substring(1); builder = new StringBuilder(); builder.Append(sentence); return builder.ToString(); }
Obviously, the string array of words being used above is too small and the sentences are very limited using this array. Ideally you would obtain a text file full of words from the English dictionary and read from that file when generating random sentences, and that way you would have much richer sentences.
That’s the end of this short article. Happy randomizing :).
Dave
I can recommend http://lipsum.com for the same purpose. Especially when doing for example website layouts. It can create a bunch of text that makes no sense (unless you know latin), but that still looks like regular text.
Great post. Here is a blog post that provides another method of generating random words, sentences and paragraphs:
http://nickstips.wordpress.com/2010/08/26/c-random-text-generator/
The full code can be found here:
http://randomtext.codeplex.com
good post.
Thanks.