What is seed value? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

What is seed value?

what is seed value in srand() function? what is the purpose of it in rand()function?

4th Jul 2018, 4:59 PM
Harshada Suryawanshi
Harshada Suryawanshi - avatar
4 Réponses
+ 10
So standard way of seeding srand() function would be using time() function by passing parameter of 0 to time() to set the current time.So seed time(0) is used in srand(). Hence, srand(time(0)) would take current time as seed to pseudo random generator algorithm used by rand() function. The time() function would return time_t value which is considered to be of integral type for POSIx (portable operating system inteface) for example like the OS UNIX. One big note is to kept that one mustn't repeteadly seed or reseed the srand() if it is required to generate new set of random numbers as srand () is not thread safe and at system level ,the program thread would interfere with other multiple threads of the same data structures to the process of same objects. Hence srand() function needs to be seeded only once prior to the call of rand() function!!!
4th Jul 2018, 8:28 PM
D-Key
D-Key - avatar
+ 8
1 short answer and 1 detailed answer. Hope it clarifies!! ONE SENTENCE ANSWER:- The seed value in srand() function is used to initiate or set the pseudo random number generator used by rand() function for generating sequence of random numbers from its pseudo random number algorithm. DETAILED ANSWER:- The srand() function seeds the pseudo random number generator used by the rand(). So srand(1) would seed the rand() to generate same sequence of values as if rand() function is used before any calls made to srand() and resulting in generation of same sequence values. The srand() function returns nothing so void and considers passing parameters of unsigned numbers as its seeds. The rand () function on other hand returns integer value and considers no arguments to its function. So to generate different sequence of values or random numbers for rand() ,one must seed srand() with different values.
4th Jul 2018, 8:06 PM
D-Key
D-Key - avatar
+ 3
It is used to set the staring value The numbers are generated by a mathematical algorithm which when given a starting number (called the "seed"), always generates the same sequence of numbers. Read this for more detail: https://mathbits.com/MathBits/CompSci/LibraryFunc/rand.htm
4th Jul 2018, 5:03 PM
Agent
Agent - avatar
0
https://www.sololearn.com/discuss/122840/?ref=app https://www.sololearn.com/discuss/247809/?ref=app https://www.sololearn.com/discuss/960641/?ref=app Try using the search function first. Good luck!
4th Jul 2018, 5:23 PM
Zeke Williams
Zeke Williams - avatar