Hiyas,
Got a question about the rand() function. If I want to generate a random number between a range of say 0 - x how would this function look like?
I'm using the gcc/g++ compiler but it doesn't seem to know what RAND_MAX is (which I believe is the largest random number possible). Of course I will include time.h and based the seed on time, i.e. srand(time);Code:#include <stdlib.h> #include <stdio.h> #define SEED 12345 main() { float x; int n; srand(SEED); x = rand() / RAND_MAX; n = 1 + (int) (6 * x); printf("x = %d", n); }
Update: Just found the solution I believe - http://www.cprogramming.com/tutorial/random.html
Cheers, Mike


LinkBack URL
About LinkBacks
Reply With Quote
That link should be more than enough for most needs though, i've added it to my bookmarks ty
