0

Help with code

trying to create a hi-lo guessing game , random function is not giving correct result #include<iostream> #include<ctime> #include<cstdlib> int getnumber() { int num; while(1) { std::cin>>num; if(std::cin.fail()) { std::cin.clear(); std::cin.ignore(32767,'\n'); } else return num; } } int ran_num() { int max(10),min(1); std::srand(std::time(nullptr)); static const double fraction = 1.0 / (RAND_MAX + 1.0); return min+(static_cast<int>((fraction*std::rand())*(max-min+1))); } bool playagain(char choice) { if(choice=='y'||choice=='Y') return 1; return 0; } void ischeck(int guessnum) { int usernum=ran_num(); std::cout<<usernum<<"\n"; if(usernum<guessnum) { std::cout<<"Guess is high"<<"\n"; } else if(usernum>guessnum) { std::cout<<"Guess is low"<<"\n"; } else if (usernum==guessnum) std::cout<<"Congrats you guessed correct num"<<"\n"; } int main() { for(int i=0;i<2;i++) { int n1=getnumber(); ischeck(n1); } }

14th Oct 2018, 12:52 PM
Test 123
2 Answers
+ 1
what is the range you want to generate a random number in?
14th Oct 2018, 4:05 PM
Tanay
Tanay - avatar
+ 1
int max(10),min(0); sorry for undocumented code, and i found the solution i.e we have to seed the srand(time(nullptr)) only once , because it will generate same value for same second
14th Oct 2018, 4:08 PM
Test 123