[Solved] Need help with using srand() function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[Solved] Need help with using srand() function

I cannot get this code to compile. I just saw the example given by the one of the lessons on functions, and seem to be having a strange error when compiling. https://code.sololearn.com/cvh30OCx3cDG/?ref=app

19th May 2017, 7:43 PM
Nik
Nik - avatar
3 Answers
+ 7
change to #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main () { srand(time(0)); int counter = 100; while(--counter) cout << 1 + (rand() % 6) << endl; } srand() should be used once from there you use rand() to generate random numbers
19th May 2017, 7:53 PM
Burey
Burey - avatar
+ 7
you welcome ;)
19th May 2017, 7:56 PM
Burey
Burey - avatar
+ 2
Thank you!
19th May 2017, 7:54 PM
Nik
Nik - avatar