mercredi 1 juillet 2015

C# Code only works when using the debugger?

So here's the code I've been using. Its just a simple program to test to see if 3 randomly generated numbers are in ascending or descending order. For some reason if I'm using the debugger and stepping into every line then the code works properly. If not then it says the numbers are in order 100% or out of order 100%, which should not be the case.

Here is the code I've been using:

        int num1;
        int num2;
        int num3;

        int yes = 0;
        int no = 0;

        for (int i = 0; i <= 99; i++)
        {

            Random rnd = new Random();

            num1 = rnd.Next(1, 11);
            num2 = rnd.Next(1, 11);
            num3 = rnd.Next(1, 11);

            if ( ((num1 <= num2) && (num2 <= num3)) || ((num1 >= num2) && (num2 >= num3)) )
            {
                yes += 1;
            }

            else
            {
                no += 1;
            }

        }


        Console.WriteLine("The Number are in ascending order " + yes.ToString() + " Times");
        Console.WriteLine("The Number are not in ascending order " + no.ToString() + " Times");

        Console.ReadLine();

I think that it might be a problem with the pseudo random and the code generating the same 3 numbers every time, but I'm still learning more about programming and other help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire