logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jul08/rand.c

/*

code from tutorialspoint's page on the rand() function in C
slightly modified by me to accept user inputs

*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (int argc, char *argv[]) {
   
   if(argc < 2){
      fprintf(stderr, "Usage: %s <number of random numbers> <maximum number>\n", argv[0]);
      exit(1);
   }


   int i, n, max;
   time_t t; //time variable is needed  to help us "seed" the random number generator
   
   n = atoi(argv[1]); //the number of numbers we want to print
   max = atoi(argv[2]);  //the biggest number we want to print

   /* Intializes random number generator */
   srand((unsigned) time(&t)); //seeding it to the computers clock

   /* Prints n random numbers from 0 to a user defined maximum */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d\n", rand() % (max + 1));
   }
   
   
   
   return 0;
}