Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mad4Life

Why wont the random number be random?

This topic is 6176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

since i never use random number( until now) im completely stuck with this little prob when i do rand() % 10 the number that comes up stays there forever so if i do a second random number its the same as the first! can u plz tell me how to make it random every time thx

Share this post


Link to post
Share on other sites
Advertisement
use:

srand((unsigned int)time(NULL));

This will seed the pseudo-random number generator with the system time.

Note that you must also include the headers time.h and stdlib.h.

Edited by - Tab on September 22, 2001 1:41:02 PM

Share this post


Link to post
Share on other sites
will that give me a number between 0 and the time?

if so that wont do cuz i need a number from 0-1000 often times

for the header you mean time.h?

Share this post


Link to post
Share on other sites
First include time.h. Then type this in your main function before do any random things: srand(time(NULL));

Then you can do the rand()%10 which would give you a random number from 0 to 9.

Edited by - IronFist on September 22, 2001 1:43:10 PM

Share this post


Link to post
Share on other sites
why would i put it in the main function when im making a random number in several other classes?

should i put srand in the header files with the classes?

Share this post


Link to post
Share on other sites
Hmmm. Thats a good question. I am not quite sure. Hopefully someone else can tell you. But if I were you, I would just experiment with it. Try putting the srand function in the header files with your classes like you said, and if that doesn''t work, try putting it somewhere else.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!