-3

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, int & 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.

Jacob
  • 77,566
  • 24
  • 149
  • 228
Probs
  • 23
  • 5
  • 9
    Sorry, but we are no homework service. What have you tried, where do you have problems? Those are the kind of questions we're gonna answer. :) – Xeo May 10 '11 at 00:16
  • 3
    Lol, talk about lazy. Post your attempt and we'll critique it for you. You should also use a spell checker on your posts. – rboarman May 10 '11 at 00:20
  • I am not asking you to do my homework for me i was wondering is there any way to write this in a more simple way by following the instructions and still compile correctly. and if so how would you go about doing that. – Probs May 10 '11 at 00:23
  • @confused: It seems you really really need to get [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Xeo May 10 '11 at 00:29
  • @confused - Why the return type of the `getTwoRandomNumbers` is **float** ? And the return type of `main` should be **int** according to the C++ standard. – Mahesh May 10 '11 at 00:31
  • @Xeo- yea tht and a good teacher. my professor said at the beging of the year he was going to learn it with us and that he used to teach math... – Probs May 10 '11 at 00:32
  • @Xeo- our book says that if you use void main you dont have to use return;. i reurned GTRN b/c our teacher said to do that. – Probs May 10 '11 at 00:33
  • 1
    @confused - I don't understand why your teacher said to return **float** for `getTwoRandomNumbers`. I personally feel the book you read that says to use **void** as the return type for `main` is not very useful book to learn C++. Get a book from @Xeo suggested link. – Mahesh May 10 '11 at 00:41
  • also i dont know why this wont compile correctly. i got the jist of the code i just can seem to figure out what is wrong. – Probs May 10 '11 at 00:48

2 Answers2

2

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.
Maxpm
  • 24,113
  • 33
  • 111
  • 170
0

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
Gio Borje
  • 20,314
  • 7
  • 36
  • 50