C examples for Data Type:Random Number
Use srand() function to generate random number
#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); }