Advertisement

Random number generator...

Started by February 29, 2000 09:27 PM
4 comments, last by verbalwords 25 years ago
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.
Advertisement
Simpler than I thought -- thanks a lot!

Ryan.
I tried that in one of my c++ programs, but
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-
I remember once paging through a book called "Inner Loops" that had some good (very fast and fairly unpredictable) random number generators in assembly - If I can re-find the book I''ll post the code.

This topic is closed to new replies.

Advertisement