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

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -