Monte Carlo Method Fails
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Board games in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!