1

This is what ive got so far, but it doesnt take into account if someone is already in a team. i.e.

Alex and John are going to be randomly assorted into the teams. Currently is the system puts Alex into team 1, it wont know that someone is in team 1 and could place John in team 1 also. This logic needs to also reflect if there are 4 teams and 16 people to be sorted etc. Can someone help?

List<int> teamsLeft = new List<int>();

            foreach (var item in namesOfAllPlayers)
            {
                if (!teamsLeft.Any())
                {
                    for (int i = 0; i < noOfTeams.Value; i++)
                        teamsLeft.Add(i + 1);
                }

                Random rnd = new Random();
                int team = rnd.Next(1, Convert.ToInt32(noOfTeams.Value));

After it determined what player is in the team it then extracts that team number from teamsLeft and continues. Once all teams have been extracted, meaning there have been 1 placed in each time more than previously, then it will refresh the teamsLeft.

A.Cassin
  • 41
  • 1
  • 6
  • 4
    Don't declare a new `Random` inside a loop. See this [question](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number/768001#768001) – Mark Benningfield Feb 06 '18 at 15:26
  • Ahh okay, my mistake, ive moved it outside the loop now thanks! – A.Cassin Feb 06 '18 at 15:29
  • 3
    Trying to take from a list without causing duplicates is a major PITA; usually, the more practical and efficient approach is simply: shuffle the set (`namesOfAllPlayers`) and then just deal them out from the shuffled list in order, like you would if it were a deck of cards. Shuffling an array or list is very readily searchable, for example: https://stackoverflow.com/questions/273313/randomize-a-listt – Marc Gravell Feb 06 '18 at 15:30
  • To @MarcGravell 's point https://stackoverflow.com/questions/273313/randomize-a-listt – Conrad Frix Feb 06 '18 at 15:32
  • Didn't even know that was a thing, cheers – A.Cassin Feb 06 '18 at 15:32
  • You should look at the Fisher-Yates shuffle, which is a O(n) solution. – Greg Feb 06 '18 at 15:43
  • using that emthod was a lot simpler than what i was thinking, thanks @MarcGravell – A.Cassin Feb 06 '18 at 15:46

1 Answers1

0

While I was doing my solution @MarcGravell suggested something along the same lines. Nevermind, I'll give my solution anyway. I'm not shuffling, just lining the players up randomly:

This code:

var players = new List<string> { "Abe", "Brian", "Chloé", "Dan", "Eve", "Finn", "Gina", "Henry",
"Ivan", "Joe", "Kate", "Lou", "Max", "Nina", "Oona", "Paul"};
var randomPlayers = new List<string>(); 

var random = new Random();

for (int i=0; i<15; i++)
{
    int place = random.Next(0, i+1);
    randomPlayers.Insert(place, players[i]);
}

makes a random player list. Now you can either take the first four to team 1, the next four to team 2 and so on, or you can do the old thing where you line people up and give them numbers 1,2,3,4,1,2...

Palle Due
  • 5,929
  • 4
  • 17
  • 32