I missed the whole week that this was covered. And I cannot find any help from the course book. All help is greatly appreciated.
Write a function named generateRandom that generates random numbers in a range specified by the user. The function takes two integer parameters representing the lower and upper bounds of the range and it returns an integer random number between the lower and upper bound inclusive. For example, if the parameters are 5 and 25, the function will return a random number between 5 and 25. Write a main function that tests this function by:
1) Asking the user for an integer with which to seed the randon number generator. Call srand with this integer.
2) Ask the user for a lower and upper bound for the random number.
3) Call generateRandom and print the generated random number.
I know that I have to include
#include <cstdlib>
#include <ctime>
It's just where to go from there. Thanks again in advance for all the help.
1) Get an int from the user, say x. Use it to seed the generator. srand(time(x)); for example.
2) Get a lower bound from the user, and an upper bound.
3) Call generateRandom and just cout the random integer, like oonej showed you.
Hm.. Ok. That makes sense. As of right now, my question is where do I insert the "srand (time(x));"? Does it go after the headers? Or before the "int somefunction (lowerboud, .......){?
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int somefunction(int lowerbound, int upperbound) // Functions are declared outside the main function.
// Also, define the type of variable being passed.
{
return lowerbound + rand() % upperbound;
}
int main ()
{
int x, LowerBound, UpperBound, SomeRandomNumber;
cout << "Please insert an integer: ";
cin >> x;
srand (x); // Take the time() out of the random seed generator.
// Somewhere down here, find out what the lower bound is, and the upper bound, and then
// call the function somefunction
}