Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys! Been trying to code another project here.

The concept is to try to reduce the actual codes needed to assign random char from A to Z to different labels in one click, and to make sure that no char will be repeated.

The total number of char needed is 25, which means for every click all the letters must be randomized except for one letter that will not be selected.

For example, after I click the button,

A will be assigned to label1
B will be assigned to label2
C will be assigned to label3
.
.
.
.
.
Y will be assigned to label25

and since all the 25 labels has been assigned with a char, the last char Z will not be used anymore.

NOTE: The char must be in random arrangement, which means on every click the 25 char will be assigned to different labels.

Is this possible?

Can somebody try to help me out?

I already tried using foreach loops and arrays, but it's still not working as I wanted it to.

This is the current code that I got that is very long:

C#
private void button1_Click(object sender, EventArgs e)
        {
            Random random = new Random();
            char let, let2, let3, let4, let5, let6, let7, let8, let9, let10, let11, let12, let13, let14, let15, let16, let17, let18, let19, let20, let21, let22, let23, let24, let25;
            let = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let2 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let3 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let4 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let5 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let6 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let7 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let8 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let9 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let10 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let11 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let12 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let13 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let14 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let15 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let16 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let17 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let18 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let19 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let20 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let21 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let22 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let23 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let24 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            let25 = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            label1.Text = Convert.ToString(let);
            label2.Text = Convert.ToString(let2);
            label3.Text = Convert.ToString(let3);
            label4.Text = Convert.ToString(let4);
            label5.Text = Convert.ToString(let5);
            label6.Text = Convert.ToString(let6);
            label7.Text = Convert.ToString(let7);
            label8.Text = Convert.ToString(let8);
            label9.Text = Convert.ToString(let9);
            label10.Text = Convert.ToString(let10);
            label11.Text = Convert.ToString(let11);
            label12.Text = Convert.ToString(let12);
            label13.Text = Convert.ToString(let13);
            label14.Text = Convert.ToString(let14);
            label15.Text = Convert.ToString(let15);
            label16.Text = Convert.ToString(let16);
            label17.Text = Convert.ToString(let17);
            label18.Text = Convert.ToString(let18);
            label19.Text = Convert.ToString(let19);
            label20.Text = Convert.ToString(let20);
            label21.Text = Convert.ToString(let21);
            label22.Text = Convert.ToString(let22);
            label23.Text = Convert.ToString(let23);
            label24.Text = Convert.ToString(let24);
            label25.Text = Convert.ToString(let25);
        }

NOTE: One more thing, with this current code I have, the char are still repeating itself since for each time it randomizes it doesn't remove the char that was already used from the other labels.


Really need some guidance here. Hope someone can enlighten me.
THANKS!
Posted
Updated 30-Jul-13 8:11am
v2

Simple...

C#
//Place these two as global class variables
List<char> myChars = new List<char>();
Random rnd = new Random(5252);

private void FillChars()
{
    for (int i = 0; i < 26; i++)
        myChars.Add((char)(65 + i));
}

private char GetRandomCharacter()
{
    int i = rnd.Next(0, myChars.Count);
    char c = myChars[i];
    myChars.RemoveAt(i);
    
    return c;
}


Call the initializer, and then when you need a random character call the GetRandomCharacter function.

Please note though, that this doesn't take into account if the list is empty, so there is some checking that I leave up to you.
 
Share this answer
 
v3
Comments
ridoy 30-Jul-13 16:14pm    
good one..+5
Sergey Alexandrovich Kryukov 30-Jul-13 17:10pm    
Sure, a 5. But look at "solution" 2; this is the conclusion OP made after reading your answer. Looks hopeless... :-(
—SA
Hi Sir Ron Beyer! Thanks for the quick response.

Here is the code that I wrote according to your suggestion:

C#
namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        //Place these two as global class variables
        List<char> myChars = new List<char>();
        Random rnd = new Random(5252);

        private void FillChars()
        {
            for (int i = 0; i < 26; i++)
                myChars.Add((char)(65 + i));
        }

        private char GetRandomCharacter()
        {
            
            int i = rnd.Next(0, myChars.Count);
            char c = myChars[i];
            myChars.RemoveAt(i);

            return c;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FillChars();
            label1.Text = Convert.ToString(GetRandomCharacter());
            label2.Text = Convert.ToString(GetRandomCharacter());
            label3.Text = Convert.ToString(GetRandomCharacter());
            label4.Text = Convert.ToString(GetRandomCharacter());
            label5.Text = Convert.ToString(GetRandomCharacter());
            label6.Text = Convert.ToString(GetRandomCharacter());
            label7.Text = Convert.ToString(GetRandomCharacter());
            label8.Text = Convert.ToString(GetRandomCharacter());
            label9.Text = Convert.ToString(GetRandomCharacter());
            label10.Text = Convert.ToString(GetRandomCharacter());
            label11.Text = Convert.ToString(GetRandomCharacter());
            label12.Text = Convert.ToString(GetRandomCharacter());
            label13.Text = Convert.ToString(GetRandomCharacter());
            label14.Text = Convert.ToString(GetRandomCharacter());
            label15.Text = Convert.ToString(GetRandomCharacter());
            label16.Text = Convert.ToString(GetRandomCharacter());
            label17.Text = Convert.ToString(GetRandomCharacter());
            label18.Text = Convert.ToString(GetRandomCharacter());
            label19.Text = Convert.ToString(GetRandomCharacter());
            label20.Text = Convert.ToString(GetRandomCharacter());
            label21.Text = Convert.ToString(GetRandomCharacter());
            label22.Text = Convert.ToString(GetRandomCharacter());
            label23.Text = Convert.ToString(GetRandomCharacter());
            label24.Text = Convert.ToString(GetRandomCharacter());
            label25.Text = Convert.ToString(GetRandomCharacter());
        }
    }
}
</char></char>


With this current code sir, everything is working fine now except one. There is still a letter that repeats for every instance, for example in every click two 'S' appears instead of only one. Do you think there is something wrong where I placed the FillChars(); or how I call the GetRandomCharacter() function?
 
Share this answer
 
Comments
idenizeni 30-Jul-13 16:59pm    
Try clearing the list of characters before you fill it...

private void FillChars()
{
myChars.Clear();
for (int i = 0; i < 26; i++)
myChars.Add((char)(65 + i));
}

I see the chars are being removed as they are retrieved but if you only pull 25 chars and there are 26 then one may be left in the list when you fill it again.
Sergey Alexandrovich Kryukov 30-Jul-13 17:09pm    
This is not a solution. This is not even programming, not at all. Programming is all about reuse, and you are repeating the same line several times. Think the line is not the same? Not for a software developer...
—SA
idenizeni 30-Jul-13 17:14pm    
Reuse sure helps but it's not the only goal of a programmer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900