+ 1

What is seed value in srand function ? - C++

To use rand() function properly, srand() function is used which provides seed value to the rand() function. My question is what does providing a seed value to rand() function means and why does rand() works properly if seed value is provided to it ?

28th Dec 2016, 1:08 PM
Yousaf
Yousaf - avatar
2 odpowiedzi
+ 6
Imagine you can actually see the algorithm behind rand() function, e.g. rand() takes a value and perform multiplication, division, etc. We call that original value as a seed. srand() function alters that value which is used by rand() to perform the algorithm of generating random numbers. This means that srand() alters the seed. e.g. srand(1); cout << rand(); //outputs first random number srand(2); cout << rand(); //outputs second random number --- In order for the seed to be really randomises, we utilise time(), since the system time alternates every second. srand(time(0));
28th Dec 2016, 1:22 PM
Hatsy Rei
Hatsy Rei - avatar
0
Thanks for the response. I have a better understanding of it now.
28th Dec 2016, 1:30 PM
Yousaf
Yousaf - avatar