Solved

Running Debugged vs. Non-Debugged

Posted on 2004-08-28
2
212 Views
Last Modified: 2010-04-15
Hi,
I have a program that simulates a game many times and returns the percent of the time that each team won (8 teams).  I believe I have all the logics correct, but I'm finding some odd results.  When I run the program stepping through the lines (in debug mode obviously), it distributes the wins correctly.  However, when I simply run the program through for 100 trials, one team will win all 100 times.  And then if I run it again, another team will win 100 times.  Then again, and team 1 will win 84 times and team 2 will win 16 times and the other 6 teams won't win at all.  Now if I run the program through a million trials, it seems to distribute nicely.  I'm really confused as to why this would ever happen.  My logic is correct since as I said before, running the program debugged works fine.  This is C# dot net console program btw.

Here's my code:
using System;

namespace Project1
{
      class Class1
      {
            [STAThread]
            public static void Main(string[] args)
            {
                  int gameWinner;
                  double percentWon;
                  int[] teamGames = new int[8];
                  
                  for (int xx = 1; xx <= 100; xx++)
                  {
                        gameWinner = Class1.RunGame();
                        teamGames[gameWinner]++;
                  }

                  for (int yy = 1; yy <= 8; yy++)
                  {
                        percentWon = (teamGames[yy - 1]/100.000);
                        Console.WriteLine("Team " + yy + " = " + percentWon);
                  }
            }
            
            public static int RunGame()
            {
                  int team1, team2, teamondeck, pointmod, winner;
                  int[] teamWins = new int[8];

                  team1 = 1;
                  team2 = 2;
                  teamondeck = 3;
                  pointmod = 1;
                  Random r = new Random();
                  
                  while (true)
                  {
                        if (r.NextDouble() >= .5)
                        {
                              teamWins[team1 - 1] = teamWins[team1 - 1] + pointmod;
                              if (teamWins[team1 - 1] >= 7)
                              {
                                    winner = team1 - 1;
                                    break;
                              }
                              team2 = teamondeck;
                              teamondeck++;
                        }
                        else
                        {
                              teamWins[team2 - 1] = teamWins[team2 - 1] + pointmod;
                              if (teamWins[team2 - 1] >= 7)
                              {
                                    winner = team2 - 1;
                                    break;
                              }
                              team1 = team2;
                              team2 = teamondeck;
                              teamondeck++;
                        }
                        if (teamondeck > 8)
                        {
                              teamondeck = 1;
                              pointmod = 2;
                        }
                  }
                  return winner;
            }
      }
}


I'd appreciate any help.  Thanks!!
0
Comment
Question by:schwarz2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
GrumbleBot earned 125 total points
ID: 11925787
I found by making the variable r a global variable your program works correctly. Not sure if I can explain why it doesn't work the way that you have it. But if i basically set up a random numbe rin a fuction and fill a listbox continually I get the same number for many consecutive values then it changes and repeats those many times , on and on.

Well here is your code modified and it seems to work now.

using System;

namespace Project1
{
      class Class1
      {
            static Random r;

            [STAThread]
            public static void Main(string[] args)
            {
                  int gameWinner;
                  double percentWon;
                  int[] teamGames = new int[8];
                  r = new Random(); //((int)DateTime.Now.Ticks)
               
                  for (int xx = 1; xx <= 100; xx++)
                  {
                        gameWinner = Class1.RunGame();
                        teamGames[gameWinner]++;
                  }

                  for (int yy = 1; yy <= 8; yy++)
                  {
                        percentWon = (teamGames[yy - 1]/100.000);
                        Console.WriteLine("Team " + yy + " = " + percentWon);
                  }
                  
            }
         
            public static int RunGame()
            {
                  int team1, team2, teamondeck, pointmod, winner;
                  int[] teamWins = new int[8];

                  team1 = 1;
                  team2 = 2;
                  teamondeck = 3;
                  pointmod = 1;
               
                  while (true)
                  {
                        if (r.NextDouble() >= .5)
                        {
                              teamWins[team1 - 1] = teamWins[team1 - 1] + pointmod;
                              if (teamWins[team1 - 1] >= 7)
                              {
                                    winner = team1 - 1;
                                    break;
                              }
                              team2 = teamondeck;
                              teamondeck++;
                        }
                        else
                        {
                              teamWins[team2 - 1] = teamWins[team2 - 1] + pointmod;
                              if (teamWins[team2 - 1] >= 7)
                              {
                                    winner = team2 - 1;
                                    break;
                              }
                              team1 = team2;
                              team2 = teamondeck;
                              teamondeck++;
                        }
                        if (teamondeck > 8)
                        {
                              teamondeck = 1;
                              pointmod = 2;
                        }
                  }
                  return winner;
            }
      }
}

Hope this helps.

Good luck
0
 

Author Comment

by:schwarz2
ID: 11926547
Excellent, GrumbleBot!!  This was driving me crazy .. I guess C# doesn't re-seed the random variable when it's re-created.  Very strange, but I'm glad you caught it.  Thanks millions.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 42
Input parameteres to DragOver 2 38
Where does legacy ASP.NET initialize language? 2 64
Problem to Office 1 45
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question