Why " rand ()" don't work? Who knows? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why " rand ()" don't work? Who knows?

#include <iostream> using namespace std; int main(int argc, const char*argv[]) { int a; a=1+rand()%10; cout <<a<<endl; return 0; }

13th Feb 2017, 6:57 PM
Artur Aytmuratov
2 Answers
+ 2
You need to provide a seed for rand to initialize the random number generator. #include <iostream> using namespace std; int main(int argc, const char*argv[]) { int a; srand(time(NULL)); // seed rand with time a = rand() % 10 + 1; cout << a << endl; return 0; }
13th Feb 2017, 7:16 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thanks!
14th Feb 2017, 3:12 AM
Artur Aytmuratov