Sign in to follow this  
MCeltic33

Timer and random numbers

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this