Strange phenomenon!

I am trying to get a random number that is between 1-10. This is the code that i am using,

1
2
3
int randNum;
randNum = rand() % 10 + 1
cout << randNum;


However, the output of the program was
47864


Anyone could explain that?

Could you post the exact code? I'm guessing there's more to it than what you're showing. Partly because your second line would throw a compile error due to missing ';'.
Hi Gaminic,

Thanks for the prompt reply. I have found the fault.
1
2
3
4
5
6
for(int i=0; i < 5; i++)
          {
              int randNum;
              randNum = rand() % 10 + 1;
              cout << randNum;
          }


I realized that the for loop was giving me 5 different numbers and I didn't include a endl for my cout. Causing the output of
47864


Topic archived. No new replies allowed.