vendredi 10 mars 2017

C# - Random.Next returns 0 after a period of iterations

I have code that looks like this:

class Program
    {
        public static Random random = new Random();

        static void Main(string[] args)
        {
             var result = false;
             for(int i = 0; i < 30; i++)
             {
                 result = MyFunc(random);
             }
        }

        public static bool MyFunc(Random rdm)
        {
            int numElements = rdm.Next(0, 20);
            ...
        }
        }

This produces a random number for about 4-5 iterations of the loop and then begins to only return 0 afterwards. I should say that in between there is roughly a minute of processing happening between each call so it's not a tight loop. It seems somehow it gets messed up and Random.Next just reverts to 0.

Any idea on what I'm doing wrong?




Aucun commentaire:

Enregistrer un commentaire