Here is the question:
Write a function named getTwoRandomNumbers that uses two parameters to return two different random numbers. The function also accepts two parameters that specify the minimum and maximum values of the random number. You will need to write data validation code that ensures that two identical random numbers will never be returned.
Here is the code I got so far:
float getTwoRandomNumbers (int Min, int Max, in开发者_JS百科t & number1, int & number2);
void main()
{
getTwoRandomNumbers (int Min, int Max, int & number1, int & number2)
cout << "The two random numbers are " << getTwoRandomNumbers << endl;
}
float getTwoRandomNumbers (int Min, int Max, int & number1, int & number2)
{
int loopNumber, number;
for (loopNumber = 0; loopNumber <= 200 ; loopNumber ++)
{
number = rand();
if (loopNumber < 100 && number >= Min && number <= Max)
{
number1 = number;
}
if (loopNumber > 100 && number >= Min && number <= Max)
{
number2 = number;
}
return number2;
}
}
I am trying to write this as simple as possible if anyone can do that would be wonderful.
I'm not going to do your homework for you, but I'll give you a few starting points.
srand()
, to initialize the random number generator. This is usually done with the program's execution time or something similar.rand()
, to get one random number after the generator has been initialized.RAND_MAX
, which is the maximum number that can be returned byrand
. You can use this to manipulate your output so it falls within the proper range.
Step By Step
- Write the function
- Write the parameters as pass by reference
- Assign the parameters a value from a random number generating algorithm
- Validate that the numbers are not the same
精彩评论