I want to choose randomly from among 4 directions:
movePlayer(map, &positionPlayer, direction);
where direction is one of UP, DOWN, LEFT, and RIGHT.
I haven’t yet found out how to do this with the rand() function.
How can I do this? Do I need to assign a number to each direction and then choose a random number within those bounds?
Thank you for your help.
The simplest way to do this is to simply do a modulos of the rand function to get a numbers between 0 and 3(inclusive).
Then you can simply perform a switch statement to choose the direction
Of course, this will not be exactly random with a perfect distrubition( the modulus is somewhat of a hack).
If you want real randomness with actual distributions then you might want to look into Boost.Random.