0

Possible Duplicate:
C++ Random number homework question

Guys, I need help (yes, last minute homework help). I'm suppose to write a function named getTwoRandomNumbers that uses two parameters to return two different random numbers. The function is suppose to accept two parameters that specify the minimum and maximum values of the random number.

I need to write data validation code that ensures that two identical random numbers will never be returned. This is what I have so far: (ANY DIRECTION IS GREATLY APPRECIATED)

Ok, I made changes and now have this:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <time.h>
using namespace std;

float getTwoRandomNumbers (int Min, int Max, int & number1, int & number2);

void main()
{
    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;
   }
}
Community
  • 1
  • 1
Cyn
  • 3
  • 2

2 Answers2

2

First off, you're on the right path with passing back both values through the parameters of the function. Then it seems like you're also trying to get the 2nd value back through the function return.

Since you need to get more than 1 value from the function, ignore the return value and just use the 2 parameters passed to get your return values.

Also, try thinking out the question in your head, what are you trying to do? Get 2 random values while number1 is not equal to number2

That should give you a decent start on the logic flow for getting the random numbers to pass back.

johnhaley81
  • 1,153
  • 10
  • 14
1

One thing for you: As I reformatted your code, it became apparent that you have a return within your for loop. That means getTwoRandomNumbers will always return after the first iteration. I'm 99.99% positive this is not what you have in mind.

joce
  • 9,624
  • 19
  • 56
  • 74