Generate random number in C Programming

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() 
{
    int random;
    // Initialize random seed using current time
    srand(time(NULL)); 
    // Generate random integer between 0 and 99
    random = rand() % 100; 
    printf("Random number: %d\n", random);
    return 0;
}

Explanation

  • The stdlib.h header provides the rand() and srand() functions for generating random numbers.
  • The time.h header provides the time() function for obtaining the current time.
  • The srand() function is used to initialize the random number generator with the current time as the seed value.
  • The rand() function is used to generate a random number.
  • The modulo operator % is used to restrict the range of the generated number to between 0 and 99.
  • The generated random number is stored in the variable random.
  • The program prints the generated random number to the console using the printf() function.