Reputation: 1535
I'm developing a small XNA GAME,
for (int birdCount = 0; birdCount < 20; birdCount++)
{
Bird bird = new Bird();
bird.AddSpriteSheet(bird.CurrentState, birdSheet);
BIRDS.Add(bird);
}
The code above runs at Load function, BIRDS is the List where all Bird's are held.
The bird constructor customize the bird randomly. If I run the code breakPoint by breakPoint the random function generates different values, but if i do not stop the code and leave program running all random values become same so that all of birds become same.
How can i solve this problem ?
the code for random and seeds:
private void randomize()
{
Random seedRandom = new Random();
Random random = new Random(seedRandom.Next(100));
Random random2 = new Random(seedRandom.Next(150));
this.CurrentFrame = random.Next(0, this.textures[CurrentState].TotalFrameNumber - 1);
float scaleFactor = (float)random2.Next(50, 150) / 100;
this.Scale = new Vector2(scaleFactor, scaleFactor);
// more codes ...
this.Speed = new Vector2(2f * Scale.X, 0);
this.Acceleration = Vector2.Zero;
}
Upvotes: 0
Views: 2390
Reputation: 160922
Chances are you are repeatedly creating a new Random
object in your code - instead create the Random
object once only (i.e. by making it static or passing it as a parameter)
Since the Random
default constructor uses the current time as initial seed and all instances of Random
with the same seed create the same sequence of numbers creating new Random
objects in fast order might produce the same exact sequence of numbers. This sounds like what you are seeing.
Upvotes: 6