1

Hello i am having a problem with my code, i am attempting to create a sports fixture system where with a click of a button it generates "team vs team" at random

my problem is that i managed to randomize it but i am having trouble trying to cancel duplicates out (not necessarily remove them but make it so a team cannot play against itself and so shuffle each time)

here is my code :

for some extra context this "btnFixture" button is linked with two other textboxes so it can generate 2 separate the team separately if that is of any help.

public partial class Form2 : Form
{
    string[] FirstTeam = { "Team 1" , "Team 2" , "Team 3" , "Team 4" };
    string[] SecondTeam = { "Team 1", "Team 2", "Team 3", "Team 4" };
    Random rand = new Random();

    private void btnFixture_Click(object sender, EventArgs e)
    {
        int indexFirstTeam = rand.Next(FirstTeam.Length);
        int indexSecondTeam = rand.Next(SecondTeam.Length);

        this.txt1stTeam.Text = FirstTeam[indexFirstTeam];
        this.txt2ndTeam.Text = SecondTeam[indexSecondTeam];
    }
}

I really appreciate the help given in advance. thank you!

jamesnet214
  • 1,044
  • 13
  • 21
  • 2
    Can `FirstTeam` and `SecondTeam` contain different strings? Why did you create 2 lists with exactly the same strings? – Sweeper May 20 '21 at 07:08
  • Search for fisher-yates shuffle. I have closed the question with a duplicate, another one: https://stackoverflow.com/questions/273313/randomize-a-listt – Tim Schmelter May 20 '21 at 07:12
  • I think fisher-yates would be overkill for such simple case, small lists and only 2 samples. @MisakiiTakasaki, I suggest you to check Linq method [Except](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=net-5.0) instead. – Rodrigo Rodrigues May 20 '21 at 07:17
  • Like this: `var secondViable = SecondTeam.Except(FirstTeam).ToArray()`. Then, you do `rand.Next(secondViable.Length)` and `secondViable[indexSecondTeam]` – Rodrigo Rodrigues May 20 '21 at 07:19
  • You can use a parallel exclusion List: use a single list of Teams (`Teams`), build the List of indexes from it, e.g., `var exclusion = new List(Teams.Select((s, i) => i));`, loop the `Teams` length / 2, then `int first = rand.Next(exclusion.Count); var firstTeam = Teams[exclusion[first]]; exclusion.RemoveAt(first); int second = rand.Next(exclusion.Count); var secondTeam = Teams[exclusion[second]]; exclusion.RemoveAt(second);`. If you have an odd number of Teams, take the index of the one that's left in the exclusion list. – Jimi May 20 '21 at 07:29

2 Answers2

0

You could check if the 2 teams you picked are the same, if so, pick a different second team while they are equal. Surely there is a better solution but this should do the trick :)

Dharman
  • 30,962
  • 25
  • 85
  • 135
Clement
  • 113
  • 1
  • 8
0
   public partial class Form2 : Form {
        string[] teams = { "Team 1" , "Team 2" , "Team 3" , "Team 4" };
        Random rand = new Random();
   }

   private void btnFixture_Click(object sender, EventArgs e) {
      int firstTeam = rand.next(teams.length);
      int secondTeam = rand.next(teams.length);

      while(firstTeam == secondTeam) {
         secondTeam = rand.next(teams.length);
      }
      
      // do whatever else
    }
Will Metcher
  • 331
  • 1
  • 11
  • You are comparing if the ramdomized indices are the same, but the question does not state that both arrays are equal. Should compare the picked values instead. – Rodrigo Rodrigues May 20 '21 at 07:24