Random movement for each object

Started by
2 comments, last by zorocke 13 years, 6 months ago
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.
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?
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.

Thank you. Yea, moving the creation of the random generator into the constructor of my class helped.

This topic is closed to new replies.

Advertisement