glitch or ...?

#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
Last edited on
When you use rand() it has a predefined list of random numbers
use #include <ctime> and srand(time(NULL)) before rand()
Last edited on
thanks. srand(time(NULL)) causes the program to start reading off a random point off the rand() list right?
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.
Topic archived. No new replies allowed.