Skip to main content
gamedev.net
🔒 Locked

Random number generator...

Started by verbalwords Feb 29, 2000 at 9:27 PM 4 replies 450+ views
Original Post
verbalwords
verbalwords
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.
SiCrane
SiCrane
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.
Noodles
Noodles
I tried that in one of my c++ programs, but
it keeps returning 41 each time, what''s wrong?
chronno
chronno
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-
KaneTHP
KaneTHP
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.