random number generation 2

Started by
7 comments, last by bioagentX 21 years, 1 month ago
I wrote a similar post to this earlier, but I''m having a problem with it again. I now know how to generate random numbers, but when I created my random number generator, it always generates the same random number each time that I execute the program. I know this is a common problem with random numbers but if someone could please help me fix it, that would really be a big help. -bioagentX
There are three types of people in this world, those who can count, and those who can't
Advertisement
include time.h and use srand(time(NULL))

-~-The Cow of Darkness-~-
-~-The Cow of Darkness-~-
Call a function like this, so that each time you call it the seed is changed, therefore yielding a different sequence.


  int RandomLocationForComputerOpponent(){	unsigned int randSeed = (unsigned int) time(NULL);	srand(randSeed);	return rand();}  


Still not entirely random, but a slightly more convincing solution.
You don''t even have to do that much.

Just add the include at the top of your program:

#include <ctime>

..and add this at the beginning of your int main function:

srand(time(0));

It only has to be called once, using the current time as the seed.

(silencer)
Mathematix: No no no. You should *not* call srand everytime you need a random number. That is a definite don''t.
Unless your program is a server running for quite some time calling srand once during program initialization is the best way to go.
(Of course, you should make sure you use a different seed value for srand every time the program starts, but the srand(time(NULL)) is an absolutely viable solution).

-Neophyte
quote:Original post by Neophyte
Mathematix: No no no. You should *not* call srand everytime you need a random number. That is a definite don''t.
Unless your program is a server running for quite some time calling srand once during program initialization is the best way to go.
(Of course, you should make sure you use a different seed value for srand every time the program starts, but the srand(time(NULL)) is an absolutely viable solution).

-Neophyte


Err, I did state it was not the perfect solution. Even so, it''s not a crime to do so.

quote:Original post by Mathematix
Err, I did state it was not the perfect solution. Even so, it''s not a crime to do so.

Not a crime, maybe, but a good way to ensure that you don''t get the results you want and expect. If such a function is called several times in rapid succession, the system time will not have time to change (it measures, what, milliseconds? - A very long interval to a computer!), and so the random number generator is reseeded with the same value, meaning that the function returns the same number.
Yes, you''re right. But nobody said that the function was called in rapid succession. For the game that I wrote this for as a temporary measure, the function is called at times greater than 5 seconds-ish.

It produces fair results.
That''s as may be; you can get better results and a faster function by simply avoiding reseeding ...

This topic is closed to new replies.

Advertisement