Can anyone explain me how srand works to give different output every time | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Can anyone explain me how srand works to give different output every time

18th Apr 2018, 4:02 PM
Jithendra Suggala
3 Answers
+ 10
srand does not give you a different result every time. if you use the same srand value you will continue to get the same results each time you produce random numbers. srand basically sets up the list of random numbers to be used by defining the seed that random will use when pulling numbers. it is often set to the current time because this number is constantly changing and thus producing a new set of random numbers each use
18th Apr 2018, 7:41 PM
Michael Simnitt
Michael Simnitt - avatar
+ 2
You need to connect the <ctime> library
18th Apr 2018, 5:38 PM
Артём
Артём - avatar
+ 2
rand () uses a seed number to produce a number. srand () sets the seed number that rand uses. Using time(nullptr) makes it as close to random as possible.
18th Apr 2018, 7:32 PM
Zeke Williams
Zeke Williams - avatar