hi
may be this will help u out
/* rand example */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
/* initialize random generator */
srand ( time(NULL) );
/* generate some random numbers */
printf ("A number between 0 and RAND_MAX (%d): %d\n", RAND_MAX, rand());
printf ("A number between 0 and 99: %d\n", rand()%100);
printf ("A number between 20 and 29: %d\n", rand()%10+20);
return 0;
}
Output:
A number between 0 and RAND_MAX (32767): 30159
A number between 0 and 99: 72
A number between 20 and 29: 23
A good way to generate almost-true random numbers is to initialize the random algorithm using srand with the current time in seconds as parameter, as obtained from time function included in <time.h>.
And, generally, a good way to get an integer random number between a range is to perform a module (%) operation on a result provided by rand():
thus rand()%25 would be a random number between 0 and 24, both included.