mercredi 14 avril 2021

How can I pick a random Element from an Array in a Parallel ForEach loop?

I am trying to pick a random Element from an Array for every time my Parallel Foreach-loop executes. But for some reason it always prints out the same string. I also tried to use the return of a function, but this also doesn't work..

Here is my Code:

        Parallel.ForEach(stringArray, element =>
        {

                Colorful.Console.WriteLine(pickElement(element));
                Colorful.Console.WriteLine(pickElement(element));
                Colorful.Console.WriteLine(pickElement(element));

        });

        private static string pickElement(String[] array)
        {

            Random random = new Random();

            return array[random.Next(0, array.Length)];

        }



Aucun commentaire:

Enregistrer un commentaire