Choosing 5 card hands from a 52 card deck

7 visualizzazioni (ultimi 30 giorni)
My code so far is:
c = ['A23456789TJQK']';
s = ['HDSC']';
d = [repmat(c,4,1),repmat(s,13,1)];
for i=1:10;
y(1,i) = datasample(d,5,'Replace',false);
end
I can create the deck vector and have it take one 5 card hand sample, but I'm having trouble making it take 5 card hands for i amount of times. I essentially want a y matrix that is size 5 x i.

Risposta accettata

Walter Roberson
Walter Roberson il 3 Lug 2015
numhands = 20;
per_hand = 5;
c = ['A23456789TJQK']';
s = ['HDSC']';
[C,S] = ndgrid(c,s);
cards = cellstr( [C(:), S(:)]);
[~, dealorder] = sort(rand(numhands,52));
and now
y = cards(dealorder(:,1:per_hand));
Note that the result would be a 20 x 5 cell array of strings.

Più risposte (1)

Image Analyst
Image Analyst il 3 Lug 2015
Try this:
% Define parameters.
numberOfHands = 10 % i.e. # persons playing cards.
numCardsPerHand = 5 % # cards each person holds
numCardsToDeal = numberOfHands * numCardsPerHand;
if numCardsToDeal <= 52
% We have enough cards to deal.
% Get the cards. Each card is numbered from 1 - 52
cards = randperm(52, numCardsToDeal);
% Reshape into numberOfHands rows high
% by numCardsPerHand columns wide.
% So each row is one hand.
hands = reshape(cards, numberOfHands, numCardsPerHand)
else
% we don't have enough cards to deal.
uiwait(warndlg('Not enough cards'));
end
The cards are numbered 1-52. That will be easier to use in whatever code follows this than if you had a character array and also had to have a "suit" array to keep track of what suit each number was.
  2 Commenti
Joey
Joey il 3 Lug 2015
Modificato: Joey il 3 Lug 2015
The way I want it to work though is after every 5 card draw, it replaces those 5 cards and then redraws 5 cards again. The goal is to eventually figure out the percent chance of getting certain hands like "royal flush" or "four of a kind" etc.
Image Analyst
Image Analyst il 4 Lug 2015
Joey, this is a very simple Monte Carlo simulation (no pun intended). Hopefully this is not homework and you got something very similar to this:
% Define parameters.
numberOfHands = 200000 % i.e. # persons playing cards.
numCardsPerHand = 5 % # cards each person holds
fourOfAKind = 0;
royalFlushes = 0;
% Monte Carlo simulation:
for h = 1 : numberOfHands
% Get the cards. Each card is numbered from 1 - 52
% 2 is card 1, and Ace is card 13 (for ease of testing for Royal Flushes).
cards = randperm(52, numCardsPerHand);
% Check for four of a kind
sameSuit = (min(cards) >= 1 && max(cards) <= 13) || ...
(min(cards) >= 14 && max(cards) <= 26) || ...
(min(cards) >= 27 && max(cards) <= 39) || ...
(min(cards) >= 40 && max(cards) <= 52);
if sameSuit
fourOfAKind = fourOfAKind + 1;
end
% Check for a straight
% An easy exercise left for Joey.
% Check for Royal flush
royalFlush = (min(cards) == (13 - numCardsPerHand + 1) && max(cards) == 13) || ...
(min(cards) == (26 - numCardsPerHand + 1) && max(cards) == 26) || ...
(min(cards) == (39 - numCardsPerHand + 1) && max(cards) == 39) || ...
(min(cards) == (52 - numCardsPerHand + 1) && max(cards) == 52);
if royalFlush
royalFlushes = royalFlushes + 1;
end
fprintf('Done with hand #%d of %d\n', h, numberOfHands);
end
fprintf('Found %d 4 of a kind in %d hands\n', fourOfAKind, numberOfHands);
fprintf('Percentage of 4 of a kind = %f%%\n', 100*fourOfAKind/numberOfHands);
fprintf('Found %d Royal Flushes in %d hands\n', royalFlushes, numberOfHands);
fprintf('Percentage of Royal Flushes = %f%%\n', 100*royalFlushes/numberOfHands);

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by