-4

When I step through the program using the debugger it works fine and the displays the correct results. However, when I run the program it displays some extremely high number for homeTeamRuns and awayTeamRuns. I can't seem to figure out the problem.

class BallGame
    {
        //Fields
        private string output = ""; 

        int outs = 0;
        int runs = 0;

        int homeTeamRuns = 0;
        int awayTeamRuns = 0;

        bool homeTeam = false;
        bool awayTeam = false;

        int[] innings = new int[12]; //Innings 

        //Properties
         public string Output
         {
             get
             {
                 return output;
             }
             set
             {
                 output = value;
             }
         }

        //Game Simulator
        public void playBall()
        {

            int[] play = new int[4];  //Bases



            for(int i = 1; i <= 2; i++)
            {
                homeTeam = false;
                awayTeam = false;



                if(i%2 == 0)
                    homeTeam = true;
                else 
                    awayTeam = true;

            //Half Inning
            while (outs < 3)
            {

                bool yourOut = false, single = false, double1 = false, triple = false, homerun = false;

                Random rnd1 = new Random();
                int randomNum = rnd1.Next(1, 101);

                //
                if (randomNum >= 1 && randomNum <= 50) //50% chance
                    yourOut = true;
                else if (randomNum > 50 && randomNum <= 60) //10% chance
                    single = true;
                else if (randomNum > 60 && randomNum <= 92) //42% chance
                    double1 = true;
                else if (randomNum > 92 && randomNum <= 97) //5% chance
                    triple = true;
                else if (randomNum > 97 && randomNum <= 100) //%3 chance
                    homerun = true;


                if (yourOut == true)
                {
                    outs++;
                }
                else if (single == true)
                {
                    Shift(play);
                    runScored(play);

                    play[0] = 1;
                }
                else if (double1 == true)
                {
                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    play[1] = 1;
                }
                else if (triple == true)
                {
                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    play[2] = 1;
                }
                else if (homerun == true)
                {
                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    play[3] = 1;
                }


                play[3] = 0;

                if (outs == 3)
                {
                    play[0] = 0;
                    play[1] = 0;
                    play[2] = 0;
                    play[3] = 0;
                }
            }

            outs = 0;

            }
            output = "Home Team Runs: " + homeTeamRuns + " Away Team Runs: " + awayTeamRuns;

         }

        //Shift Array
        public int[] Shift(int[] array)
        {
            for (int i = array.Length -1; i >= 1; i--)
            {
                array[i] = array[i - 1];
            }
            array[0] = 0;

            return array;
        }

        public void runScored(int[] array)
        {
            if(array[3] == 1)
            {
                if(awayTeam == true)
                    awayTeamRuns++;
                else if(homeTeam == true)
                    homeTeamRuns++;
            }
        }


    }
user3413118
  • 37
  • 1
  • 2
  • 7
  • 3
    Your code would be a *lot* shorter if you'd learn about automatically implemented properties. Additionally, it's hard to know what's wrong without you explaining what you *expect* to see vs what you *actually* see. It doesn't help that the code wouldn't actually *run* - it's clearly part of a bigger program. A short but complete console app would be easier to help you with... (Additionally, you should learn about `List` so that you don't need to mess around with arrays like this...) – Jon Skeet Sep 23 '14 at 21:47
  • Could you tell us the values that the program is giving (Correct and incorrect results)? – John Odom Sep 23 '14 at 21:48
  • Also note you don't have to initialize ints to 0 or bools to false. You can also just say `if (yourOut)` instead of `if (yourOut == true)` – Mike Christensen Sep 23 '14 at 21:49
  • 1
    Good time to learn how to debug your code. You can't always dump your code here and ask *"where is my bug"*. – L.B Sep 23 '14 at 21:51
  • 2
    If I had to guess I'd say try moving the `Random rnd1 = new Random();` outside of the loop. I suspect that's using the same seed so the call to `rnd1.Next` is returning the same value when run outside of the debugger. – petelids Sep 23 '14 at 21:52
  • @petelids: Or rather, using the same seed. – Jon Skeet Sep 23 '14 at 21:53
  • See http://csharpindepth.com/Articles/Chapter12/Random.aspx for more details on the hazards of `Random` – Jon Skeet Sep 23 '14 at 21:53
  • Well I'm new to C# and Visual Studio so I have yet to learn about automatically implemented properties. Also I'm not very familiar with Lists yet. The program does run, but it doesn't give me expected results. When I use debug and step through it does give me the correct results. I expect the runs of each team to generally be below 50. However, I'm getting numbers like 23,452 which makes no sense. – user3413118 Sep 23 '14 at 22:13
  • Moving Random outside of the loop seemed to fix the problem. I don't fully understand why though. – user3413118 Sep 23 '14 at 22:15

1 Answers1

2

You are creating a new instance of the Random class on each iteration of your loop. The documentation for the constructor you are using says:

Initializes a new instance of the Random class, using a time-dependent default seed value.

As the loop executes very quickly when you aren't debugging you end up with the same seed being used on many iterations and thus the call to Next returns the same value each time. If that value happens to be a value that will increment the score then the score will be incremented on many iterations of the loop.

Moving the line

Random rnd1 = new Random();

above your for loop will construct one Random instance and then create a new random number for each call to Next.

You can't reproduce this when running in the debugger as the clock has moved on by the time you create a new Random instance and thus the seed has changed.

More information on the Random instance returning the same value can be found in this StackOverflow post. Also, Jon Skeet has a blog post covering pitfalls when using the Random class which is well worth a read.

Community
  • 1
  • 1
petelids
  • 12,305
  • 3
  • 47
  • 57