-
Can't understand FAQ
what library and command do i need to get a random number between number x and number y? I am using a DEV C++ compiler if that makes anydifference. I looked in the FAQ and they dont address this directly at least not in the search results i found under random. thanks in advance.
-
Code:
#include <cstdlib> //for rand() and srand()
#include<ctime> //for time(0)
int main()
{
srand(time(0)); //seed the random number generator
...
int num=LOWER+rand()%(UPPER-(LOWER-1));
...
}
the LOWER and UPPER are just numbers you put it... they're the upper and lower limits... for example, num=1+rand()%6; will simulate a dice roll {1..6}
EDIT: just remembered... the srand(time(0)) line will give you a warning... all you need to do to get rid of that warning is to cast the time(0) function to an unsigned int... your code will look like srand(static_cast<unsigned int>(time(0))); If you don't know what I'm talking about, don't bother with it for now...
-
-
i havent used dev c++ in a long time, but why do you have to static_cast it? why not just srand( (unsigned int) time(0) )?