locked
Random Letter generator

    Question

  • Hello Everyone, 

    Thank you ahead of time for taking time to help me out. 

    The problem I am having is...I'm new. 

    I am using Microsoft Visual C# 2010 Express, and what I am attempting to do is create a random letter generator for the alphabet. I have 26 text boxes, one for each letter of the alphabet; I am trying to get each letter to randomly go in each text box; however, I am getting more than one of the letters throughout the 26 boxes; or I get them to all have the same letter. The way I have it setup is to generate the letters after pressing a button.

    How do I get all 26 letters of the alphabet to randomly show up in all 26 text boxes? 

    The code I have so far (this is the shortened version but is setup for all 26 letters; textbox1.text - textbox26.text): 

                

    private void button1_Click(object sender, EventArgs e)
            {
                String[] strLabel = {
                                            "A","B","C","D","E","F","G","H","I","J","K","L","M",
                                            "N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                                    };

                Random r = new Random();

                int iSelect1 = r.Next(0, 26); textBox1.Text = strLabel[iSelect1];

                int iSelect2 = r.Next(0, 26); textBox2.Text = strLabel[iSelect2];

                int iSelect3 = r.Next(0, 26); textBox3.Text = strLabel[iSelect3];

                int iSelect4 = r.Next(0, 26); textBox4.Text = strLabel[iSelect4];

                int iSelect5 = r.Next(0, 26); textBox5.Text = strLabel[iSelect5];

                int iSelect6 = r.Next(0, 26); textBox6.Text = strLabel[iSelect6];

                int iSelect7 = r.Next(0, 26); textBox7.Text = strLabel[iSelect7]; 

    Sunday, October 14, 2012 9:39 AM

Answers

  • ...

            private void button1_Click(object sender, EventArgs e)
            {
                List<char> l = new List<char>();
                for (int i = 65; i < 91; i++)
                    l.Add((char)i);
    
                int iSelect1 = _rnd.Next(0, 26); textBox1.Text = l[iSelect1].ToString(); l.RemoveAt(iSelect1);
                int iSelect2 = _rnd.Next(0, 25); textBox2.Text = l[iSelect2].ToString(); l.RemoveAt(iSelect2);
                int iSelect3 = _rnd.Next(0, 24); textBox3.Text = l[iSelect3].ToString(); l.RemoveAt(iSelect3);
            }

    • Marked as answer by J_Hopps Monday, October 15, 2012 3:42 PM
    Sunday, October 14, 2012 9:52 AM

All replies

  • Hi, the code seems to be correct, but place the Random outside the method at class level like:

            Random _rnd = new Random();
    
            private void button1_Click(object sender, EventArgs e)
            {
                String[] strLabel = {
                                            "A","B","C","D","E","F","G","H","I","J","K","L","M",
                                            "N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                                    };
    
                int iSelect1 = _rnd.Next(0, 26); textBox1.Text = strLabel[iSelect1];
                int iSelect2 = _rnd.Next(0, 26); textBox2.Text = strLabel[iSelect2];
                int iSelect3 = _rnd.Next(0, 26); textBox3.Text = strLabel[iSelect3];
                int iSelect4 = _rnd.Next(0, 26); textBox4.Text = strLabel[iSelect4];
                int iSelect5 = _rnd.Next(0, 26); textBox5.Text = strLabel[iSelect5];
                int iSelect6 = _rnd.Next(0, 26); textBox6.Text = strLabel[iSelect6];
                int iSelect7 = _rnd.Next(0, 26); textBox7.Text = strLabel[iSelect7]; 
            }

    You also could directly assign a reult of the Random.Next method, if you covert the int to a char. like:

            private void button1_Click(object sender, EventArgs e)
            {
                int iSelect1 = _rnd.Next(0, 26); textBox1.Text = ((char)(iSelect1 + 65)).ToString();
                int iSelect2 = _rnd.Next(0, 26); textBox2.Text = ((char)(iSelect2 + 65)).ToString();
                //...
            }

    Regards,

      Thorsten


    Sunday, October 14, 2012 9:44 AM
  • ... ah I think I misunderstood... You want to select each letter only once, right?
    Sunday, October 14, 2012 9:48 AM
  • ...

            private void button1_Click(object sender, EventArgs e)
            {
                List<char> l = new List<char>();
                for (int i = 65; i < 91; i++)
                    l.Add((char)i);
    
                int iSelect1 = _rnd.Next(0, 26); textBox1.Text = l[iSelect1].ToString(); l.RemoveAt(iSelect1);
                int iSelect2 = _rnd.Next(0, 25); textBox2.Text = l[iSelect2].ToString(); l.RemoveAt(iSelect2);
                int iSelect3 = _rnd.Next(0, 24); textBox3.Text = l[iSelect3].ToString(); l.RemoveAt(iSelect3);
            }

    • Marked as answer by J_Hopps Monday, October 15, 2012 3:42 PM
    Sunday, October 14, 2012 9:52 AM
  • or a complete form:

        public partial class Form1 : Form
        {
            Random _rnd = new Random();
            TextBox[] tb = new TextBox[26];
            Button b = new Button();
    
            public Form1()
            {
                InitializeComponent();
    
                for (int i = 0; i < 26; i++)
                {
                    TextBox t = new TextBox();
                    t.Location = new Point(24, 20 + (i * t.Height));
                    this.Controls.Add(t);
                    tb[i] = t;
    
                    if (tb.Length > 0 && tb[0] != null)
                        this.ClientSize = new Size(this.ClientSize.Width, 20 + 26 * tb[0].Height + 20);
                }
    
                b.Text = "click";
                b.Click += new EventHandler(button1_Click);
                b.Location = new Point(200, 20);
                this.Controls.Add(b);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                List<char> l = new List<char>();
                for (int i = 65; i < 91; i++)
                    l.Add((char)i);
    
                for (int i = 0; i < tb.Length; i++)
                {
                    int iSelect = _rnd.Next(0, 26 - i); tb[i].Text = l[iSelect].ToString(); l.RemoveAt(iSelect);
                }
            }
        }

    Regards,

      Thorsten



    Sunday, October 14, 2012 9:56 AM
  • Hi,

    Random needs to be marked as static.

    static Random _rnd = new Random();

    M

    Sunday, October 14, 2012 3:29 PM
  • Hi,

    Random needs to be marked as static.

    static Random _rnd = new Random();

    M

    No, not needed, copy the code I posted to a new WinFotmsProject's Form1 and run it.

    Regards,

      Thorsten


    Sunday, October 14, 2012 3:33 PM
  • Thank you so much Thorsten, you were a big help. Thank you also Malc63, I appreciate you being a support. 

    You have helped me out tons; if I get stuck again on this project I hope either of you get the chance to chime in. 

    Thanks again, 

    John

    Monday, October 15, 2012 3:45 PM