Generating Random Numbers

May 1, 2009 at 6:16pm
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 May 1, 2009 at 6:31pm
May 1, 2009 at 6:43pm
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;
}
May 5, 2009 at 5:27pm
So is it possible to generate a random 3-digit number every milli-second instead of second?
May 5, 2009 at 6:14pm
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!
May 5, 2009 at 9:44pm
valueX = rand()%LIMIT+9; // Must start higher than 9, include 9
Last edited on May 5, 2009 at 9:47pm
Topic archived. No new replies allowed.