Gambler's Ruin in Matlab: Solving the Loop Problem

AI Thread Summary
The discussion centers on creating a Matlab program simulating a gambler starting with 5 dollars and betting 1 dollar with a 70/30 house edge. The user struggles to properly end the loop when the gambler's funds reach zero, resulting in negative values. They initially used a for loop but are advised that a while loop would be more suitable for this scenario. The conversation highlights the need to check the gambler's balance before each bet to prevent further losses. The key takeaway is the importance of implementing a condition to exit the loop when the gambler's funds are depleted.
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

Back
Top