Generating Random Numbers

s0ftware:
Visual C++ 2008 Express

inf0:
Console Project

c0de:
1
2
3
4
	srand ( time (NULL) );
	valueX = rand()%LIMIT+9; // Must start higher than 10
	valueY = rand()%LIMIT+0;
	valueZ = rand()%LIMIT+0;


LIMIT = 500

questi0n:
I'm trying to write a program that would randomly generate 3-digit numbers (000) and display the result. Also, the 3-digit numbers is the time elapsed in milliseconds on the user's computer. This program is running fine, however, the randomly generated numbers only changes every second, would it be possible to use
srand ( time (NULL) );
generate a different random number every milliseconds instead?
Last edited on
Don't use srand() more then once. Do something like this:

1
2
3
4
5
6
7
8
//includes blah
int main() {
    srand(time(NULL));
    for(int i = 0; i < 10; ++i) {
        myrandfunc();
    }
    return 0;
}
So is it possible to generate a random 3-digit number every milli-second instead of second?
Yes, as long as you only call srand ONCE, EVER, in your program. firedraco's code will easily generate a thousand random numbers in a second. Give it a try!
valueX = rand()%LIMIT+9; // Must start higher than 9, include 9
Last edited on
Topic archived. No new replies allowed.