Poker bar graph, probability

10 vues (au cours des 30 derniers jours)
Linus
Linus le 10 Oct 2013
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.

Réponse acceptée

Jonathan LeSage
Jonathan LeSage le 18 Oct 2013
Modifié(e) : Jonathan LeSage le 20 Oct 2013
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 commentaire
Linus
Linus le 20 Oct 2013
Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Card games dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by