Using MATLAB to work out coin-flip style probabilities

In summary, the conversation is about a task that involves using random number features and loop features in MATLAB to simulate a gambling game. The goal is to reach £100 without going below £0. The person is struggling with the probability aspect of the problem and is seeking help. They come up with a solution involving running 1000 "sessions" of the game and recording the outcomes to calculate the probability. However, there are a few mistakes in their code that need to be fixed.
  • #1
Flucky
95
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,076
  • GamblerMan2.jpg
    GamblerMan2.jpg
    39.8 KB · Views: 662
  • GamblerMan3.jpg
    GamblerMan3.jpg
    43.4 KB · Views: 1,153
Last edited:
Physics news on Phys.org
  • #2
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)
 
  • #3
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: 862
Last edited:
  • #4
Bump :(
 
  • #5
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.
 

1. How do I create a coin-flipping simulation in MATLAB?

To create a coin-flipping simulation in MATLAB, you can use the rand function to generate random numbers between 0 and 1. If the number is greater than 0.5, it represents heads, and if it is less than or equal to 0.5, it represents tails.

2. How can I calculate the probability of getting a certain number of heads in a series of coin flips?

To calculate the probability of getting a certain number of heads in a series of coin flips, you can use the binopdf function. This function takes three inputs: the number of trials, the probability of success (in this case, 0.5 for a fair coin), and the number of successes. It will return the probability of getting that number of successes in the given number of trials.

3. Can I visualize the results of my coin-flipping simulation in MATLAB?

Yes, you can use the histogram function to plot a histogram of the results from your coin-flipping simulation. This will show the distribution of heads and tails and can help you visualize the probabilities.

4. How can I change the probability of getting heads in my simulation?

You can change the probability of getting heads in your simulation by adjusting the threshold in the rand function. For example, if you want a coin that is more likely to land on heads, you could use a threshold of 0.7 instead of 0.5.

5. Is there a limit to the number of coin flips I can simulate in MATLAB?

No, there is no limit to the number of coin flips you can simulate in MATLAB. However, keep in mind that as the number of trials increases, the simulation may take longer to run and require more memory and processing power.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
569
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
339
  • Precalculus Mathematics Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top