This is a C Program to generate random numbers within given range. This version of code uses rand() and srand() functions. The function srand() is used to initialize the pseudo-random number generator and rand() function generates a pseudo random number.
Here is the source code of the C program to use rand and srand functions. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num;
/* Seed number for rand() */
srand((unsigned int) time(0) + getpid());
printf("\nGenerating a random number using srand and rand function.\n");
num = rand();
printf("%d\n", num);
return EXIT_SUCCESS;
}
$ gcc random.c -o random $ ./random Generating a random number using rand and srand functions. 320830841
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Apply for C Internship
- Practice BCA MCQs
- Apply for Computer Science Internship