Question about rand() function

Hi again,

I read in the C++ book that rand() function returns a random number and that in order to get a random number in a range that you want, the formula is:

1-6, And that rand() % 6 would return 0-6, and then the +1 would make that 1-7 though?

Also, if I run it never returns 13, only 0-12, but can't you get 13 if you divide the right number as the remainder?

Thanks again.

Re: Question about rand() function

Quote:

*Originally posted by learning C++ *
Hi again,

Hi
I read in the C++ book that rand() function returns a random number and that in order to get a random number in a range that you want, the formula is:

1-6,

Code:

`1 + `**(**rand() % 6**)** // add parens for clarity

And that rand() % 6 would return 0-6,

and then the +1 would make that 1-7 though?

No, it returns 0-5, then the +1 gives 1-6
Also, if I run

it never returns 13, only 0-12, but can't you get 13 if you divide the right number as the remainder?

Don't know what you are asking here.

The *right* number to get 0-13 is **14**
Thanks again.

Welcome