Every time I run a program with rand()
it gives me the same results.
Example:
#include <iostream>
#include <cstdlib>
using namespace std;
int random (int low, int high) {
if (low > high)
return high;
return low + (rand() % (high - low + 1));
}
int main (int argc, char* argv []) {
for (int i = 0; i < 5; i++)
cout << random (2, 5) << endl;
}
Output:
3
5
4
2
3
Each time I run the program it outputs the same numbers every time. Is there a way around this?
Source: Stackoverflow.com