Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help on Random functions

Started by neilski_2003 Nov 27, 2004 at 10:14 AM 2 replies 700+ views
Original Post
neilski_2003
neilski_2003
Could anyone help, or point me in the direction of some for random functions please. I'm doing an asteroids environment and i wish to generate them randomly within the environement. I would also like to have them rotate about a random axis. Cheers in advance. Neil
Arthenor
Arthenor
If you are looking for a function to generate random values, there is srand() and rand().

srand seeds the random number generator with a value (such as the current time) and rand returns a random number within the range 0 and RAND_MAX.
neilski_2003
neilski_2003
Sorry i'm relatively new to this game so could you give me an example of how i'd use the srand() function.

The idea is that i've got an asteroid and i'd like to set it so that a random translation is put on them so they appear at random places in the world.

Once i've done that i also hope to nest in some random rotation but i'll need to understand the format of how random works before i could do that.

Cheers again
Neil
andyZER0
andyZER0
srand() uses a "seed" to generate more random numbers. Before you start generating random numbers, it's a good idea to srand(time(0)). This takes the current time as a seed, thus, making a more random number. Then the value rand()%(n) will generate a number between 0 and n.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.