c++ srand() not working -


i didn't understand said here: srand(time(null)) doesn't change seed value quick enough , supposed use srand() generate different random number every time keep getting same number, suggestions?

#include <iostream>  #include <time.h> using namespace std;  int main() {     int n, num;     cout<<"enter number:\n";     cout<<"n= "<<n<<endl;     cin>>num;      srand(time(null));     n= rand()%10+1;      if(n==num)         cout<<"win\n";     else         cout<<"lose\n";       return 0; } 

cout<<"n= "<<n<<endl; 

here printing n before initialization exhibits undefined behavior.

solution
print after n= rand()%10+1;


Comments

Popular posts from this blog

wireshark - USB mapping with python -

c++ - nodejs socket.io closes connection before upgrading to websocket -

Deploying Qt Application on Android is really slow? -