Generating a random string of letters

Hi, this is probably a stupid question, but I cant seem to find the answer. My problem is this:

I want to make a program that generates a random string of letters when the user clicks a button (I'm going to use it as a sort of name generator). I was thinking of maby having a string variable, and then a loop that generates a random letter and adds it to the string, until the string is at the desired length. My main problem will probably be getting it to only generate letters of the alphabet.

Anyone know how




Answer this question

Generating a random string of letters

  • dstorch

    After seeing how random the strings produced actually are, this seems like a very good idea. Would you be able to give me an example as to how I would do this

    I've got a constant with the vowels, and one with all the other letters, and two generate functions that put together strings (from the above posts), but how do I make a string that alternates between one and the other



  • juergen.b

    That could be greatly simplified to:

    public class NameGenerator
    {
    const string _characters ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    public NameGenerator()
    { }

    Generate() would be the same, except _characters.Count would now be _characters.Length, although that could even be cut down a bit.

    public string Generate(int lenght)
    {
    Random random = new Random();
    StringBuilder buffer = new StringBuilder(lenght);
    for (int i = 0; i < lenght; i++)
    {
    int randomNumber = random.Next( 0, _characters.Lenght);
    buffer.Append(_characters, randomNumber, 1);
    }
    return buffer.ToString();
    }



  • rene schrieken

    If the names still look too weird for you, take a look at some of the links from this page for inspiration algorithm-wise. Some of the sites are just funny, but there are some serious ones on there too. You may even find some source code there (but probably not C#).

  • Tamim Sadikali

    If you want to generate names that are pronouncable, you're going to want to make sure that you alternate vowels between consonants. So you might want one list that will be the vowels, and one list that will be the consonants and consonant clusters. Try searching google for more information--I'm pretty sure you can find some good information about this topic.

  • hanafy

    I'll update my version:

     



    public string Generate(int lenght)
        {
            Random random = new Random();
            StringBuilder buffer = new StringBuilder(lenght);
            while(length > 0)
            {
                int randomNumber = random.Next( 0, _constants.Lenght);
                buffer.Append(_constants, randomNumber, 1);
                --length;
                if (length == 0)
                     break;

                randomNumber = random.Next( 0, _vowels.Lenght);
                buffer.Append(_vowels, randomNumber, 1);
                --length;
            }
            return buffer.ToString();
        }


     

    Of course, if these are names, you'd want the first character to be capitalized:

     



    public string Generate(int lenght)
        {

            if (length < 1)
                  throw new ArgumentException();
            Random random = new Random();
            StringBuilder buffer = new StringBuilder(lenght);
            int randomNumber = random.Next( 0, _uppercase.Lenght);
            buffer.Append(_uppercase, randomNumber, 1);
            --length;


            while(length > 0)
            {
                randomNumber = random.Next( 0, _vowels.Lenght);
                buffer.Append(_vowels, randomNumber, 1);
                --length;

                if (length == 0)
                     break;
                randomNumber = random.Next( 0, _lowercase.Lenght);
                buffer.Append(_lowercase, randomNumber, 1);
                --length;
            }
            return buffer.ToString();
        }


     



  • Hammo

    Alternatively, you can also use the GetChars() method provided by AsciiEncoding class in the System.Text. This method allows you to convert a byte array a Char array.

    http://msdn2.microsoft.com/en-us/library/system.text.asciiencoding.getchars.aspx

    This method allow conversion in one statment.



  • dcaton

    Here is a simple class that generates string that are build of letters and numbers:


    public class NameGenerator
    {
    List<char> _characters;
    public NameGenerator()
    {
    _characters = new List<char>();
    // Fill character list with A-Z.
    for (int i = 65; i <= 90; i++)
    {
    _characters.Add((char)i);
    }
    // Fill character list with a-z.
    for (int i = 97; i <= 122; i++)
    {
    _characters.Add((char)i);
    }
    // Fill character list with 0-9.
    for (int i = 48; i <= 57; i++)
    {
    _characters.Add((char)i);
    }
    }
    public string Generate(int lenght)
    {
    Random random = new Random();
    StringBuilder buffer = new StringBuilder(lenght);
    for (int i = 0; i < lenght; i++)
    {
    int randomNumber = random.Next( 0, _characters.Count );
    char randomChar = _characters[ randomNumber ];
    buffer.Append(randomChar);
    }
    return buffer.ToString();
    }
    }



  • prog.gabi

    Thankyou! that seems to work quite well!

    Many thanks to all who have posted in this topic!



  • RyanB88

    Hi,

    use the Random object with Random.Next(minValue, maxValue) to get random numbers between two values. Use the ascii value of a for the minValue, and the ascii value of z as the maxValue. All generated numbers will be ascii numbers of the characters between a and z. Add the characters to the string variable you defined in the generate method. You can also include A .. Z or 0 .. 9. If you do that, the generate methode will get somewhat more complex.

    Good luck!

  • Generating a random string of letters