What exactly is the seed value that is the parameter of the srand function? It hasn't been clearly defined to me. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What exactly is the seed value that is the parameter of the srand function? It hasn't been clearly defined to me.

5th Jun 2019, 9:52 PM
Guerschom Tambwe
Guerschom Tambwe - avatar
2 Answers
0
A seed is the start value. Like in gardening the seed is what starts the plant. The difference is in random number generators, they aren't actually random. They follow an algorithm. If you and a friend put the same seed into the same algorithm you will get the same random sequence back. Because the algorithm started in the same place for both. So random seeds like using the current timestamp are preferred. An example of this is minecraft which let's you enter a seed value to generate a world map. The same seed creates the same world every time.
6th Jun 2019, 12:59 AM
Adam
Adam - avatar
0
Adam, thank you for your reply. I see the analogy but I don't understand what effect(s) srand's seed parameter has on rand(). What happens if you completely omit this perameter and simply use the code "cout<<1 × rand() %6;" (of course assuming you've inserted the correct includes and placed it within the main function)? I know it generates a number in the range 1-6, but what difference does the seed bring to the output of the example code I mentioned above?
6th Jun 2019, 1:25 AM
Guerschom Tambwe
Guerschom Tambwe - avatar