I'm trying to populate a simple array in C++ using a function that generates random numbers within a given range. For whatever reason it is giving me the same random number for each element. I think the problem has something to do with where I'm seeding the random variable.
Any ideas?
#include <iostream> // for user input/output
#include <cstdlib> // for rand/srand functions
#include <ctime> // for time usage
using namespace std;
const int SIZE = 10;
const int MIN = 100;
const int MAX = 200;
int main()
{
int Arr[SIZE];
for (int i = 0; i < SIZE; i++){
Arr[i] = rng(MIN, MAX);
}
for (int j = 0; j < SIZE; j++){
cout << Arr[j] << " ";
}
}
int rng(int lo, int hi){
static bool variable;
variable = false;
if (variable == false){
srand(time(0));
variable = true;
}
return rand() % (hi - lo + 1) + lo;
}