SFML how to make various shapes move in random directions?

Viewed 342

I am having troubles in making shapes moving in random directions, with the code that i currently have they all move in the same direction and then change to another direction randomly.

Here is the code

for(int i = 0; i < 50; i++)
    {
        int randomMoveX = rand() % 2 + (-1);
        int randomMoveY = rand() % 2 + (-1);
        circleObjectArray[i].move(randomMoveX,randomMoveY);
        while(randomMoveX == 0)
        {
            randomMoveX = rand() % 2 + (-1);
        }
        while(randomMoveY == 0)
        {
            randomMoveY = rand() % 2 + (-1);
        }

        cout << "randomMoveX: " << randomMoveX << endl;
        cout << "randomMoveY: " << randomMoveY << endl;
    }

How can i change my code to be able to do move them in random directions individually?

Thank you for your time :D

1 Answers

When you run your app, a seed is set for random as srand(1).

To use different value in rand you need to change the seed of random. Then use the function srand and save it in a unsigned int to use different rand for different shape.

Related