Gambler's Ruin in Matlab: Solving the Loop Problem

Click For Summary
SUMMARY

The discussion focuses on implementing a Gambler's Ruin simulation in Matlab, where a gambler starts with 5 dollars and bets 1 dollar with a 70/30 win/loss probability. The user struggles to end the loop when the gambler's balance reaches zero, resulting in negative values. A while loop is suggested as a more effective solution for this scenario, allowing the simulation to terminate correctly when the gambler runs out of funds.

PREREQUISITES
  • Understanding of Matlab programming syntax
  • Familiarity with random number generation in Matlab
  • Knowledge of control flow structures, specifically loops
  • Basic concepts of probability and gambling simulations
NEXT STEPS
  • Implement a while loop in Matlab to handle the gambler's balance correctly
  • Explore Matlab's rand function for generating random outcomes
  • Learn about error handling in Matlab to manage unexpected values
  • Research additional gambling simulations to expand understanding of probability
USEFUL FOR

Matlab programmers, students studying probability, and anyone interested in developing simulations related to gambling scenarios.

JohnPrior3
Messages
17
Reaction score
5
For Matlab, I need to write a program where a Gambler starts with 5 dollars and runs out with 1 dollar bets. The house has favorable odds of 70/30. I have gotten very far, but I can't find out how to end the loop when the gambler reaches 0. Here is my script:

Code:
A = 5

n=100;

x=rand(1,n);

    for i=1:n-1

        if x(i)>0.3

          A=A-1

        else

          A=A+1

        end
end

My script runs the 100 times and leaves me with a negative number. Everything works for the script, except ending at 0. I tried doing a while loop but it didn't work.
 
Last edited by a moderator:
Physics news on Phys.org
The while loop is a better approach. Can you show that?

You can use [code] tags to make it easier to show code:

Code:
for i=1:n-1
  if x(i)>0.3
    A=A-1
 
JohnPrior3 said:
For Matlab, I need to write a program where a Gambler starts with 5 dollars and runs out with 1 dollar bets. The house has favorable odds of 70/30. I have gotten very far, but I can't find out how to end the loop when the gambler reaches 0. Here is my script:

Code:
A = 5

n=100;

x=rand(1,n);

    for i=1:n-1

        if x(i)>0.3

          A=A-1

        else

          A=A+1

        end
end

My script runs the 100 times and leaves me with a negative number. Everything works for the script, except ending at 0. I tried doing a while loop but it didn't work.

So what you're saying is IF the gambler has no money, then he can't gamble??
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K