Jump to content
  • Advertisement

Archived

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

Muzlack

better time method

This topic is 5967 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

Is there a more accurate time method than time()? Because I think that I have a loop that is run through so fast that it gets the same time and it gives the same random number for that reason. But I can''t be sure of that. I do have a srand((unsigned)time(NULL); at the beginning of my game, but I get the enemies at the same state at the same time...

Share this post


Link to post
Share on other sites
Advertisement
Don''t seed srand more than once - it''ll produce consistent but spurious results - if you display the results on the screen you''ll see an obvious pattern, which means the data isn''t very random anymore.

...
A better timer on Windows is QueryPerformanceCounter - but it''s expensive, takes like 5us on fast computer, and requires you to divide the results by QueryPerformanceFrequency - which means the precision varies between machines.

There''s also the x586 op code, rdtsc, which returns a 64bit CPU tick count. If you know (or accurately measure) the clock MHz of the computer, in thoery you can get sub-nanosecond timing on a fast machine - however in practice nanosecond accuracy is difficult to obtain. Precision and (32bit) roll-over are heavily dependent upon the clock of the computer. You need to call a serializing instuction prior to rdtsc, cpuid is the most popular choice.

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!