Advertisement

Random Numbers...?

Started by December 20, 2000 09:36 PM
4 comments, last by RegularKid 24 years ago
I am having trouble getting a different random number everytime I run the program. I keep getting the same one. Here''s my code: #include #include #include #include int main() { long time = clock(); srand(time % INT_MAX); cout<< (rand()% 100) + 1; return 0; } What am I doing wrong?
im not familiar with those particular time functions that ur using to set the seed, but i can offer an alternate way of getting random numbers. u have to include stdlib.h and limits.h i think, and you use the randomize() function to get a random seed automatically without all that time crap, and then u can just use the rand() function to produce random numbers. works for me.
Advertisement
You could also try this:

srand((unsigned)(time(NULL)));

Don''t forget to include time.h


NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ...
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick
RegularKid, your problem is that clock() returns zero the first time it is called. After that, it returns the number of clock ticks since the last call. So, you are always seeding the random number generator with the same number, 0.

Use something like time() instead, which returns some unit of time (seconds?) since 1970 if I remember correctly. It will always be nonzero and, unless you set your clock back, will always give a new number.
clock should not return 0 the first time it is called:
"The clock function returns the implementation’s best approximation to the processor time used by the program since the beginning of an implementation-defined era related only to the program invocation."
(ANSI Sec 7.23.2.1)
In English, this means it returns the number of clock cycles elapsed since the program started.
quote: Original post by FordPrefect

clock should not return 0 the first time it is called:
"The clock function returns the implementation’s best approximation to the processor time used by the program since the beginning of an implementation-defined era related only to the program invocation."
(ANSI Sec 7.23.2.1)
In English, this means it returns the number of clock cycles elapsed since the program started.


Most implementations of clock() that I have bothered to check out (such as DJGPP) return 0 on the first invocation of clock(). I have not checked this with Visual Studio to see what Microsoft''s implementation does, but I''m guessing it is something similar.

This behavior is not prohibited by the ANSI section that you just referenced. It doesn''t say that it is based on the amount of time since the program started, just that it isn''t based on anything external to the program. Using an implementation that starts at 0 on the first call to clock is perfectly valid and fairly common.

The only thing guaranteed by the ANSI Section above is that you can''t rely on clock values being comparable between two simultaneously running programs.

This topic is closed to new replies.

Advertisement