samedi 30 janvier 2021

Why does using the Random() class to create 100 random arrays create repeating arrays? [duplicate]

I am making a program that creates 100 arrays and fills them with random numbers between 1 and 100. However, it produces repeating arrays. The amount of repeating arrays is inconsistent and changes every time I run it. How can I create 100 non-repeating random arrays? Additionally, I am using this in a program where I time different sorting techniques. Why does it take different amounts of time to sort identical arrays (the ones that are supposed to be random arrays) with the same sorting method?


int[] array = null;
        for (int i = 0; i < 101; i++)
        {
           Random r = new Random();
           int length = 15;
           array = new int[length];
           for (int n = 0; n < length; n++)
           {
              int newNum = r.Next(100);
              array[n] = newNum;
           }
           Console.WriteLine();
           Console.WriteLine("#" + (i + 1));
           PrintArray(array);




Aucun commentaire:

Enregistrer un commentaire