Rand Function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Rand Function

I run the rand() code snippet and the code spits out 41 each time. It seems less than random. A slight variation to the code, recompile and rerun it...same old 41. What's the deal??

16th May 2018, 6:52 AM
Martin Smith
Martin Smith - avatar
4 Answers
+ 9
Andrea Simone Costa is correct. Martin Smith This is covered on the last slide of the rand() function in the c++ lessons. https://www.sololearn.com/learn/CPlusPlus/1638/?ref=app
16th May 2018, 7:27 AM
jay
jay - avatar
16th May 2018, 8:25 AM
Shahil Ahmed
Shahil Ahmed - avatar
+ 2
rand() needs a seed. so #include <ctime> and do srand(time(NULL)); only once before using rand();
16th May 2018, 7:19 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
0
hi, share the code, please
16th May 2018, 7:04 AM
Rajeeb