Talbert1209
Talbert1209

Reputation: 175

random.Next() not returning random only returning 0 - C#

I'm sure this will get marked as a duplicate, but I genuinely have tried to figure this out. Here goes nothing.

I have come across a problem while working through an exercise in Head First C#. My problem is my random.Next(2) in my Move() method will only return 0. Here is the condensed code:

public class Opponent
    {
        private Location myLocation;
        private Random random;
        public bool Hidden { get; private set; }

        public Opponent(Location startingLocation)
        {
            myLocation = startingLocation;
            random = new Random();
            Hidden = false;
        }

        public void Move()
        {
            var coinFlip = random.Next(2);

            if (myLocation is IHasExteriorDoor)
            {
                if (coinFlip == 1)
                {
                    var myLocationWithDoor = (IHasExteriorDoor)myLocation;
                    myLocation = myLocationWithDoor.DoorLocation;
                }
            }

            myLocation = myLocation.Exits[random.Next(myLocation.Exits.Length)];
            while (!(myLocation is IHidingPlace))
            {
                myLocation = myLocation.Exits[random.Next(myLocation.Exits.Length)];
            }

            Hidden = true;
        }
    }

I have tried moving the random.Next() around, putting it directly in the if statement, but that didn't change anything.

I also replaced my code with the code from the book and that seems to work. The only problem is I truly do not see the difference. Here is the Move() method the book provided.

public void Move()
    {
        bool hidden = false;
        while (!hidden)
        {
            if (myLocation is IHasExteriorDoor)
            {
                IHasExteriorDoor locationWithDoor =
                                myLocation as IHasExteriorDoor;
                if (random.Next(2) == 1)
                    myLocation = locationWithDoor.DoorLocation;
            }
            int rand = random.Next(myLocation.Exits.Length);
            myLocation = myLocation.Exits[rand];
            if (myLocation is IHidingPlace)
                hidden = true;
        }
    }

I don't really understand why that random.Next() will give me 1 and 0 but mine will not.

I've tried to do some googling and I have found a bunch of folks having issues with threading. Where they are creating multiple Random objects that end up with the same seed, thus receiving identical random numbers. However, I don't think that is what is happening here.

Any insight on what is happening here? This is the link to my GitHub so you can see the whole project if that helps.

Thank you in advance for the help!

Upvotes: 1

Views: 634

Answers (1)

Talbert1209
Talbert1209

Reputation: 175

I'm really sorry. This is embarrassing. My problem was that I had my break point on the line with the random.Next(2) so every time I checked it it was always zero. Again super sorry to waste people's time and thanks for the suggestions.

Upvotes: 1

Related Questions