Confusing VS behavior

Cryotech

New member
Joined
Jan 27, 2016
Messages
2
Programming Experience
5-10
First off, I'd like to say hello to everyone and it's great to be here :).

I'm having a particular behavior issue I can't seem to figure out and I've tried searching online for the issue, as well as debugging numerous times with no success.

Here's the code I'm having issues with:
C#:
            int userAnswer = 1; //Set to '1' for debugging purposes -- Convert.ToInt32(Console.ReadLine());
            int pHealth = 0, pAttack = 0, pStr = 0, pDefend = 0, pArmor = 0, pDex = 0, pMagic = 0, pLuck = 0;
            int[] pAttributes = new int[7];

            if(userAnswer == 1)
            {
               
                for(int i = 0; i < 7; i++)
                {
                    pAttributes[i] = Roll();
                }
                
                //set attributes randomly. NOTE:: BaseAttributes is an ENUM from another class with default values of 10 for all attributes.
                pHealth = (int)BaseAttributes.baseHealth + pAttributes[0];
                pAttack = (int)BaseAttributes.baseAttack + pAttributes[1];
                pStr = (int)BaseAttributes.baseStr + pAttributes[2];
                pArmor = (int)BaseAttributes.baseArmor + pAttributes[3];
                pDex = (int)BaseAttributes.baseDex + pAttributes[4];
                pMagic = (int)BaseAttributes.baseMagic + pAttributes[5];
                pLuck = (int)BaseAttributes.baseLuck + pAttributes[6];

            }

            Console.WriteLine("-------------");
            Console.WriteLine("Health: {0}\nStrength: {1}\nDexterity {2}\nAttack: {3}\nArmor: {4}\nMagic: {5}\nLuck: {6}", 
                                        pHealth, pStr, pDex, pAttack, pArmor, pMagic, pLuck);

            Console.ReadKey();
        }

        public static int Roll()
        {
            Random rand = new Random();
            int randomRoll = rand.Next(1, 20);
            return randomRoll;
        }
    }

I realize the above code is NOT the best way to do what what is intended. I was just messing around and playing with different coding aspects and nothing more. I just ran into an odd issue I can't figure out so I'm asking for a second pair of eyes. The above code works, but that's where the issue lies oddly enough.

The problem is, in Microsoft VS 2015, when that code runs without debugging, every value for the attributes shown in the console are the same, however, when I run debugger it works as it should and displays different values.

I'm hoping someone can help me see what I'm not seeing.

Thanks in advance.
 
The issue is that you create a new Random object in your Roll method. The Random class uses a seed value derived from the current system time by default. When you debug, you are slowing things down so that the time changes between each call to Roll so each Random object uses a different seed. When you run the app properly, the calls are made so fast that every Random object uses the same seed and thus produces the same pseudo-random sequence.

This is a trap for young players. When using the Random class, you should avoid creating multiple instances if possible. In your case, you should be creating a single instance and assigning it to a field and then calling Next on that one instance multiple times:
private static Random rand = new Random();

public static int Roll()
{
    int randomRoll = rand.Next(1, 20);
    return randomRoll;
}
The documentation for the Random class addresses this issue specifically, so one wonders why you didn't realise your mistake when you read that documentation.

By the way, are you aware that your Next call will never return the number 20? The minimum value that you specify is inclusive but the maximum is exclusive, so you'll only get numbers from 1 to 19. Maybe that's what you want but I thought I'd make sure.
 
Hi jmcilhinney,

Thank you for the help. Yeah, I was having a serious man-brain moment there haha. I re-read the documentation more than once and was searching all over the net for some clues and it never dawned on me. I actually ended up figuring it out shortly after I posted and just didn't have the time to come back to this board, but still, it was a face palm moment lol.

As far as the range, I was aware about the max range but I wasn't really going for anything particular. They were basically put there to test out what I was doing. Thanks for ensuring I was aware of that if I wasn't though, greatly appreciate it!

Again, thank you for your help.
 
Back
Top Bottom