Simply put, I want mylist to contain 7 unique numbers and I wrote the code below for that purpose.
However, it does not do the job and I get fewer unique numbers (mostly only 5 and sometimes 7) when deletion becomes necessary after I get a roll violating uniqueness.
Basically, I want a loop that keeps working and rolling random numbers within a specified range until a string of 7 unique numbers is observed. Of course, it should also put that desired string into a list.
class Program
{
static void Main(string[] args)
{
Random number = new Random();
List<int> mylist = new List<int>();
int i = mylist.Count;
while(i<7)
{
int selected = number.Next(50);
if (mylist.Contains(selected))
{ mylist.Remove(selected); }
else
{
mylist.Add(selected);
}
i++;
}
Console.WriteLine("Amount:"+ mylist.Count);
foreach (int item in mylist)
{
Console.WriteLine(item);
}
Console.ReadKey();