In next code I've same result everytime although using "srand" and followed instructions of the next link rand() and srand() in C/C++:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void)
{
int i, x;
srand(5);
for(i=1;i<=10;++i)
{
x=rand()%5;
cout << x << endl;
}
return 0;
}
The reason you are getting the same result is that you are always passing the same value (5) to srand
.
As the linked page notes, srand(time(0))
is a common way to get the seed to vary.