What is the difference between srand() and rand() function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference between srand() and rand() function?

4th Mar 2018, 6:28 PM
Ipshita Pal
Ipshita Pal - avatar
2 Answers
+ 3
Rand()----> 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. Srand()----> 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.
5th Mar 2018, 9:37 AM
Prabhat Thakur
Prabhat Thakur - avatar
+ 1
RAND: Rand uses a multiplicative congruential random number generator with period232 to return successive pseudo-random numbers in the range 0 to RAND_MAX. Return Value: Rand returns the generated pseudo-random number. SRAND(): The random number generator is reinitialized by calling srand with an argument value of 1.The generator can be set to a new starting point by calling srand with a given seed number.
4th Mar 2018, 6:37 PM
Sudarshan Rai
Sudarshan Rai - avatar