I have ten teams in database table. How to randomly generate matches between teams?
Asked
Active
Viewed 55 times
-3
James Z
- 12,104
- 10
- 27
- 43
Arsalan Raza
- 3
- 1
-
welcome to stackoverflow. please include code, error messages, and other _text-based_ information ***as text***, [not as screenshot](https://idownvotedbecau.se/imageofcode). what have you [***tried yourself***](https://idownvotedbecau.se/noattempt/) so far? what problems did you encounter? what have you researched? please **edit** your question to include more information. i recommend [taking the tour](https://stackoverflow.com/tour), as well as reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [what's on topic](https://stackoverflow.com/help/on-topic). – Franz Gleichmann Apr 14 '22 at 07:43
-
Your question have a solution here. https://stackoverflow.com/questions/3093622/generating-all-possible-combinations – Wei Chen Chen Apr 14 '22 at 07:43
-
1I think `randomly generate matches` can be understood like this: You can put the ID of the 10 teams into an array and then randomly generate 5 arrays of length 2 , Then the two teams whitch are Randomly divided into the same array play against each other. – Xinran Shen Apr 14 '22 at 09:13
1 Answers
0
You can try to use the following code:
var random = new Random();
List<yourModel> list= _context.yourModel.ToList();
Dictionary<int, List<yourModel>> d = new Dictionary<int, List<yourModel>> { };
var count = list.Count();
for (int i = 0; i < count/2; i++) {
List<yourModel> temp = new List<yourModel>();
int index1 = random.Next(list.Count);
temp.Add(list[index1]);
list.RemoveAt(index1);
int index2 = random.Next(list.Count);
temp.Add(list[index2]);
list.RemoveAt(index2);
d.Add(i , temp);
}
And the dicitionary is what you want.
Yiyi You
- 12,796
- 1
- 4
- 13