Monte Carlo Method Fails
显示 更早的评论
I am trying to find the probability of getting a pair when drawing 2 cards from a regular deck that contains 52 cards by using Monte-Carlo Method. The function needs one input, a number of trials. However, I noticed that as the input increases the probability converges to zero. I think the theoretical probability of getting a pair is 3/51. Any ideas why my function fails?
function onepair(k)
p = zeros(1,k);
for i = 1:k
%% Making a regular deck with four patterns and 13 numbers (including A,J,Q,K)
% randperm is a function that creats an array with N elements that are
% randomly selected from 1:N
hearts = randperm(13);
diamonds = randperm(13);
spades = randperm(13);
clovers = randperm(13);
allcards = [hearts,diamonds,spades,clovers];
%% Picking up a card
% datasample is a function that picks random sample for an array
hands = datasample(allcards,1);
%% Removing the picked card from the deck
allcards(allcards==hands) = [];
allcards = [allcards,hands,hands,hands];
%% Picking up one more card from the deck
hands = [hands,datasample(allcards,1)];
%% Checking whether hands is a pair
if hands(1) == hands(2)
p(k) = 1;
end
pp(i) = (sum(p)/i) * 100;
end
plot([1:k],pp,'r.')
percentage = pp(end)
end
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Board games 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!