### Thread: Random number generator problem(noob)

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2013
Posts
1
Rep Power
0

#### Random number generator problem(noob)

Code:
```/*
Sean M. Dane
November 14,2013
COP 2000 assignment #4
*/

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;
void intRand (int *);

int main ()
{

float guess, ANS;
int r[2];
int choice = 0;
QUIT_CHOICE = 4;

srand((unsigned int)time((time_t *)NULL));//need for rand()

while (choice < ADD_CHOICE || choice > QUIT_CHOICE)
{
cin >> choice;
}

if (choice == QUIT_CHOICE)
return 0;

intRand(r);

switch (choice)
{
case 1:
ANS = (r[0] + r[1]);
cout << r[0] + r[1] << "= ? ";
cin>> guess;
if (guess == ANS)
cout << "Great job!";
else
cout << ANS << '\n';
break;

case 2:
ANS = (r[0] - r[1]);
cout << (r[0] - r[1]) << "= ? ";
cin>> guess;
if (ANS == guess)
cout << "Great job!";
else
cout << ANS << '\n';
break;

case 3:
ANS = (r[0] * r[1]);
cout << r[0] * r[1] << "= ? ";
cin>> guess;
if (ANS == guess)
cout << "Great job!";
else
cout << ANS << '\n';
break;
}

return 0;
}

{
cout << "\n\t\tMENU OF OPERATIONS \n\n"
<< "\n 2. Subtraction.\n"
<< "\n 3. Multiplication.\n"
<< "\n 4. Quit the Program.\n\n";
}

void intRand(int *r)
{
r[0] = rand() % 11 + 2;
r[1] = rand() % 11 + 2;
}```
it seems to be generating 1 number and not 2. when I click 1 for addition it says

2. You could try to output r[0] and r[1] in the intRand function.

But my guess is that it is calculated when you try to print/cout it out.
Take a look at how you get the ANS value, and then look how you want to print it out. (same syntax used)

To print each out, try something like this (untested):
Code:
`cout << r[0] <<  " + " << r[1] << "= ? ";`
3. (guess == ANS)

This is binary floating-point that we're talking about, so the representation of decimal values is only approximate. It may look like they'd have to be equal, but they could instead differ by less than a millionth -- remember, they have to be exactly the same to count as being equal.

Why use float? The numbers you are getting in intRand are integers. Why not work with integers instead? Integers won't give the approximation problems that floating-point will.
4. BTW, compiling your code as-is and running, I got this:
Code:
```C:TEST>a