• Advertisement
Sign in to follow this  

Random movement for each object

This topic is 2757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like for my enemy objects to have a bit of a random movement using the code below.


Random random = new Random((int)time);


//Sway our ship moving left sometimes to moving right sometimes
int offset = random.Next(70, 120);
double degrees = time * offset;
double radians = MathHelper.ToRadians((float)degrees);
float sin = (float)Math.Sin(radians);
//Create a random range to all
if (sin > 0)
m_move.X = m_speed;
else if (sin < 0)
m_move.X = -m_speed;


My enemy objects are stored in an array, and the code above is the in a method called for each enemy in the array.

The result is all my enemy objects follow the exact same movements.

Share this post


Link to post
Share on other sites
Advertisement
Need a bit more context. Are you instantiating an instance of Random for each ship using the same seed? Whats the resolution on the time stored in the time variable?

Share this post


Link to post
Share on other sites
Use a single pseudo-random number generator. Seed it once at the beginning of the program and use it as many times as you want.

Share this post


Link to post
Share on other sites
Thank you. Yea, moving the creation of the random generator into the constructor of my class helped.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement