-3

I'm trying to make a Computer Vs. Computer guessing game. I've done it on python and matlab, but im having trouble doing it with c#.

I dont think that the application is executing the loop, but when using the debugger to step through the application it appears to work as intended.

using System;
using static System.Console;

namespace ComVsCom
{
    class ComVsCom
    {
        static void Main(string[] args)
        {
            int rand, comp, pick = 0;
            bool tri = false;
            Random number = new Random();
            rand = number.Next(1, 10);
            Random computer = new Random();
            comp = computer.Next(1, 10);

            while (comp != rand && tri == false)
            {
                if (comp > rand)
                    WriteLine("Guess again you are too high!");
                if (comp < rand)
                    WriteLine("Guess again you are too low!");
                 pick++;
                 WriteLine("{0} attempt", pick);
                 comp = computer.Next(1, 10);
                if (comp == rand)
                {
                    WriteLine("You got it! it took you {0} times the number was {1}", pick, rand);
                    tri = true;
                }
            }
        }
    }
}

Running the program normally, it terminates gives no output. However, when debugging the output given is what was expected and is something like:

Guess again you are too high!
3 attempt
Guess again you are too low
6 attempt
You got it! it took you 3 times the number was 6

Why does this work when using the debugger and not when running the program normally?

moreON
  • 1,977
  • 17
  • 19
  • 4
    So what is your problem here? – Markiian Benovskyi Feb 22 '18 at 00:15
  • 2
    Are we supposed to also be guessing about what your question is ? – Zze Feb 22 '18 at 00:18
  • 1
    As I can see you have two `Random` variables, you should use a single `Random` instance like described [here](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – Markiian Benovskyi Feb 22 '18 at 00:19
  • 1
    Notice that if the first time both values are the same, absolutely nothing happens. Also, what's that `try` for? It's useless as far as the code you posted – Camilo Terevinto Feb 22 '18 at 00:21
  • Possible duplicate of [Random number generator only generating one random number](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – Ňɏssa Pøngjǣrdenlarp Feb 22 '18 at 01:43
  • 1
    You can see for yourself that @moreON gave the right answer. Set one breakpoint only, at `while (comp != rand && tri == false)`. When you hit the breakpoint, hold your mouse over `comp` and then `rand` and you will see they are equal. Then set a breakpoint at `Random computer = new Random();` and run again. This time pause a couple of seconds at the first breakpoint and then continue. This time `comp` and `rand` should be not equal. Then to fix it, remove `Random computer = new Random();` and replace all references to `computer` with `number`. Run again without breakpoints and it will work. – Stephen Kennedy Feb 22 '18 at 11:52

1 Answers1

3

The problem is almost certainly that you are never entering the loop:

See https://msdn.microsoft.com/en-us/library/h343ddh9(v=vs.110).aspx

The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers.

You create two Randoms at the start, then call exactly the same method on each of them. As expected based on the documentation, they produce exactly the same result. In the loop condition you check that these are different - they aren't, so the loop is never entered. Because all of your program logic is in that loop, none of it ever happens.

Note that your different behavior when debugging is probably due to stepping through the constructing of Randoms, making them occur at different times, so they will have different seed values and produce different sequences.

moreON
  • 1,977
  • 17
  • 19