I am experiencing some very odd behavior when using RANDOM to randomly pick a direction for my BOTs to move in (making a game)...
Specifically - I have a an object (BOT) that have an object (BRAIN) has a private member - the class BRAIN is responsible for telling the BOT where to go (what direction to move in), typically it is expected to move any a RANDOM direction (defined by MoveDirection)...
This is the code I use:
So, this code returns a random choice of MoveDirection (one of 4 possible options: Up,Down,Left, or Right) as expected (so far so good)...
And the bots move accordingly (so they move randomly - which is great).
Now here is the joke, each object BOT has its own instance of BRAIN (public BRAIN AI = null so I would assume that each BOT would move randomly, independent of the other, but I have noticed (been watching for a while, loaded 4 bots to make sure) that they all move in perfect synch. Which leads me to believe the RANDOM keeps returning the same values for each instance of BOT... Coincidence over 20mintes?
Why would my Random be the same for each BOT all the time, this code is executed a LOT (each tick they need to determine a new random direction to move in) and yet they all move in the exact same direction all the time... I don't get it ... Should I NOT be using RANDOM? Is there something I am missing?
Any ideas, hints, and help would be greatly appreciated, thanks
Specifically - I have a an object (BOT) that have an object (BRAIN) has a private member - the class BRAIN is responsible for telling the BOT where to go (what direction to move in), typically it is expected to move any a RANDOM direction (defined by MoveDirection)...
This is the code I use:
Code:
Random rand = new Random();
botDirection = (MoveDirection)(rand.Next() % 4);
So, this code returns a random choice of MoveDirection (one of 4 possible options: Up,Down,Left, or Right) as expected (so far so good)...
And the bots move accordingly (so they move randomly - which is great).
Now here is the joke, each object BOT has its own instance of BRAIN (public BRAIN AI = null so I would assume that each BOT would move randomly, independent of the other, but I have noticed (been watching for a while, loaded 4 bots to make sure) that they all move in perfect synch. Which leads me to believe the RANDOM keeps returning the same values for each instance of BOT... Coincidence over 20mintes?
Why would my Random be the same for each BOT all the time, this code is executed a LOT (each tick they need to determine a new random direction to move in) and yet they all move in the exact same direction all the time... I don't get it ... Should I NOT be using RANDOM? Is there something I am missing?
Any ideas, hints, and help would be greatly appreciated, thanks