🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

generating random numbers in c++

Started by
4 comments, last by Popper_Demi 24 years, 1 month ago
how would i go about generating a random number in c++? for example, how would i generate a number that would be in the range of 1 and 12? Thanx in advance ~Popper_Demi
Advertisement
int n = (rand()%12) + 1;

--
Float like a butterfly, bite like a crocodile.

--Float like a butterfly, bite like a crocodile.
One other thing you need to know: rand() is not truely
random. It gives the same sequence of numbers for a given "seed". You can set the seed with srand(int seed). This fact is good for debugging; it''s nice to see aliens appear in the same order to track down bugs, however, you generally want to base the seed on something that changes each time you run the program: The current time, process/thread ID, etc.

-- Pryankster
-- Pryankster(Check out my game, a work in progress: DigiBot)
Here's an example code for changing seeds:
#include time.h
srand(time( NULL ) );
call the rand() fuction.


Edited by - Iceman on May 15, 2000 8:57:27 AM

Edited by - Iceman on May 15, 2000 8:57:55 AM
thnx a lot guys for all ur help, i realli appreciate it.
Theres a flaming post about this over at the OpenGL Beginner''s forum

http://www.opengl.org

the link to the forum is in the right column

This topic is closed to new replies.

Advertisement