Monday, July 27, 2009

What is the use of srand() function in c language???

please explain the use of srand() function in c language

What is the use of srand() function in c language???
srand is the function to initialize/reinitialize the seed of random number generator.





From C std library doc:


Header File





stdlib.h





Category





Math Routines





Prototype





void srand(unsigned seed);





Description





Initializes random number generator.





The random number generator is reinitialized by calling srand with an argument value of 1. It can be set to a new starting point by calling srand with a given seed number.





Return Value





None.
Reply:you should ask it in computer%26amp;internet category
Reply:if you have a program that uses the rand() function to generate random numbers the srand() function will initialize the starting point of the sequence.


Imagine the random number generator like a list of numbers that are evenly distribuited across a range. Something like





4


3


5


1


2





Now, if you don't call srand() and start your program your first rand() will always return 4, the second 3 and so on.





If you call srand(1) at the start of the program ,the first rand() will return 3, the second 5 and so on.





Most of the time, srand() gets initialized with the time in seconds since the start of the pc or something similar, this gives the impression that the "randomness" is real and not mathematically generated in a predictable way because at every run the srand() "seed" will be different.


No comments:

Post a Comment