How to get multiple Abels to have a randomly generated number

Asked

Viewed 81 times

3

I am playing a Bingo game. Where I have 4 cards and each card has 15 Bingo cards (15 numbers). I want that when the form starts, each label has a randomly generated number. However, I’m only getting a label to get a random number.

This is what I got:

foreach (Label lbl in this.Controls.OfType<Label>())
        {
            Random rnd = new Random();
            rnd.Next(100).ToString();
        }

2 answers

2

The @Leonardol explanation is very good for your problem! But I will leave an additional code to solve another type of problem if it occurs in your scenario.

Leonardo’s scenario will work very well if all his Abels are inside the form as primary control, if they are daughters of some other control, such as, for example, a Panel (they are inside a panel), you will not be able to find them in your foreach.

To solve this scenario, we need to create a recursive method that will navigate all the controls of our screen, until the last level, search for all the Abels contained in this form and define a new value for the property Text:

public void DefinirValorLabels(Control control, Random random)
{
    // Se nosso controle for uma label, definimos a propriedade "Text" como um número aleatório
    if (control is Label)
        control.Text = random.Next(1, 100).ToString();

    // Se o controle não é uma label, ele pode ser um panel (por exemplo), ou seja, dentro desse controle (panel) pode haver outras labels.
    // Por esse motivo, iremos percorrer nosso controle e ver se possuimos mais labels para definirmos um valor.
    foreach (Control ctrl in control.Controls)
    {
        DefinirValorLabels(ctrl, random);
    }
}

To use, simply call the method as follows:

Random random = new Random();
DefinirValorLabels(this, random);
  • I think in the future I’ll need this, thank you!

2


You must use the variable lbl inside the foreach, as it will represent each label of your form.

Also, it is recommended that you create the instance of the Random class outside of your loop, as you run the risk of generating the same numbers repeatedly, plus performance issues.

Initialize two random number generators in a narrow loop, or in rapid succession creates two random number generators that can produce identical sequences of random numbers. Most of the cases, this is not the intention of the developer and may lead to performance problems, such as instantiating and initializing a generator random number are a relatively expensive process.

Source

Another point given your context, you are making a bingo, so there is no number 0 in the chart. The most recommended for you to generate the random numbers is to use it as follows, because your code has the chance to generate the number 0.

The final code will be as follows:

Random rnd = new Random();

foreach (Label lbl in this.Controls.OfType<Label>())
        {    
            lbl.Text = rnd.Next(1, 100).ToString(); // Irá gerar um número de 1 até 99
        }
  • Thank you so much! It really makes more sense to use this way. I really appreciate it, it helped a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.