int RandomNum(int n, int nMax)
int r = 1 + rand() % nMax;
while (r == n)
r = 1 + rand() % nMax;
int _tmain(int argc, _TCHAR* argv)
for (int i = 0; i < 10; i++)
int x=RandomNum(4, 100);
std::cout << x << "\n";
Is my VS13 going wild or what because it is always outputting the same number in every execution of the code above ? Debugging shows the output display different numbers but one time run does not :(
The loop is short, and will loop over all values within a single second. That causes all your calls to
rand to start with the same seed.
srand once, preferably very early in the
You‘re specyfing the seed every time. Instead you have to do it only once.
srand(time(NULL)) to the beginning of main.