rand() function is used in C to generate random numbers. If we generate a sequence of random number with rand() function, it will create the same sequence again and again every time program runs. Say if we are generating 5 random numbers in C with the help of rand() in a loop, then every time we compile and run the program our output must be the same sequence of numbers.
int rand(void): returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX: is a constant whose default value may vary between implementations but it is granted to be at least 32767.
NOTE: This program will create same sequence of random numbers on every program run.
453 1276 3425 89
453 1276 3425 89
453 1276 3425 89
The srand() function sets the starting point for producing a series of pseudo-random integers. If srand() is not called, the rand() seed is set as if srand(1) were called at program start. Any other value for seed sets the generator to a different starting point.
void srand( unsigned seed ): Seeds the pseudo-random number generator used by rand() with the value seed.
Note: The pseudo-random number generator should only be seeded once, before any calls to rand(), and the start of the program. It should not be repeatedly seeded, or reseeded every time you wish to generate a new batch of pseudo-random numbers.
Standard practice is to use the result of a call to srand(time(0)) as the seed. However, time() returns a time_t value which vary everytime and hence the pseudo-random number vary for every program call.
NOTE: This program will create different sequence of random numbers on every program run.
453 1432 325 89
8976 21234 45 8975
563 9873 12321 24132
How srand() and rand() are related to each other?
srand() sets the seed which is used by rand to generate “random” numbers. If you don’t call srand before your first call to rand, it’s as if you had called srand(1) to set the seed to one.
In short, srand() — Set Seed for rand() Function.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Output of C programs | Set 33 (rand() and srand())
- Guess Game using rand() and srand() in C
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- What are the differences between bitwise and logical AND operators in C/C++?
- fesetround() and fegetround() in C++ and their application
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Get first and last elements from Array and Vector in CPP
- Similarities and Differences between Ruby and C language
- Code Optimization Technique (logical AND and logical OR)
- Similarities and Differences between Ruby and C++
- Generate an array of given size with equal count and sum of odd and even numbers
- Similarities and Difference between Java and C++
- Getting System and Process Information Using C Programming and Shell in Linux
- INT_MAX and INT_MIN in C/C++ and Applications
- Difference and Similarities between PHP and C
- Change/add only one character and print '*' exactly 20 times
- Comma in C and C++
- Return values of printf() and scanf() in C/C++
- What is return type of getchar(), fgetc() and getc() ?
Improved By : avinashavi0941