Random not really very Random? [C# 2005]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
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:
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
 
Every computer-based algorithm cannot be considered 100% random because it's generates sequence of pseudo-random values. If you want to improve randomness while using Random class use the overloaded version of ctor which accepts an integer as a "seed" value.

Shaitan00 said:
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:
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
 
Igor Sukhov is correct. I believe the default seed for the Random object is the TickCount it was created on. Since all your objects are probably created at the same time, they all have the same seed. Thus given a standard algorithm to calculate a random number they will all follow the same sequence. You would be better storing the Random object so that it is globally accessible, whether this be through static (shared) declaration or by reference. That way each object will call Next() getting a differen't number.
 
Back
Top