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.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs
- Buy Computer Science Books