The task is to create two random teams from a list of players.
Given an example of players = ["John", "Mike", "Alice", "Bob"]
One example random team would be:
team1 = ["Mike", "Alice"];
team2 = ["John", "Bob"];
This is my solution but I'm not sure how efficient it is because random can create same number for a number of times.
The question to separate equally and randomly without repeating the same player.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
Random rnd = new Random();
var numbers = new List<int>();
var ilkListe = new List<int>();
var ikinciListe = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
numbers.Add(4);
numbers.Add(5);
numbers.Add(6);
numbers.Add(7);
numbers.Add(8);
numbers.Add(9);
numbers.Add(10);
Console.WriteLine("LIST 1: " + numbers.Count);
do {
int sayi = numbers[rnd.Next(numbers.Count)];
if (!(ilkListe.Contains(sayi))) {
Console.WriteLine(sayi);
ilkListe.Add(sayi);
}
}
while (ilkListe.Count < numbers.Count / 2);
Console.WriteLine("Ikinci liste");
do {
int sayi = numbers[rnd.Next(numbers.Count)];
if (!(ilkListe.Contains(sayi)) && !(ikinciListe.Contains(sayi))) {
Console.WriteLine(sayi);
ikinciListe.Add(sayi);
}
}
while (ikinciListe.Count < numbers.Count / 2);
}
}