12ice
Windows Phone App Developer
Ok so I have an XNA game that has a food object class, inside of the class there is some logic to generate a random spawn within some parameters for the object. However when the game first starts, It instantiates these objects virtually simultaneously (There is a spawn loop until a required amount of objects have spawned) that looks like this:
Inside of the objects class is this logic:
The problem being that because the Objects are calling the random class within less than milliseconds of each other Random returns the same X and Y "Random" values for each objects making the spawns not random at all. I know this is because the Random class is Pseudo random and is basing the random value off something somewhere but am I doing anything wrong ? and what can I do (Apart from pre determine the spawns)
Code:
if (gameTimer < 60000)
{
if (totalFoodObjects < 1)
{
int foodPicker = new Random().Next(1, 20);
if (foodPicker <= 20)
{
baconList.Add(new Bacon());
Bacon lastBacon = baconList.Last();
lastBacon.setup();
AddFoodObjects();
}
}
}
Inside of the objects class is this logic:
Code:
public void getStartingPosition()
{
//Thread this
Random random = new Random();
int randomX = random.Next(400, 800);
if (randomX <= 800)
{
int randomY = random.Next(-50, 300);
food1Position.Y = randomY;
food1Position.X = randomX;
subtractXAxis = -6f;
food1Speed.Y = food1Speed.Y + 0.1f;
food1Speed.X = subtractXAxis;
return;
}
....... More else If statements like this
}
The problem being that because the Objects are calling the random class within less than milliseconds of each other Random returns the same X and Y "Random" values for each objects making the spawns not random at all. I know this is because the Random class is Pseudo random and is basing the random value off something somewhere but am I doing anything wrong ? and what can I do (Apart from pre determine the spawns)