• Advertisement
Sign in to follow this  

Timer and random numbers

This topic is 4061 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 am trying to get a timer to work in C++ using 05 vs.net. Basically a while loop of (timer < 2 seconds) then do this... I also need help with generating random numbers from 1 - 10. This is a blank c++ project. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by MCeltic33
I also need help with generating random numbers from 1 - 10. This is a blank c++ project.
For quick 'n' dirty (pseudo-)random numbers in C++, look up the functions rand() and srand().

Share this post


Link to post
Share on other sites
you can do a simple random number generator by using:

// seed the random generator
srand(unsigned int(time(0)));

// get number between 1 - 10
int random_number = (rand() % 10) + 1;


As for a timer, you can use a simple counter with the function GetTickCount, it returns the total tick since your program started, so you'll have to do some math to convert it.

Share this post


Link to post
Share on other sites
Quote:
Original post by GamerYZ
you can do a simple random number generator by using:

// seed the random generator
srand(unsigned int(time(0)));

// get number between 1 - 10
int random_number = (rand() % 10) + 1;


As for a timer, you can use a simple counter with the function GetTickCount, it returns the total tick since your program started, so you'll have to do some math to convert it.


Are there any special headers i need for this to work?

Share this post


Link to post
Share on other sites
off top of my head, you need "time.h" for time function that is inside the srand(). I think the rest is in "windows.h".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement