Use srand() function to generate random number - C Data Type

C examples for Data Type:Random Number

Description

Use srand() function to generate random number

Demo Code

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

int main() {// w  w  w  . j  a  va2s . c  om
   int iRandomNum = 0;
   srand(123);
   iRandomNum = (rand() % 4) + 1;
   printf("\nFortune Cookie - Chapter 3\n");

   switch (iRandomNum) {
   case 1:
      printf("\n a.\n");
      break;
   case 2:
      printf("\n b.\n");
      break;
   case 3:
      printf("\n c.\n");
      break;
   case 4:
      printf("\n d.\n");
      break;
   }
   printf("%d ", (rand() % 49) + 1);
   printf("%d ", (rand() % 49) + 1);
   printf("%d ", (rand() % 49) + 1);
   printf("%d ", (rand() % 49) + 1);
   printf("%d ", (rand() % 49) + 1);
   printf("%d\n", (rand() % 49) + 1);
}

Result


Related Tutorials