The srand function in C is used to seed the random number generator. It initializes the random number generator used by the rand function. By providing a seed value, you can ensure that the sequence of random numbers generated by rand is reproducible. If you use the same seed value, you will get the same sequence of random numbers each time you run the program.
Here's a simple example:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
// Seed the random number generator with the current time
srand(time(NULL));
// Generate and print random numbers
for (int i = 0; i < 5; i++) {
printf("%d\n", rand());
}
return 0;
}
In this example, srand(time(NULL)) seeds the random number generator with the current time, ensuring that you get different random numbers each time you run the program.
