Hi,
I have made a program that generates random numbers. It takes arguments from the command line like this:
Usage: argv[0] [-n | -c [val]] [min-max] [num]
Default behaviour is to print [num] random numbers, ranging from [min] to [max], with a newline after each number. The option "-n" will cause no newlines to be printed between each value, OR, "-c val" will print an ascii value "val", between each number.
Here is the main processing function of the program (all the arguments have been checked):
Code:
/* Main function to output random numbers.
It outputs "num" random numbers, between "min" and "max",
with "ch" between each number. */
void OutputRanNums
(int ch, int min, int max, unsigned long int iterations)
{
register long int rannum;
register unsigned int diff;
register unsigned long int num = iterations;
/* Work out offset value, increment as rand()
could return 0 as lowest value */
diff = max - min;
diff++;
/* Any better ideas? */
srand(time(NULL));
for( ; num>0 ; num-- )
{
rannum = rand();
rannum %= diff;
rannum += min;
printf("%ld",rannum);
if( ch )
printf("%c",ch);
}
fflush(stdout);
}
I wonder if any bit shifting could be done (or any other tricks) to speed this up...