samedi 4 juillet 2015

Generating all random maches from lists with some exceptions

Let's say I have five lists like these

  List<string> A = new List<string> {"A1", "A2", "A3", "A4", "A5"};
  List<string> B = new List<string> {"B1", "B2", "B3", "B4", "B5"};
  List<string> C = new List<string> {"C1", "C2", "C3", "C4", "C5"};
  List<string> D = new List<string> {"D1", "D2", "D3", "D4", "D5"};
  List<string> E = new List<string> {"E1", "E2", "E3", "E4", "E5"};

I want to randomly generate all possible pairs from their elements like "A1 E2", "D4 A2" (with A1 E2 and E2 A1 being different matches) etc. etc. BUT so that elements from lists B and C would never match.

So I just tried doing something like this

  int rand = X.Next(1, 10); // actual range depends on number of lists
  if (rand == 1)
  Console.WriteLine(A[X.Next(A.Count)] + B[X.Next(B.Count)]) // AB match
  else if (rand == 2)
  Console.WriteLine(A[X.Next(A.Count)] + C[X.Next(C.Count)]) // AC match
  else if (rand == 3)
  Console.WriteLine(A[X.Next(A.Count)] + A[X.Next(A.Count)]) // AA match

And so on. Excluding BC matches. If I have a few lists it works fine. But if the number of lists grows higher this code gets too long and clumsy. So my question is - how do I make it shorter and more efficient?

And question 2 - what if I want to generate not only pairs but also triples? Like "A1 B3 E5". With exceptions (B and C) or without.




Aucun commentaire:

Enregistrer un commentaire