Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Code only works when using the debugger? [duplicate]

Tags:

c#

random

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.

like image 960
vurhd1 Avatar asked Jul 01 '15 16:07

vurhd1


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

The new Random() constructor uses the current time as the seed.

Unless you wait in the debugger, all of your Random instances have the same seed.

You should use a single instance.

like image 112
SLaks Avatar answered Sep 22 '22 23:09

SLaks


This has to do with how the random numbers are generated.

If you take

Random rnd = new Random();

and move it out of the loop, you should see the desired results.

More background:

The random number generator uses a seed based on the time you instantiate it. Because your code is running so quickly, the seed is the same so the numbers are the same. This is why it works when you step through.

Instantiating the Random outside of the loop will instantiate it once and use the random algorithm to generate new numbers.

like image 42
oppassum Avatar answered Sep 24 '22 23:09

oppassum