CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Gambler's Ruin

  1. #1
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Question Gambler's Ruin

    *This is an assignment*
    *instructions for original assignment attached*

    Ok, I am trying to get the program to calculate the number of times one goes broke and the number of time one reaches the goal. I want the program to run a number of trails, originally it was going to be 1000, but I kept it short for testing.

    Can anyone explain what I am doing wrong I know it has to do with the mess of a loop that I have created. Also any advice?

    Thanks in advance.

    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
    	srand(time(NULL)); // Seed the RNG once, at the start of the program
    
        int n, m, flip, trial;
    	int broke = 0;
    	int goal = 0;
    
        cout << "Gamblers' Ruin" << endl;
        cout << "Please enter the amount of money you wish to bet." << endl;
        cin >> n;
        cout << "You entered $" << n << " dollars." << endl;
        cout << endl;
    
        cout << "Please enter the goal amount you wish to quit at." << endl;
        cin >> m;
        cout << "You entered: " << m << endl;
        cout << endl;
    
    	for (trial = 1; trial <= 100; trial++)
    	{
    		while(n == 0 || n != m)
    		{
    			if(rand()%2 == 0)
    			{
    				cout << "Heads" << endl;
    				n++;
    				cout << endl;
    			}
    			else
    			{
    				cout << "Tails" << endl;
    				n--;
    				cout << endl;
    			}
    				cout << n;
    				cout << endl;
    		}
    			if (n == 0) 
    			{
    				
    				broke++;
    			}
    			else if (n == m)
    			{
    			
    				goal++;
    			}
    	}
    		cout << broke;
    		cout << endl;
    		cout << goal;
    		cout << endl;
    
        system("pause");
        return 0;
    }
    Again Thanks.
    Attached Files Attached Files

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Gambler's Ruin

    What did you expect with this code and what did you get in reality?
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2011
    Location
    Tennessee
    Posts
    46

    Re: Gambler's Ruin

    I am wanting the program here to run 100 trials and for every trial display the number of times one will go broke or reach there goal depending on the initial amount that is bet.

    So for one trail say you bet $5 dollars. Then the coin is flipped and depending on heads $1 will be added, or tails $1 deduction. This will run until the user either loses all their money, or reaches the goal. So if the user does lose all their money then increment broke, or if they reach their goal increment goal.

    This will run 100 times so the output should finally be something like this:
    Goal: 43
    Broke: 57
    a total of 100 trails.

    I am actually getting the trial to run 100 times and that is it. Goal and Broke are not being incremented like they are supposed to be, and this is wrong I am having trouble trying to get the loops to work in properly.
    I think that the design of the loop is crappy.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Gambler's Ruin

    Quote Originally Posted by Wh1t3gh0st View Post
    I am wanting the program here to run 100 trials and for every trial display the number of times one will go broke or reach there goal depending on the initial amount that is bet.
    Say n = 5 and m = 10. I get tails 5 straight times, now n == 0.

    Your while loop doesn't stop if n == 0, since n != m. Now say I get tails again, n is now -1, so gain gets increased. So in other words, you can be broke (more than broke, you owe money) and gain at the same time!

    Once n gets to 0, that while loop should be stopped, since you have nothing left.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 18th, 2012 at 12:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured