I'm trying to write a program that uses a function to generate 10 random numbers within a range provided by the user. It seems to work okay, other than the fact that the numbers returned are all 1's:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rand_int(int min, int max);
int main()
{
int min, max;
cout << "Hello user.\n\n"
<< "This program will generate a list of 10 random numbers within a
given range.\n"
<< "Please enter a number for the low end of the range: ";
cin >> min;
cout << "You entered " << min << ". \n"
<< "Now please enter a number for the high end of the range: ";
cin >> max;
while(min > max){
cout << "Error: Your low number is higher than your high number.\n"
<< "Please reenter your high number, or press ctrl + c
to end program.\n";
cin >> max;
cout << endl;
}
for(int i = 0; i < 10; i++){
int rand_int(int min, int max);
cout << rand_int << endl;
}
return 0;
}
int rand_int(int min, int max)
{
srand(time(0)); // Ensures rand will generate different numbers at different times
int range = max - min;
int num = rand() % (range + min);
return num;
}