1. ## Generating Random Numbers

I don't know alot about C++ but I know almost enough to make a slot machine game. The only thing I don't know is how to generate the random numbers. If you could give me some code or explain how that would be great.

2. I have done a bit of work on it, it hasn't got the random conponent and isn't fully functional but why doesn't it compile? Here it is
Code:
```#include <iostream>
using namespace std;

int main()
{
int return1=5;
int return2=5;
int return3=5;
int coins=10;
int input;

cout << "Welcome to Slot Machine" << endl;
cout << "You have 10 coins" << endl;
cout << "Please press 1 to play, or 2 to exit and then hit enter";
cin >> input;

if(input==1)
{
cout << "You have chosen to play. Hit enter to input a coin and play";cin.get();
cout << return1 << "   " << return2 << "   " << return3 << endl;
if(return1==return2&&return1==return3)
{
coins=coins+10;
cout << "You got the jackpot of 10 coins" << endl;
cout << "You now have " << coins << " coins.";cin.get();
cout << "Congratulations, you won.";
}
else if(return1!=return2||return1!=return3||return2!=return3)
{
cout << "You lose, I am sillyI am sillyI am sillyI am sillyI am silly";
}
else
{
cout << "You have chosen to exit, please play again." << endl;
}
return 0;
}```

3. That still means I need to learn how to make it random.

4. this is a example:

Code:
```#include<cstdio>
#include<ctime>

main()
{
srand((unsigned)time(0));
//use time() function as seed of rand() function
int randomnum=rand()%1000;
//define a int,and initialize it to a 'random number'
// %1000 means this number wouldn't beyond 1000

}```

5. The FAQ has a great tutorial on random numbers

essentially, what you will want to do.. is to use the rand() function.. which will return an unsigned random number ranging from zero to RAND_MAX (which is a very large number). Anywhoo.. use the modulus operator as an upper bound on the range of numbers you wish to use...

Code:
`slot_output1 = rand()%5    //will return a random integer between 0 and 4`
rand() may return a very large random number, but using the modulus operator will continously divide this larger number by 5 (in this example) until 5 is no longer able to fit into the randomly generated number.. and will return the remainder (if any)... and the remainder cannot be larger than 5

also, don't forget to seed your random number generator.. as per the faq.

edit: Hermitsky wins! (getting slow at me old age)

6. Thankyou, but that still doesn't answer why it wont compile.

7. Here's the edit with random, still bad but should work. Why won't compile?
Code:
```#include <iostream>
using namespace std;

int main()
{
srand((unsigned)time(0));
int return1=rand()%10;
int return2=rand()%10;
int return3=rand()%10;
int coins=10;
int input;

cout << "Welcome to Slot Machine" << endl;
cout << "You have 10 coins" << endl;
cout << "Please press 1 to play, or 2 to exit and then hit enter";
cin >> input;

if(input==1)
{
cout << "You have chosen to play. Hit enter to input a coin and play";cin.get();
cout << return1 << "   " << return2 << "   " << return3 << endl;
if(return1==return2&&return1==return3)
{
coins=coins+10;
cout << "You got the jackpot of 10 coins" << endl;
cout << "You now have " << coins << " coins.";cin.get();
cout << "Congratulations, you won.";
}
else if(return1!=return2||return1!=return3||return2!=return3)
{
cout << "You lose";cin.get();
}
else
{
cout << "You have chosen to exit, please play again." << endl;
}
return 0;
}```
the debugger says that there is a syntax error at line 39, a line that doesn't exist in my compiler, devc++

8. it works now! try it if you care, its still bad but if you like it that much im sure you can fix it up, ill post the perfected version when it is finished.
Code:
```#include <iostream>
using namespace std;

int main()
{
srand((unsigned)time(0));
int return1=rand()%10;
int return2=rand()%10;
int return3=rand()%10;
int coins=10;
int input;

cout << "Welcome to Slot Machine" << endl;
cout << "You have 10 coins" << endl;
cout << "Please press 1 to play, or 2 to exit and then hit enter";
cin >> input;

if(input==1)
{
cout << "You have chosen to play. Hit enter to input a coin and play"<<endl;cin.get();
cout << return1 << "   " << return2 << "   " << return3 << endl;cin.get();
}
if(return1==return2&&return1==return3)
{
coins=coins+10;
cout << "You got the jackpot of 10 coins" << endl;
cout << "You now have " << coins << " coins.";cin.get();
cout << "Congratulations, you won.";cin.get();
}
else if(return1!=return2||return1!=return3||return2!=return3)
{
cout << "You lose, I am sillyI am sillyI am sillyI am sillyI am silly";cin.get();
}
else
{
cout << "You have chosen to exit, please play again." << endl;
}
return 0;
}```

9. absolutly, you missed something,
this works fine:
Code:
```#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
srand((unsigned)time(0));
int return1=rand()%10;
int return2=rand()%10;
int return3=rand()%10;
int coins=10;
int input;

cout<<"Welcome to Slot Machine"<<endl;
cout<<"You have 10 coins"<<endl;
cout<<"Please press 1 to play, or 2 to exit and then hit enter"<<endl;
cin>>input;

if(input==1)
{
cout <<"You have chosen to play. Hit enter to input a coin and play"<<endl;
cin.get();
cout <<return1<<"   "<<return2<<"   "<<return3<<endl;
if(return1==return2&&return1==return3)
{
coins=coins+10;
cout<<"You got the jackpot of 10 coins"<<endl;
cout<<"You now have "<<coins<<" coins."<<endl;
cin.get();
cout<<"Congratulations, you won."<<endl;;
}
else if(return1!=return2||return1!=return3||return2!=return3)
{
cout <<"You lose, I am sillyI am sillyI am sillyI am sillyI am silly"<<endl;
}
else
{
cout<<"You have chosen to exit, please play again."<<endl;
}
return 0;
}
}```

10. why include those two libraries? it works without them

11. time() is defined in ctime,rand() is defined in cstdlib

12. Just because it works without them on one compiler (which only means that <iostream> eventually includes them), doesn't mean it will still work on another.

The third check here is redundant:
Code:
`if(return1!=return2||return1!=return3||return2!=re  turn3)`
If return1 == return2 and return1 == return3 (which is the precondition for the third part to be even evaluated), then return2 == return3 and the third inequality is always false.

13. i see your point, but it doesn't matter really. Also, once the random numbers values are declared, they dont change. I put in a loop so the prog is half good but the same three numbers pop up everytime. I know why, but i dont know how to make it change everytime. heres the code and tell me where to fix it.
Code:
```#include <iostream>
using namespace std;

int main()
{
srand((unsigned)time(0));
int return1=rand()%10;
int return2=rand()%10;
int return3=rand()%10;
int coins=10;
int input;

cout << "Welcome to Slot Machine" << endl;
cout << "You have 10 coins" << endl;
cout << "Please press 1 to play, or 2 to exit and then hit enter "<<endl;
cin >> input;

do{
if(input==1)
{
cout << "You have chosen to play. Hit enter to input a coin and play"<<endl;cin.ignore();cin.get();
cout << return1 << "   " << return2 << "   " << return3 << endl;cin.get();
}
if(return1==return2&&return1==return3)
{
coins=coins+10;
cout << "You got the jackpot of 10 coins" << endl;
cout << "You now have " << coins << " coins.";cin.get();
cout << "Congratulations, you won.";cin.get();
}
else if(return1!=return2&&return1!=return3&&return2!=return3)
{
coins=coins-1;
cout << "You lose. You now have " << coins << " coins.";cin.get();
}
else if(return1==return2||return1==return3||return2==return3)
{
coins=coins+3;
cout << "You win 3 coins. You know have " << coins << " coins.";cin.get();
srand((unsigned)time(0));
}
else
{
cout << "You have chosen to exit, please play again." << endl;cin.get();
}
}while(coins!=100||coins>=1);
cout << "You have either won the game by reaching 100 coins or lost all your money.";cin.get();
return 0;
}```

14. i fix it up even more, but still many problems. The obvious one that the three numbers never change exists, but i can fix it. Its just that i cant get the loop to work correctly eg if you hit more than 100 coins or get less than 1 coin it says the same message cause i dont know how to get the loop to functions so you get a different response if you lose and if you win. here it is
Code:
```#include <iostream>
using namespace std;

int main()
{
srand((unsigned)time(0));
int return1=rand()%10;
int return2=rand()%10;
int return3=rand()%10;
int coins=10;
int input;

cout << "Welcome to Slot Machine" << endl;
cout << "You have 10 coins" << endl;
cout << "Please press 1 to play, or 2 to exit and then hit enter "<<endl;
cin >> input;

while(coins>0&&coins<100)
{
if(input==1)
{
cout << "You have chosen to play. Hit enter to input a coin and play"<<endl;cin.ignore();cin.get();
cout << return1 << "   " << return2 << "   " << return3 << endl;cin.get();
}
if(return1==return2&&return1==return3)
{
coins=coins+10;
cout << "You got the jackpot of 10 coins" << endl;
cout << "You now have " << coins << " coins.";cin.get();
cout << "Congratulations, you won.";cin.get();
}
else if(return1!=return2&&return1!=return3&&return2!=return3)
{
coins=coins-1;
cout << "You lose. You now have " << coins << " coins.";cin.get();
}
else if(return1==return2||return1==return3||return2==return3)
{
coins=coins+3;
cout << "You win 3 coins. You know have " << coins << " coins.";cin.get();
}
else if(coins==100)
{
cout << "Congratulations. You won.";cin.get();
}
else if(coins<1)
{
cout << "You are broke.";cin.get();
}
else
{
cout << "You have chosen to exit, please play again." << endl;cin.get();
}
}
cout << "You have either won the game by reaching 100 coins or lost all your money.";cin.get();
cin.get();return 0;
}```

15. Hi pizzapie,

Code:
```else if(coins==100)
{
cout << "Congratulations. You won.";cin.get();
}
else if(coins<1)
{
cout << "You are broke.";cin.get();
}```
Looks like this part of your if/elseif/else ladder would only ever execute if the previous three conditions were not met, which in this case would be never. Maybe if you take them out of the first if ladder, maybe even out of the while loop (since they're printed at the end of the game) then you could get them to display properly.

Might solve one of your problems.