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; }
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;
}
+ 1
Thanks!