using loops to play blackjack
显示 更早的评论
Use Matlab to calculate the odds of landing blackjack on the first hand dealt. REF: In blackjack you are dealt two cards from a standard deck of 52. There are:
4 Aces worth 11 points each (in your first two cards)
16 Jacks, Queens, Kings, and Ten-cards combined worth 10 points each
4 Nine-cards worth 9 points
4 Eight-cards worth 8 points
And so on down to two-cards
I.E. The probability of getting a 10 point card is 16/52 or 30.769%;
The probability of getting an Ace card is 4/52 or 7.692%;
To hit blackjack you MUST get an ace and a 10 card dealt (in either order)
Run this a different number of iterations, (try a high number) and report to the user what the average chance is of hitting blackjack.
As you likely know from statistics, the theoretical odds are (assuming ONE deck of cards): (4/52)*(16/51)+(16/52)*(4/51) or 4.83% Use to check your answer
Hint: Use a FOR loop (for number of attempts) and nested IF statements to calculate
count=0;
tries=10^7; %10 million tries; This will still run faster than 1 million iterations above using RANDI!
for i=1:tries
card1=rand; %1st card (random integer), 1-52
card2=rand; %2nd card (random integer), 2-52
card3=rand; %3rd card (random integer), 3-52
card4=rand; %4th card (random integer), 4-52
card5=rand; %5th card (random integer), 5-52
if card1<=(4/52) %4/52 chance of hitting the 1st card
if card2<=(16/51) %16/51 chance of hitting the same card on card 2
if card3<=(16/52) %16/52 chance on the 3rd card
if card4<=(4/51) %4/51 chance on the 4th card
%disp('You got 4 of a kind! Holy crap!'); %You can uncomment this to see how often you hit 4 of a kind while the code is running
count=(4/52)*(16/51)+(16/52)*(4/51);
end
end
end
end
end
chance=count/tries;
disp(['You hit 4 of a kind ' num2str(count) ' times, or'])
disp([num2str(chance*100) '% of the time.'])
5 个评论
David Hill
2020-4-13
What is your question?
Todd Wyzkiewicz
2020-4-13
Geoff Hayes
2020-4-13
Todd - I think that on each iteration of your simulation, you just need to generate two cards and check to see if exactly one is an ace (from any of the four suits) and that one is a 10, Jack, Queen, or King (from any of the four suits). I'm not sure why your code is referencing four of a kind unless that is copied from another assignment.
Todd Wyzkiewicz
2020-4-13
Todd Wyzkiewicz
2020-4-13
采纳的回答
更多回答(1 个)
Steven Lord
2020-4-13
Coding the cards so the aces are 1:4 and the tens are 5:20 is okay, but we can get closer to the actual game with our simulator.
J = 10;
Q = 10;
K = 10;
A = 11;
oneSuit = [A 2:10 J Q K];
deck = repmat(oneSuit, 1, 4);
Yes, I know there's no suit information in deck but that's okay since we don't care about suits in blackjack.
Draw two cards (without replacement) from deck using randperm. If their sum is 21, you've hit a blackjack. No need for ismember or remembering whether you stored the aces first (meaning the order in oneSuit would be [A 2:10 J Q K]) or last ([2:10 J Q K A]).
If you were recording all the totals for the first two cards, you'd need to replace 22 (two A cards) with 12 (one A being 1 and the other 11) to get an accurate histogram. You could count both aces as 1 but you'd probably only want to do that if your third card was worth 10 and we're only dealing with the first two cards in this simulation.
2 个评论
Todd Wyzkiewicz
2020-4-13
Steven Lord
2020-4-13
This is an alternate approach / data structure to use to represent a deck, one that I think would make the code a bit more self-explanatory.
类别
在 帮助中心 和 File Exchange 中查找有关 Signal Integrity Kits for Industry Standards 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!