Poker bar graph, probability

조회 수: 10 (최근 30일)
Linus
Linus 2013년 10월 10일
편집: Jonathan LeSage 2013년 10월 20일
Hello.
Im trying to come up with a program that shows the probability of poker hands of pairs/twopairs/three of a kind etc... I want to put in the number of hands, and the program will show how many times i will get pairs/twopairs/three... in a bar graph or histogram.
All help is appreciated.

채택된 답변

Jonathan LeSage
Jonathan LeSage 2013년 10월 18일
편집: Jonathan LeSage 2013년 10월 20일
An interesting problem for sure, as there are many methods through which you could tackle this problem. If you're relatively new with MATLAB or coding in general, this is a great problem that you can tackle to learn about both probability and programming! Hopefully the code and discussion I provide can help jump start you on your own project!
Perhaps the most straightforward method for computing probabilities of complex systems (a poker game being one of these) is to use the Monte Carlo method. Effectively, you write some code that simulates a hand of poker, and then run that bit of code thousands of times. You can compute the probabilities of events this way.
Since I am not sure what particular format of poker you're interested in simulating, I'll leave the nuances to you. What I did was code up a very simple Monte Carlo poker simulator. In the code, I only count up pairs, three-of-a-kinds, and four-of-a-kind. I also am just looking the probabilities of being dealt these hands directly. In other words, I am not allowing draws. We effectively deal a thousand hands and see how often certain poker hands arise. Then we can plot the probabilities on a bar chart using the bar function.
Really, the only tricks in this code are the use of the randperm function to "shuffle" the deck and the histc function to count up hands. Hope this helps to get you started!
% Monte Carlo Generation of Poker Hand Probability
% Simple example with no suits, and a single deal of five cards with no
% draw. Only looking at pairs, three-of-a-kinds and four-of-a-kinds.
% Generate all cards in the deck
oneSuit = 1:13;
deck = repmat(oneSuit,1,4);
numCards = numel(deck);
% Run Monte Carlo Simulations
% Effectively play large number of hands to determine probabilities
numSimulations = 1000;
numPlayers = 4;
numCardsDealt = 5;
% Create matrix to store pairs, etc. for each player
% Three rows as this code only counts pairs, three-of-a-kinds,
% and four-of-a-kinds.
handsTotal = zeros(3,numPlayers);
for i = 1:numSimulations,
% Randomize card vector, "shuffle the deck"
shuffleIndex = randperm(numCards);
deckShuffled = deck(shuffleIndex);
% Deal hands (5x cards a player)
dealCards = deckShuffled(1:numPlayers*numCardsDealt);
dealCards = reshape(dealCards,numPlayers,numCardsDealt);
% Count the cards in each hand
cardCount = histc(dealCards',oneSuit);
% Count singletons, pairs, etc. Eliminate singletons and no card counts
handCount = histc(cardCount,0:4);
handCount = handCount(3:end,:);
% Add counts from this deal to overall count
handsTotal = handsTotal + handCount;
end
% Plot results
figure;
subplot(2,1,1)
bar(handsTotal./numSimulations);
grid on;
title('Hand Probability per Player');
ylabel('Probablity')
set(gca,'XTick',1:3,'XTickLabel',{'Pair','Three-of-kind',...
'Four-of-kind'});
subplot(2,1,2)
bar(sum(handsTotal,2)./(numSimulations*numPlayers));
grid on;
title('Overall Hand Probability');
ylabel('Probability')
set(gca,'XTick',1:3,'XTickLabel',{'Pair','Three-of-kind',...
'Four-of-kind'});
Also, another recommendation. You can always check to see if someone has made their own code available on the File Exchange! The following submission may peak your interest:
  댓글 수: 1
Linus
Linus 2013년 10월 20일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Card games에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by