### random number

Hello !

I'm knew here , and having a problem .

I'm trying to make program that picks numbers between 1 and 100 , and lets the user guess what the number is .

 ``1234567891011121314151617181920212223242526272829303132333435`` `````` #include #include #include using namespace std; int main() { int t,guessing ; cin>>guessing; srand(time(NULL)); for(int i = 1; i < 100; ++i ) { t=rand; cout<< t<t){ cout <<"too high"; } } } ``````

when I try to build it , it always says (invalid conversion from int to int )

what should I do to solve this problem ?

Last edited on
To call rand you have to put parenthesis after it. `rand()`
`rand()` will return a number between 0 and RAND_MAX. To get a random number between 1 and 100: `rand() % 100 + 1;`
thank you very much peter & dash

it works fine now

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include using namespace std; int main() { int t,guessing ; cin>>guessing; srand(time(NULL)); t=rand() % 100 + 1; cout<< t<t){ cout <<"too high"; } else if (guessing

---------------------------------

`% 100 + 1;`

but why do we have to put `+1`

what the use of it
the function `rand()` as mentioned above generates a number between 0 and RAND_MAX. By changing it to `rand() % 100`, 100 is used instead of RAND_MAX thus the number generated will vary between 0 to 99 (100 numbers). So you add 1 to the result so that it's 1 to 100.
Last edited on
OH it starts from 0 not 1

thank you I appreciate your help
Last edited on
Topic archived. No new replies allowed.