// produce a list of random integers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// create a "random" seed for random number generation
int make_seed() {
// use current time in second * processor time
time_t now = time(NULL);
clock_t ticks = clock();
return now * ticks;
}
int main(int argc, char * argv[]) {
// usage statement
if (argc < 4) {
printf("Usage: ./randomInts howMany min max [seed]\n");
printf(" randomInts will choose howMany random integers, which are each\n"
" between min and max (inclusive). If a seed is given as input\n"
" it is used to seed the pseudorandom number generator (so you can\n"
" use the same seed and get the same \"random\" list each time you\n"
" run the program. If no seed given, then a seed is used based on\n"
" the current time in seconds and the # clock cycles elapsed in the program\n"
" generating the numbers. Note - srand and rand from the C standard library\n"
" are used for generating the pseudorandom #'s.\n\n");
exit(0);
}
// get args
int howMany = atoi(argv[1]),
min = atoi(argv[2]),
max = atoi(argv[3]);
// seed the random number generator
int seed;
if (argc > 4) seed = atoi(argv[4]);
else seed = make_seed();
srand(seed);
for(int i=0; i < howMany; i++) {
// pseudorandom int
int r = rand();
// put it in the desired range
// to start, r is between 0 and RAND_MAX
r = r % (max-min+1); // max-min+1 possible values
if (min > 0)
r -= min; // minimum possible is min now
else
r += min;
printf("%d\n", r);
}
return 0;
}