#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
int x;
x=1+(rand()%500);
cout<<x;
system("pause");
return 0;
}
For some reason, this program always outs 42 for me when I try debugging. Is it something wrong with the program or with Microsoft Visual studio?
edit: tried a loop with this, always gets 42, 468, 335, 1, 170, 225, 479, 359, 463, 465..... I feel theres something wrong with this random number generator
In general, you should never blame the compiler - fault almost always lies with your own code. If you google "c++ rand always returns same number" you can probably get your answer, but I'll give it to you.
You need to call 'srand(...)' before using rand(). Usually you would do so like: srand ( time(NULL) ); so that it changes every time you run the application.