dimanche 26 août 2018

Trying to get a random number between two numbers in C++

Ok, so i am trying to make a program to guess what the user is thinking of, the problem is with the random thingy, BUT! it just prints a number bigger than the max int by one. i.e: if max is 20, output is 21.

{
string hl;
string right;
bool found=false;
int max;
int min;
cin>>min;
cin>>max;
srand(time(NULL));
int x = rand() % max +  min;
while(found==false)
{
    cout<<x;
    cout<<"True?";
    cin>>right;
    if(right=="y"){
        found=true;
    }
    else{
        cout<<"h or l";
        cin>>hl;
    }
    if(hl=="h"){
        min=min+rand() % 3 + 6;
    }
    else if(hl=="l"){
        max=max-rand() % 3 + 6;
    }
    srand(time(NULL));
    x = rand() % min +  max;
}}

I just want to know what i am doing wrong in the random int generator (Btw i just started learning c++ so cut me some slack in this language :P)

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire