samedi 11 juillet 2020

Why I cannot generate more than 6551 random numbers

I have the following code in C#, inside the Main method of a Simple Console Application.

I have debuged and, after List.Count = 6551 it seems the random values repeat themselves.

List<int> list = new List<int>(9999);
bool waiting = true;
Random random = new Random(DateTime.Today.Milliseconds);

do
{
    int units = random.Next(0, 9);
    int tens = random.Next(0, 9);
    int hundreds = random.Next(0, 9);
    int thousands = random.Next(0, 9);

   int result = int.Parse(String.Format("{0}{1}{2}{3}", units, tens, hundreds, thousands));

   if(list.Contains(result))
   {
       continue;
   }
   else
   {
       list.Add(result);
   }

   if(list.Count == 9999)
   {
       waiting = false;
   }

}while(waiting);

Console.WriteLine("Finished"):
Console.ReadKey();



Aucun commentaire:

Enregistrer un commentaire