Random Movement for multiple entities

Posted by opiop65 on Game Development See other posts from Game Development or by opiop65
Published on 2012-10-28T21:59:39Z Indexed on 2012/10/28 23:23 UTC
Read the original article Hit count: 227

Filed under:
|

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

© Game Development or respective owner

Related posts about java

Related posts about random