# Random Movement for multiple entities

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
}
}
}
}

• Sorry -- what is the purpose of your for loop? I don't think you need that? What does the 1000 refer to? Oct 28, 2012 at 22:05
• its a crude timer that only allows random to generate a new number after 1000 has been hit. I know its bad but I can't figure out a timer.
– user20439
Oct 28, 2012 at 22:07
• Ouch, that's bound to cause issues. Let me think.. what are you trying to achieve with that, game-mechanics-wise? Is this to change direction only after some time has passed? Oct 28, 2012 at 22:08
• Yeah but anyway... How would I fix the random movement? All the entities move the same way. I honestly have no idea how to fix it.
– user20439
Oct 28, 2012 at 22:10

## 1 Answer

Your entities ("females") need to have a public member on them, something like

public int lastChangedDirectionTime = 0; //milliseconds


You also need some time interval at which you want them to change direction, set up as a constant:

public final int CHANGE_DIRECTION_PERIOD = 1000; //milliseconds


As each game loop goes by, get the amount of time that has elapsed since the last game loop iteration (if you don't have this value, comment!), and do

for (Entity entity : entities) //every one!
{
entity.lastChangeDirectionTime += timeElapsed; //presumably both in the same time unit eg. seconds

}


As soon as you've done the addition, do this:

if (entity.lastChangeDirectionTime >= CHANGE_DIRECTION_PERIOD)
{
entity.lastChangeDirectionTime = 0;

//do your directional change logic, here.
}


I'm not 100% sure of your structure so you'll need to figure out where to slot these into your code.

• Thanks so much for the answer, but I get this error when try to to for(Females female : females): Can only iterate over an array or an instance of java.lang.Iterable I have no idea what this means at all, I've never actually dealt with this particular for loop before.
– user20439
Oct 28, 2012 at 22:25
• Not to worry. First I see your typename is Females with an 'S', is that right? Secondly a for-each loop like I have there is just the same as saying for (int c = 0; c < entities.length; c++) { Entity entity == entities[i]; //etc. } No difference, for-each is just a bit quicker to type. Oct 28, 2012 at 22:29
• ok so if my arraylist is in my GameFrame class, the for loop would be like this?: for (GameFrame.Female female:females) Is that correct?
– user20439
Oct 28, 2012 at 22:35
• My arraylist in gameframe: static ArrayList<Female> females = new ArrayList<Female>();
– user20439
Oct 28, 2012 at 22:35
• Ah, you have an ArrayList. Yes, this is why I said, "Females with an 'S'" right? Because you had a typo, before. Try again with for (Female female : females) That should work fine, because ArrayList adheres to java.lang.Iterable as mentioned before. Read that colon as "in", in your mind, and it will make sense as you type it. Oct 28, 2012 at 22:39