Timer and random numbers

Started by
3 comments, last by GamerYZ 17 years, 4 months ago
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.
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().
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.
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?
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".

This topic is closed to new replies.

Advertisement