Skip to content

Instantly share code, notes, and snippets.

@gidili
Created April 13, 2009 18:21
Show Gist options
  • Save gidili/94600 to your computer and use it in GitHub Desktop.
Save gidili/94600 to your computer and use it in GitHub Desktop.
TypingMonkey to generate random strings
/// <summary>
/// The Typing monkey generates random strings - can't be static 'cause it's a monkey.
/// </summary>
/// <remarks>
/// If you wait long enough it will eventually produce Shakespeare.
/// </remarks>
class TypingMonkey
{
private const string legalCharacters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.";
static Random random = new Random();
/// <summary>
/// The Typing Monkey Generates a random string with the given length.
/// </summary>
/// <param name="size">Size of the string</param>
/// <returns>Random string</returns>
public string TypeAway(int size)
{
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++)
{
ch = legalCharacters[random.Next(0, legalCharacters.Length)];
builder.Append(ch);
}
return builder.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment