0

I have a list with names. These names are linked to a label. When I click the label a name at random designated for that label is selected.

What I need is for when a button is clicked, it selects a random name for each label.

for example;

I have 5 names in a list and each individual list is linked to an individual label. One name will be selected for a label at random. I need all 10 of my labels to select a random name, when a button is clicked.

Hope that makes sense. I am using Visual Studio 2010 and c# on a form. Many Thanks

T.Fensom
  • 13
  • 5
  • 10 labels and 5 names. Do you want duplicate names in your labels? – Steve Dec 06 '15 at 15:40
  • 1
    [What have you already tried](http://stackoverflow.com/help/mcve)? Do you have code that assigns new text to 10 labels on a button click (meaning that you have trouble getting the values to be random)? How does your code look so far? – joranvar Dec 06 '15 at 15:43
  • You may also have some benefit from [this related question](http://stackoverflow.com/questions/24189122/how-to-show-two-seperate-random-items-from-2-listbox-into-a-messagebox). – joranvar Dec 06 '15 at 16:00

3 Answers3

1

The question is very vague.

However, a method to choose a random string from a list of strings would look like this:

 public string ChooseRandomName(List<string> names)
 {
     Random rnd = new Random();
     return names[rnd.Next(0, names.Count)];
 }

Now, the only thing to do is to call this method with the proper input and attribute the output to the proper label. Not sure how your labels are given, but something like:

label1.Text = ChooseRandomName(listOfNamesForLabel1);

I hope this helps.

Mario Mucalo
  • 597
  • 5
  • 16
  • 1
    You would definitely need to create only one `Random` object per program or so, to really get random choices. Perhaps make it a static field or something, and initialize it only once. Otherwise, this will work nicely. – joranvar Dec 06 '15 at 17:03
  • @joranvar - correct. What you suggest is a better solution. What I wrote was just a quick proof of concept. If you call it several times in a row, it might return same results. Extracting the Random object and passing it in as a parameter would be better. – Mario Mucalo Dec 06 '15 at 17:32
1
public string ChooseRandomName(List<string> names,Random rnd)
{
   return names[rnd.Next(0, names.Count)];
}
Random rnd = new Random();
label1.Text = ChooseRandomName(listOfNames,rnd);
label2.Text = ChooseRandomName(listOfNames,rnd);

Dont create intialize random object every time. other wise it will give same value. check this link

Community
  • 1
  • 1
Anik Saha
  • 4,313
  • 2
  • 26
  • 41
0

So this is what I have. This is part of the code I need (dont worry with what its about). The goalkeeper label is called "goalkeeper" my button should be called "pickTeam" but when clicked shows up as "button1_Click" private void button1_Click(object sender, EventArgs e)

I need it so that when the button clicked, every label (I can do myself once figured out) will choose a random name that has been given. I.E; Begovic, De Gea etc etc.

i have a method for each position. This is the Goalkeeper, once I know this I can do it for the other positions. Thanks

 private void goalkeeper_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            List<string> goalkeepers = new List<string>();
            goalkeepers.Add("Neuer");
            goalkeepers.Add("De Gea");
            goalkeepers.Add("Lloris");
            goalkeepers.Add("Begovic");
            for (int i = 0; i < 4; i++)
            {
                int index = rand.Next(0, 4);
                goalkeeper.Text = goalkeepers[index];
            }
        }
T.Fensom
  • 13
  • 5