Matlab Coin Toss Simulation

Posted by user1772959 on Stack Overflow See other posts from Stack Overflow or by user1772959
Published on 2012-12-14T17:02:06Z Indexed on 2012/12/14 17:03 UTC
Read the original article Hit count: 152

Filed under:
|

I have to write some code in Matlab that simulates tossing a coin 150 times. I have to count how many times the coin lands on heads and create a vector that gives a running percentage of the heads.

Then I have to make a table of the number of trials, random 'flips", and the running percentages of heads. I assume random "flips" means heads or tails for that trial.

I also have to create a line graph with trials on the x-axis and probabilities (percentages) on the y-axis. I'm assuming the percentages are just the percentage of getting heads.

Sorry if this post was long. I figure giving the details now will make it easier to see what I was trying to do with the code. I didn't create the table or plot yet because I'm not even sure how to code for the actual problem.

NUM_TRIALS = 150;
trials = 1:NUM_TRIALS;
heads = 0;
t = rand(NUM_TRIALS,1);

for i = trials

    if (t < 0.5)
        heads = heads + 1;
    end

    z = zeros(NUM_TRIALS,1);
    percent_h = heads/trials;  
end

© Stack Overflow or respective owner

Related posts about matlab

Related posts about percentage