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;
}
Chances are you are repeatedly creating a new
Randomobject in your code – instead create theRandomobject once only (i.e. by making it static or passing it as a parameter)Since the
Randomdefault constructor uses the current time as initial seed and all instances ofRandomwith the same seed create the same sequence of numbers creating newRandomobjects in fast order might produce the same exact sequence of numbers. This sounds like what you are seeing.