6

I am in need of generating a random string with spaces and mixedCase.

This is all I got so far:

    /// <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
    {
        /// <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();
            Random random = new Random();
            char ch;

            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }
    }

I am getting only uppercase strings with no spaces - I believe the tweak should be pretty striaghtforward to get mixed case and spaces in the soup.

Any help greatly appreciated!

JohnIdol
  • 48,899
  • 61
  • 158
  • 242

3 Answers3

11

The easiest way to do this is to simply create a string with the following values:

private readonly string legalCharacters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

Then use the RNG to access a random element in this string:

public string TypeAway(int size)
{
    StringBuilder builder = new StringBuilder();
    Random random = new Random();
    char ch;

    for (int i = 0; i < size; i++)
    {
        ch = legalCharacters[random.Next(0, legalCharacters.Length)];
        builder.Append(ch);
    }

    return builder.ToString();
}
John Rasch
  • 62,489
  • 19
  • 106
  • 139
  • What your example does is exactly the same thing as what I do because it's picking a Unicode character value between 65 and 90 (inclusive). If you want it even "more random" (define that however you'd like), you're going to need a "better" PRNG than what .NET provides. – John Rasch Mar 25 '09 at 22:07
  • I'll give it a shot and let you know - I like it anyway, thanks! – JohnIdol Mar 25 '09 at 22:08
  • Nothing gets more random by randomizing twice. This is a good option if you want every character in the string appear with equal probability. (But you need to add the space character to legalCharacters, and you'll get two consecutive spaces with 1/2809 probabiity). – Pontus Gagge Mar 25 '09 at 22:10
  • The space character is the 1st element – John Rasch Mar 25 '09 at 22:12
  • it's working like charme - I need it to generate a population of random strings to be evolved into a target string by a genetic algorithm. – JohnIdol Mar 25 '09 at 22:14
  • If you want "more random", you could always fill an array of bytes using System.Security.Cryptography.RNGCryptoServiceProvider. – Jim Mischel Mar 25 '09 at 22:31
  • Tnx - I'll see what happens with what I have now - in case I will struggle to get the results I expect I'll switch over to the Cryptography stuff – JohnIdol Mar 26 '09 at 00:19
1

You could start with an array of all the characters you'll allow

private static readonly char[] ALLOWED = new [] { 'a', 'b', 'c' ... '9' };

And then:

{
    ...
    for (int i = 0; i < size; i++)
    {
        ch = ALLOWED[random.NextInt(0, ALLOWED.Length)];
        builder.Append(ch);
    }

    ...

    return builder.ToString();
}

return builder.ToString();

I paraphrase, of course. I'm not certain about the syntax on random.NextInt(), but intelisense aught to help.

dustyburwell
  • 5,755
  • 2
  • 27
  • 34
0

You can also use Lorem Ipsum. It is widely used in the graphic design industry to fill in for random, realistic text without distracting the user from the design elements.

You can copy and paste a big chunk of the Lorem Ipsum into a constant string in your code and then just substring it into whatever sizes you need.

I found this was better than having completely random text since it was too distracting.

Hope that helps.

Rezlaj
  • 244
  • 1
  • 3
  • thanks but I need it to generate a population of random strings to be evolved into a target string by a genetic algorithm – JohnIdol Mar 26 '09 at 00:20