0

Through trial and error I ended on this please assist me. A random generator that produces 5 random letters to a text box and all the letters must have their own random chance. I am looking for a cleaner way to write this random letter generator. Perhaps a different way to use the Random().

       private void GenerateLetter_Click(object sender, EventArgs e)
    {       Random rnd = new Random(); 
            String[] rArray = { "A", "B", "C", "D", "E", "F", "G" };            
            int x1 = rnd.Next(0,7);
            int x2 = rnd.Next(0, 7);
            int x3 = rnd.Next(0, 7);
            int x5 = rnd.Next(0, 7);
            int x6 = rnd.Next(0, 7);
            int x7 = rnd.Next(0, 7);
            int x8 = rnd.Next(0, 7);
            int x9 = rnd.Next(0, 7);
            int x10 = rnd.Next(0, 7);
            int x11 = rnd.Next(0, 7);
            int x12 = rnd.Next(0, 7);
            int x13 = rnd.Next(0, 7);
            int x14 = rnd.Next(0, 7);
            int x15 = rnd.Next(0, 7);
            int x16 = rnd.Next(0, 7);
            int x17 = rnd.Next(0, 7);
            int x18 = rnd.Next(0, 7);
            int x19 = rnd.Next(0, 7);
            int x20 = rnd.Next(0, 7);

            textBox1.Text =  rArray[x1] + rArray[x2] + rArray[x3] + rArray[x4] + rArray[x5];
            textBox2.Text =  rArray[x6] + rArray[x7] + rArray[x8] + rArray[x9] + rArray[x10];
            textBox3.Text =  rArray[x11] + rArray[x12] + rArray[x13] + rArray[x14] + rArray[x15];
            textBox4.Text =  rArray[x16] + rArray[x17] + rArray[x18] + rArray[x19] + rArray[x20];            
    } 
  • 1
    possible duplicate of [Random String Generator Returning Same String](http://stackoverflow.com/questions/1122483/random-string-generator-returning-same-string). Quite a few answers were posted there, you should be able to find a suitable one. And here is an algorithm [Fisher–Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) – oleksii Jan 20 '15 at 12:02
  • use a for loop to generate new characters. there's a good bit of unneeded duplicate code there – user1666620 Jan 20 '15 at 12:04
  • See this. there is a getletter function that returns a random letter. http://www.dotnetperls.com/random-lowercase-letter – e4rthdog Jan 20 '15 at 12:04
  • Use a for/while loop and abstract the code to a function (similar code for all the 4 textboxes) for a start? – gsharp Jan 20 '15 at 12:06

3 Answers3

3
static readonly Random Rand = new Random();
private const string Alpha = "ABCDEFG";

 public static string GenerateAlphaString(int size)
        {
            var chars = new char[size];
            for (int i = 0; i < size; i++)
            {
                chars[i] = Alpha[Rand.Next(Alpha.Length)];
            }
            return new string(chars);
        }

Then call that method:

textBox1.Text = GenerateAlphaString(5);
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
0

A static method to give you a random letter:

using System;

static class RandomLetter
{
    static Random _random = new Random();
    public static char GetLetter()
    {
    int num = _random.Next(0, 26); // Zero to 25
    char let = (char)('a' + num);
    return let;
    }
}

Use:

Console.WriteLine(RandomLetter.GetLetter());
e4rthdog
  • 5,103
  • 4
  • 40
  • 89
0

This code will do exactly the same as the code you provided:

private static Random rand = new Random();
private static string alphabet = "ABCDEFG";
private static string GetRandomString(int length)
{
    var stringBuilder = new StringBuilder();
    for (int i = 0; i < length; i++)
    {
        stringBuilder.Append(alphabet[rand.Next(0, alphabet.length)]);
    }
    return stringBuilder.ToString();
}

private void GenerateLetter_Click(object sender, EventArgs e)
{
    textBox1.Text = GetRandomString(5);
    textBox2.Text = GetRandomString(5);
    textBox3.Text = GetRandomString(5);
    textBox4.Text = GetRandomString(5);         
}
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123