I am trying to write a function for a random number in a range decided by the user. I call srand() in int main() with the function RandomNumber defined above.
I know if do the randomnumber equation in main(), while also calling srand() in main(), it works how I want.
This is the program that does not do what I want. The return value is a combination of numbers and letters. I need return of a number in a range determined by the user input. Essentially, I need to create a random number generating function outside the int main() function, then be able to call the RandomNumber inside int main(). I run into trouble with calling srand(time(NULL));. I have to call srand() inside main() or the random number is not generated correctly.
#include <iostream>
#include <ctime>
using namespace std;
int RandomNumber(int userNum1, int userNum2) {
return (rand() % (userNum2 - userNum1) + userNum1);
}
int main()
{
srand(time(NULL));
int userNum1;
int userNum2;
cin >> userNum1;
cin >> userNum2;
cout << RandomNumber << endl;
return 0;
}
Even if I call srand() inside the RandomNumber function, it does not work as I want. /////////////////////////////////////////
This is a program that does what I want, but I am trying to define the function RandomNumber above int main().
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int RandomNumber;
int userNum1;
int userNum2;
cin >> userNum1;
cin >> userNum2;
RandomNumber = rand() % (userNum2 - userNum1) + userNum1;
cout << RandomNumber << endl;
return 0;
}
///////////////////////////////////////////// How can I create my RandomNumber function outside main() and call it insdide main()?