Gambler's Ruin in Matlab: Solving the Loop Problem

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
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:
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??