Random Movement for multiple entities
- by opiop65
I have this code for a arraylist of entities.
All the entities use the same random and so all of them move in the same direction. How can I change it so it generates a new random number for each entity?
public void moveFemale() {
for(int i = 0; i < 1000; i++){
random = rand.nextInt(99);
}
if (random >= 0 && random <= 25) {
posX -= enemyWalkSpeed; // right
}
if (random >= 26 && random <= 50) {
posX += enemyWalkSpeed; // left
}
if (random >= 51 && random <= 75) {
posY -= enemyWalkSpeed; // up
}
if (random >= 76 && random <= 100) {
posY += enemyWalkSpeed; // down
}
}
Is this correct?
public void moveFemale() {
for (Female female: GameFrame.females){
female.lastChangedDirectionTime += elapsedTime;
if (female.lastChangedDirectionTime >= CHANGE_DIRECTION_TIME)
{
female.lastChangedDirectionTime = 0;
random = rand.nextInt(100);
if (random >= 0 && random <= 25) {
posX -= enemyWalkSpeed; // right
}
if (random >= 26 && random <= 50) {
posX += enemyWalkSpeed; // left
}
if (random >= 51 && random <= 75) {
posY -= enemyWalkSpeed; // up
}
if (random >= 76 && random <= 100) {
posY += enemyWalkSpeed; // down
}
}
}
}