What’s the difference between rand() & srand() function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What’s the difference between rand() & srand() function?

4th Jul 2020, 8:58 AM
WaYR🇧🇩
WaYR🇧🇩 - avatar
1 Answer
+ 1
The function rand() is used to generate the pseudo random number. It returns an integer value and its range is from 0 to rand_max i.e 32767. syntax of rand() : int rand(void); Example #include <stdio.h> #include<stdlib.h> int main() {    printf("%d\n", rand());     printf("%d", rand());    return 0; } Output 1804289383 846930886 The function srand() is used to initialize the generated pseudo random number by rand() function. It does not return anything. syntax of srand() : void srand(unsigned int number); Example #include <stdio.h> #include<stdlib.h> #include<time.h> int main() {    srand(time(NULL));    printf("%d\n", rand());     srand(12);     printf("%d", rand());    return 0; } Output 1432462941 1687063760
4th Jul 2020, 9:24 AM
123