Using MATLAB to work out coin-flip style probabilities

Click For Summary
The discussion revolves around using MATLAB to simulate a coin-flip style gambling scenario, where a player either gains or loses money based on random outcomes. The user initially struggles with the correct implementation of loops and conditions to track the player's balance, starting from £1 and aiming for £100. Suggestions include using a loop to run multiple experiments and recording wins and losses to calculate probabilities. The conversation highlights the importance of resetting the player's balance after each session and accurately counting the number of wins and losses. Overall, the focus is on refining the MATLAB script to achieve the desired simulation and probability calculations.
Flucky
Messages
93
Reaction score
1
I've basically been set the task below however I've not really been taught how to go about it. I think I know the general theory of how to go about it but the actual commands and putting it all together bamboozles me. The question is attatched.

attachment.php?attachmentid=56634&stc=1&d=1363105194.jpg

Just seen the typo, it should be 'loses' not 'looses'. Rookie mistake!

My thinking is that you use the random number feature to get a value between 0 and 1. If it comes out greater than 0.5 it adds a pound (£) to the pot and if it comes out less than 0.5 it removes a £ from the pot (starting off with only £1). You then use the loop feature to continue doing this until/if he reaches £100. However I've not the slightest clue how to measure the probability or even run multiple loops to get an average probability.

The furthest I've got with this problem is:

a=rand % where a is the 50/50 flip
x=1 % where x is the current total amount of money he has


if a<0.5
x=x+1

if a>0.5
x=x-1
end
end


but even when I run that simple script, if the random number comes out less than 0.5 it doesn't remove a £ it just remains at 1.

Any insight on how to solve this would be greatly appreciated as my MATLAB knowledge is poor.

-----------EDIT-------------------------------------------------------------------------------------------

I think I'm getting closer, does this look promising?

attachment.php?attachmentid=56639&stc=1&d=1363110168.jpg


Here it stops the script if he runs out of money and also stops when he reaches his target of £100. Now I've hit a wall and don't have a clue how to continue with the question, I haven't even addressed the probability side of it.
 

Attachments

  • GamblerMan.jpg
    GamblerMan.jpg
    30.8 KB · Views: 1,136
  • GamblerMan2.jpg
    GamblerMan2.jpg
    39.8 KB · Views: 737
  • GamblerMan3.jpg
    GamblerMan3.jpg
    43.4 KB · Views: 1,217
Last edited:
Physics news on Phys.org
hmm. Well really it resets n and x when he hits zero pounds. It doesn't exit the loop. But, that's not necessarily a bad thing. You can make the algorithm this way, you do need to add some more things to it though.

For the probability side of the question: think, for each 'experiment', the gambler either goes bust ('loses'), or reaches his target ('wins'). And you observe a number of these 'experiments'. Now, if you recorded the outcome of each experiment, and how many experiments happened in total, then what would you 'predict' is the probability of the next experiment being a 'success' or 'loss' ? (The answer is intuitive).

hint: think of the extreme case. If he won 99 times out of 100, you'd assume the next experiment is very likely to be a win, right? So exactly what probability should we predict? And then how does this generalise for any given number of wins (w) and total experiments (T)
 
brucew said:
hmm. Well really it resets n and x when he hits zero pounds. It doesn't exit the loop. But, that's not necessarily a bad thing. You can make the algorithm this way, you do need to add some more things to it though.

For the probability side of the question: Think, for each 'experiment', the gambler either goes bust ('loses'), or reaches his target ('wins'). And you observe a number of these 'experiments'. Now, if you recorded the outcome of each experiment, and how many experiments happened in total, then what would you 'predict' is the probability of the next experiment being a 'success' or 'loss' ? (the answer is intuitive).

Hint: Think of the extreme case. If he won 99 times out of 100, you'd assume the next experiment is very likely to be a win, right? So exactly what probability should we predict? And then how does this generalise for any given number of wins (w) and total experiments (t)

EDIT

Hi Bruce, realized what you meant earlier today so decided to change my script a little. For some reason when I used 'while 0<x<100' it would still give me negative x values and so I ditched that effort and used two 'for' loops. I'm planning on running 1000 "sessions" ie run the experiment 1000 times recording whether he wins or loses. From that it will give me however many wins vs however many losses and like you said, from that I can work out the probability. I think the final hurdle for this is getting the whole running 1000 loops thing. I'm not sure what to put in, in order to get a total amount of wins/losses out of 1000.

I've tried adding 's=s+1' (where s stands for session) in various places within the loops but can't get it to work. What is needed in the following script?

Apologies for the lousy wording.

attachment.php?attachmentid=56695&stc=1&d=1363201139.jpg
 

Attachments

  • the100pundman.jpg
    the100pundman.jpg
    29.6 KB · Views: 939
Last edited:
Bump :(
 
Nice work. It is pretty much correct except for a few mistakes. For the 's loop', you have put "for s=1000" but I think you meant to type "for s=1:1000". Also, when you check for x==100 and x==0, they are both inside the else part. This means your program will only do the check when a>0.5.

Another thing is that in your program, if x gets to 100 (or zero), then the next session will start with that value, which is not what you want the program to do. I'll leave it to you to think of fixes for these problems. p.s. I always make mistakes the first time I write code. The other day I was puzzled for ages about why my program was doing something strange, then I realized that I had just declared one of my variables as the wrong type.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 29 ·
Replies
29
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K