Random number generator...
Not sure if my question is a newbie one or not (which I''m sure means that it is...) but I''m looking for a simple (read: fast) bit of C/C++ code to generate a (psuedo-)random integer.
Thanks, any help would be appreciated!
Ryan.
Ryan.
In stdlib.h there are functions called random() and rand().
rand() or random() % NUM will give you a number from 0 to NUM-1.
Which one you use depends on your compiler. I think MSVC only uses one or the other. g++ will recoginize both.
rand() or random() % NUM will give you a number from 0 to NUM-1.
Which one you use depends on your compiler. I think MSVC only uses one or the other. g++ will recoginize both.
I tried that in one of my c++ programs, but
it keeps returning 41 each time, what''s wrong?
it keeps returning 41 each time, what''s wrong?
Its because you forgot to initialize the randomizer! you have to choose a seed for the real random numbers. Call first of all randomize and pass as a parameter the current time. It will work!
-Chronno-
-Chronno-
-Chronno-
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement