dimanche 3 juillet 2022

Why Random range give same output value every time? [duplicate]

Here is the code which i used to for implementing Random class

class Program
{
    public static void Main()
    {
        for (int j = 0; j < 5; j++)
        { 
            foreach (var item in GenerateRandomList(new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }))
                Console.Write(item + " ");
            Console.WriteLine(" ");
        }

    }
    public static List<int> GenerateRandomList(List<int> arr)
    {
        var random = new Random(new Random().Next(1,10000));
        var ls = new List<int>();
        while(arr.Count>0)
        {
            var index = random.Next(0, arr.Count-1);
            ls.Add(arr[index]);
            arr.RemoveAt(index);
        }
        return ls;
    }
}

below are the results I am getting

first time
4 3 8 2 1 6 7 5 9 0
4 3 8 2 1 6 7 5 9 0
4 3 8 2 1 6 7 5 9 0
4 3 8 2 1 6 7 5 9 0
4 3 8 2 1 6 7 5 9 0

second time
6 3 4 2 8 5 9 1 7 0
6 3 4 2 8 5 9 1 7 0
6 3 4 2 8 5 9 1 7 0
6 3 4 2 8 5 9 1 7 0
6 3 4 2 8 5 9 1 7 0

third time 
9 2 4 8 1 6 3 5 7 0
9 2 4 8 1 6 3 5 7 0
9 2 4 8 1 6 3 5 7 0
9 2 4 8 1 6 3 5 7 0
7 1 3 6 5 2 8 9 4 0

and So on..

what am i Missing? sometimes it give different result but again it starts to repeat previous result.




Aucun commentaire:

Enregistrer un commentaire