generating random numbers in c++
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
int n = (rand()%12) + 1;
--
Float like a butterfly, bite like a crocodile.
--
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
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
#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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement