# A simple game using RNG

• 03-12-2005
amirahasanen1
A simple game using RNG
Hi there,
I am designing this simple game to generate two random numbers using RNG and get their sum, if it is 7 or 11 the player wins and the budget of the game is incremented by the bet the player has input at the beginning of the game, if the sum is 2 or 3 or 12 the player loses and the budget is decremented by the bet the player has made, if the sum is otherwise other 2 random numbers are generated and their sum is got using the same bet the user has made at the beginning without re-asking the user for the bet..
Now, the game is working but the problem is that when the sum is otherwise and it repeats the RNG process it keeps getting the same results for so many times and the 2 randomly generated numbers change after a long time.. what is wrong with that?
All thanks,

-Amy

Code:

```// Including librarie(s) needed for running the program #include <iostream> #include <ctime> using namespace std; // declaring a prototype functions int rng(); int winorlose(); int budgetcalculator(char, float, float); // starting the main function int main() {         float budget = 10000;         float bet;         char winlose;         cin>>bet;         do         {                 winlose = winorlose();         }while(winlose=='n');         budget = budgetcalculator(winlose, budget, bet);         cout<<budget<<endl;         return 0; } // end of the main function int rng() {         // declaring local variables         const int n = 6; // the number of faces of the dice         int x1, x2, sum; // the 2 number generated by the RNG (x1 & x2), and their sum                 srand ( (unsigned) time (NULL) ); //Initialize RNG                 x1 = rand( ) % n + 1;        // Generate a number from the sequence         cout<<"The 2 generated numbers are: "<<x1<<" ";                        // Print it                                 x2 = rand( ) % n + 1;        // Generate another number from the sequence         cout<<"& "<<x2<<endl;                        // Print it         sum = x1 + x2; // summing the two randomly generated numbers         cout<<"Thir sum is: "<<sum<<endl;         return sum; } //end of function int winorlose() {         int dicesum;         dicesum = rng();                 // if statment for checking if the sum of the 2 randomly generated numbers is 7 or 11         if((dicesum==7) || (dicesum==11))                 return 'w';         else if((dicesum==2) || (dicesum==3) || (dicesum==12))                 return 'l';         else                 return 'n';         // end of if statment } int budgetcalculator(char state, float credit, float moneybet) {         if(moneybet<=credit)                 if(state=='w')                         credit += moneybet;                 else if(state=='l')                         credit -= moneybet;                         return credit; }```
• 03-12-2005
Salem
> srand ( (unsigned) time (NULL) ); //Initialize RNG
Put this at the start of main()
You only really need to do this ONCE per run of the program.
• 03-12-2005
Brain Cell
Quote:

Now, the game is working but the problem is that when the sum is otherwise and it repeats the RNG process it keeps getting the same results for so many times and the 2 randomly generated numbers change after a long time.. what is wrong with that?
The problem is that your program isn't genarting a different random number in every single loop which causes it to test two numbers multiple times before generating another two different random numbers. This happens because you're seeding the random number generator in each loop. The random number generator should be seeded ONLY ONCE.

take this line off the rng() function :
Code:

`srand ( (unsigned) time (NULL) );`
place it before your do-while loop and see if your program starts working the way you wanted it.

btw , your winorlose() function always returns a 'char' ; why did you declare it as an 'int'?

hope this helps :)

*EDIT* bah , nevermind. Salem was faster (as usual) :p
• 03-12-2005
amirahasanen1
Many thanks guys :)
• 03-12-2005
amirahasanen1
In the game I wanted to like put the win's & lose's in a report file so I used file streams, but there are some errors which I don't know the reason of..
All the error comes in this part when I put it outside the main (i.e. gloabal):
Code:

```ofstream target; string fname = "report.txt"; target.open(fname.c_str());```
But when I put this code inside the main I get no errors from the compiler.. but the problem is that when I put it inside the main function everytime I call the main function the file is re-opened so all the old data inside it are erased.. what can I do..
Thanks

-Amy

The whole program code:
Code:

