Using MATLAB to work out coin-flip style probabilities

Click For Summary

Discussion Overview

The discussion revolves around using MATLAB to simulate a coin-flip style gambling scenario, where a gambler either wins or loses money based on random outcomes. Participants explore how to implement the simulation, measure probabilities, and refine the code to achieve the desired functionality.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes their initial approach using random numbers to simulate the gambling process, noting difficulties in measuring probability and running multiple simulations.
  • Another participant suggests that the algorithm should record the outcomes of multiple experiments to predict future probabilities based on past results.
  • There are discussions about the structure of the loops in the MATLAB code, with suggestions to modify the conditions for exiting the loop and resetting values.
  • Participants express uncertainty about how to properly implement the logic for running multiple sessions and counting wins and losses.
  • One participant points out specific mistakes in the code, such as incorrect loop syntax and placement of condition checks, while encouraging the original poster to think through potential fixes.

Areas of Agreement / Disagreement

Participants generally agree on the need to simulate multiple sessions and track outcomes, but there is no consensus on the best way to structure the code or the exact approach to calculating probabilities.

Contextual Notes

Participants mention issues with variable initialization and loop conditions, indicating potential limitations in the current implementation that may affect the simulation's accuracy.

Who May Find This Useful

Individuals interested in programming simulations, probability theory, or those seeking assistance with MATLAB coding challenges may find this discussion beneficial.

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,152
  • GamblerMan2.jpg
    GamblerMan2.jpg
    39.8 KB · Views: 751
  • GamblerMan3.jpg
    GamblerMan3.jpg
    43.4 KB · Views: 1,228
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: 948
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
7K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K