Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bioagentX

random number generation 2

This topic is 5655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!