开发者

Random number homework [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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 by rand. You can use this to manipulate your output so it falls within the proper range.


Step By Step

  1. Write the function
  2. Write the parameters as pass by reference
  3. Assign the parameters a value from a random number generating algorithm
  4. Validate that the numbers are not the same
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