#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Garbage Collection Issue (or more likely, I'm an idiot!)


    I'm still learning to program. This is the first practical application I've tried to write outside of copying from my textbook. I thought it'd be something easy and within my ability, but it's turning out not so much.

    I'ts a simple command line "dice roller" where two people roll 2 six sided dice each and compare the result (with some wrinkles, but those haven't been programmed in yet).

    Here's a slimmed down version so you can see what I'm doing.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
    
    
            static int GenericAttack()
            {
                Random myRandom = new Random();
                int die1 = myRandom.Next(1, 6);
                int die2 = myRandom.Next(1, 6);
                int attackTotal = die1 + die2;
                Console.WriteLine("The attacking character rolls a {0} and {1} for a total of {2}", die1, die2, attackTotal);
                return attackTotal;
            }
    
            static int GenericDefend()
            {
                Random myRandom = new Random();
                int die1 = myRandom.Next(1, 6), die2 = myRandom.Next(1, 6);
                int attackTotal = die1 + die2;
                Console.WriteLine("The defending character rolls a {0} and {1} for a total of {2}", die1, die2, attackTotal);
                return attackTotal;
            }
    
            static int Fight()
            {
                int attackTotal = 0, defendTotal = 0;
    
                attackTotal = GenericAttack();
    
                defendTotal = GenericDefend();
    
                if (attackTotal > defendTotal)
                    return 1;
                else if (attackTotal < defendTotal)
                    return 2;
                else
                    return 0;
            }
    
            static void Main(string[] args)
            {
                Random myRandom = new Random();
                Console.WriteLine("Let's Fight!");
    
                int winner = 0;
    
                while (winner == 0)
                {
                    winner = Fight();
                }
    
                if (winner == 1)
                    Console.WriteLine("The attacking Character wins!");
                else if (winner == 2)
                    Console.WriteLine("The defending Character wins!");
                else
                    Console.WriteLine("Something went horribly wrong!");
    
                Console.ReadKey();
    
            }
        }
    }
    What I'm getting as an output, though, is something like:

    Code:
    Let's Fight!
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 1 and 4 for a total of 5
    The attacking character rolls a 1 and 4 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 3 and 2 for a total of 5
    The defending character rolls a 3 and 2 for a total of 5
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 2 and 4 for a total of 6
    The attacking character rolls a 2 and 4 for a total of 6
    The defending character rolls a 4 and 3 for a total of 7
    The defending Character wins!
    So, at first I thought somehow the die1 and die2 variables were carrying over between functions, even though they shouldn't. But renaming the variables didn't help. Nor did renaming the myRandom methods to something like attackRandom and defendRandom.

    It also never happens when I try to Step Into the functions while debugging (though it always happens when I step over). Grrrr. Can someone tell me what's going wrong? Thanks!
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    There is not a lot of C# people around here, but I would hazard a guess that your problem is you keep creating a new Random object which probably starts the random number sequence over again each time. You should initialise one for the class and reuse the same one. Also, look into seeding the random number generator, if you use the exact same seed you get the exact same sequence.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,112
    Rep Power
    1803
    The Random class generates a sequence; if you constantly create and destroy Random objects, the sequence restarts. The Random() constructor (with no seed parameter) uses the current time in seconds to seed the random number sequence, so if you create multiple generators in the same second they will all create the same sequence - that is why you get the same value when you free run, but different values when you step the code.

    You need to instantiate just one instance of the Random object for the entire class or even the entire program and then use that one instance in all your functions.

    Here is a minimal example:

    c# Code:
     
    using System;
     
    namespace ConsoleApplication1
    {
        class Program
        {
            static Random myRandom = new Random() ;
     
            static void Main(string[] args)
            {
                for (int x = 0; x < 20; x++)
                {
                    int i = myRandom.Next();
     
                    Console.WriteLine(i);
                }
            }
        }
    }
    Last edited by clifford; September 2nd, 2012 at 09:43 AM.

IMN logo majestic logo threadwatch logo seochat tools logo