whats the difference between "rand" and "srand" function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

whats the difference between "rand" and "srand" function?

rand() and srand()

26th Jan 2018, 11:54 AM
sina ep
sina ep - avatar
3 Answers
+ 1
you can't create truly random numbers, so you rely on some outside source of randomness to get random enough number. rand() function simply returns a number that is based on what is seeded in stl. srand() helps you seed rand(). So you could use something like this srand(ctime(0)); rand(); rand(); rand(); and now your random will depend on current time of your system. Similarly you could use something else to seed it other than ctime.
26th Jan 2018, 2:44 PM
shobhit
shobhit - avatar
+ 1
that was a clear answer thanks!
26th Jan 2018, 7:14 PM
sina ep
sina ep - avatar
- 1
11th Mar 2018, 5:38 AM
Seyed Khalil Hoseini
Seyed Khalil Hoseini - avatar