```// Including librarie(s) needed for running the program #include <iostream> #include <ctime> #include <fstream> #include <string> using namespace std; // declaring a prototype functions int rng(); char winorlose(); int budgetcalculator(ofstream, char, float, float); // Start report file preparation ofstream target; string fname = "report.txt"; target.open(fname.c_str()); // End report file preparation // declaring a global variable float budget = 10000; // starting the main function int main() {         // declaring local variables         float bet;         char winlose;         cout<<"Your budget is: \$"<<budget<<endl;         cout<<"Please input the amount of money you want to bet: ";         cin>>bet;         srand ( (unsigned) time (NULL) ); //Initialize RNG         do         {                 winlose = winorlose();         }while(winlose=='n');         budget = budgetcalculator(target, winlose, budget, bet);         cout<<"Your budget now is: "<<budget<<endl<<endl;         // Start play again option         char playagain;         cout<<"Do you want to play again? [press (y) for yes, and (n) for no]: ";         toupper(playagain);         cin>>playagain;         if(playagain=='y')                 main();         // End play again option                 // closing the output stream                 // Start showing game report                string line;         string filename = "report.txt";         ifstream source;         source.open(filename.c_str());         cout<<endl<<"This is a report for money won and lost from the orignial budget: "<<endl;         cout<<"Original budget: \$10000"<<endl;         cout<<"Final budget: \$"<<budget<<endl;         cout<<"---------------------"<<endl;         while(!source.eof())         {                 getline(source,line);                 cout<<line<<endl;         }         // End showing game report         target.close();         return 0; } // end of the main function int rng() {         // declaring local variables         const int n = 6; // the number of faces of the dice         int x1, x2, sum; // the 2 number generated by the RNG (x1 & x2), and their sum                 x1 = rand( ) % n + 1;        // Generate a number from the sequence         cout<<"The 2 dice threw have the numbers: "<<x1<<" ";                        // Print it                                 x2 = rand( ) % n + 1;        // Generate another number from the sequence         cout<<"& "<<x2<<endl;                        // Print it         sum = x1 + x2; // summing the two randomly generated numbers         cout<<"Thir sum is: "<<sum<<endl;         return sum; } //end of function char winorlose() {         int dicesum;         dicesum = rng();                 // if statment for checking if the sum of the 2 randomly generated numbers is 7 or 11         if((dicesum==7) || (dicesum==11))                 return 'w';         else if((dicesum==2) || (dicesum==3) || (dicesum==12))                 return 'l';         else                 return 'n';         // end of if statment } int budgetcalculator(ofstream destination, char state, float credit, float moneybet) {         if(moneybet<=credit)         {                 if(state=='w')                 {                         credit += moneybet;                         destination<<"\$"<<moneybet<<" won."<<"\n";                 }                 else if(state=='l')                 {                         credit -= moneybet;                         destination<<"\$"<<moneybet<<" lost."<<"\n";                 }         }         else         {                 cout<<"Bet is larger than your current budget."<<endl;                 main();         }                 return credit; }```
• 03-12-2005
Brain Cell
Quote:

In the game I wanted to like put the win's & lose's in a report file so I used file streams, but there are some errors which I don't know the reason of..
All the error comes in this part when I put it outside the main (i.e. gloabal):
you can't make a procedure global (target.open(fname.c_str());) . It must be within a function ,regardless whether its inside main or another function. If you want to use the same ofstream and string objects in multiple functions then make them global , open the file within main (or any other function) then output data to it. Another way would be sending the string and ofstream object by reference\address so you can use them.

Quote:

everytime I call the main function the file is re-opened so all the old data inside it are erased
The problem is with your "open" mode , not main(). You only sent the file name to "open" member function , the other argument (mode) didn't recieve anything so its assumed ios::out (default mode).

ios::out writes to the file you specified discarding the old contents , and if the file isn't found , it creates a new file with that name. You just need to use the "app" mode (append) so the new contents are appended to the end of the file. You need to specify the opening mode in "open" like this:
Code:

`target.open("output.txt", ios::app);`
this will append to the file instead.

hope this helps
• 03-12-2005
amirahasanen1
I did not need this command (which outputs data to files without erasing what inside it) in this program, but I strongly needed it in another program of mine, so really thank you so much Brain Cell :)