Help with coin toss loop

40 ビュー (過去 30 日間)
Kathryn Janiuk
Kathryn Janiuk 2020 年 11 月 7 日
コメント済み: senthil 約11時間 前
Hi, so I am very new at this! I am writing a code that is trying to simulate a fair coin toss that flips a coin 100x with -1 being tails and 1 being heads. I am then trying to run this 100 times and find the mean of those 100 samples and put it into a histogram.
Here is what I have written currently:
samplesize = 50;
nsamples=100;
coins = (binornd(1,.5,nsamples,1)*2)-1;
count_toss = mean(coins);
for i = 1:100
mean(count_toss)
end
lots_toss = mean(count_toss);
Bins = 10;
histfit(lots_toss,10);
When I run this I get:
Error using histfit (line 94)
Not enough data in X to fit this distribution.
Error in coin_toss (line 15)
histfit(lots_toss,10);
I also generate the same mean for the loop, when I am trying to generate 100 different means for the histogram.
I'm not sure what I'm missing but I think I need to change something in the loop index, but I'm not entirely sure of how to code it and have been searching how to do this-I think right now I have it hard programmed in to just display the same number repeatedly which is why I'm getting the error I'm seeing down below. If someone can help point me in the right direction on how to alter this slightly I think it should be okay? I'm fairly sure it's not THAT far off.
Thank you!!
  1 件のコメント
Jasson
Jasson 2025 年 4 月 30 日
You're actually really close — great start, especially if you're just getting into this!
The main issue is that you're calculating the mean of the same coin toss 100 times in the loop, instead of generating 100 different toss samples and storing their means. That’s why you’re getting the same number over and over, and why histfit is complaining — it only sees one value instead of a whole list.
Here’s how you can fix it in a simple way:
samplesize = 50; % Number of coin tosses per sample
nsamples = 100; % Number of samples
means = zeros(nsamples,1); % Store the mean of each sample
for i = 1:nsamples
coins = (binornd(1, 0.5, samplesize, 1) * 2) - 1; % Simulate coin tosses: -1 = tails, 1 = heads
means(i) = mean(coins); % Store the mean of this sample
end
% Now plot the histogram of the means
histfit(means, 10); % 10 bins for the histogram
This version generates 100 different samples, calculates the mean for each, and then plots all the means in a histogram with a fitted curve. Now you'll get a proper distribution!
And if you're ever unsure in real life… just flip a coin online! 😊
Let me know if you want to try other visualizations too!

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2020 年 11 月 8 日
There is no need to use a loop.
First, get used to using meaningful variable names. You have samplesize, and nsamples. What do they mean? I'll guess this:
nsim = 10000; % how many simulated runs? Here I used 10000 total simulated runs.
tossespersim = 100; % each simulated run has 100 coin tosses
There is no need to use binornd at all. But you can if you want to do so. Rand is simpler.
p = 0.5; % the coin is fair. p is the probability of a head
coins = (rand(nsim,tossespersim) <= p)*2 - 1; % -1 means we got a tail, 1 is a head.
Now just use mean to compute the means.
meantoss = mean(coins,2);
meantoss will be a vector of of length nsim.
size(meantoss)
ans = 1×2
10000 1
The histogram is simple.
nbins = 10;
histfit(meantoss,nbins)
  1 件のコメント
Kathryn Janiuk
Kathryn Janiuk 2020 年 11 月 8 日
Oh my gosh, thank you for being so thorough! I learned a lot from this, I really appreciate it!

サインインしてコメントする。

その他の回答 (1 件)

David Hill
David Hill 2020 年 11 月 8 日
編集済み: David Hill 2020 年 11 月 8 日
a=mean((-1).^randi(2,100));
histogram(a,100);
  1 件のコメント
senthil
senthil 約10時間 前
you can check this flip a coin tool site.

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by