How to find same values in a randi function
조회 수: 3 (최근 30일)
이전 댓글 표시
RunTotal = 100000;
NoPair = 0;
OnePair = 0;
TwoPairs = 0;
ThreeofKind =0;
FullHouse = 0;
FourofKind = 0;
FiveofKind = 0;
for i = 1:RunTotal
Hand = randi(13,[1,5])
I am trying to program the probability of getting pairs, full houses, and of kinds of a poker game. I want to use a randi function to generate the 5 card hand, but I cannot seem to figure out how to "read" the randi ouput and calculate how many pairs, full houses and of kinds. Any help is appreciated.
댓글 수: 0
채택된 답변
Image Analyst
2024년 2월 25일
I've already done it. You can look at the attached m-file code.
% Finds frequency of 5 card poker hands
% Reference
% https://en.wikipedia.org/wiki/Poker_probability#Frequency_of_5-card_poker_hands
% Theory says
% One pair 42.2569%
% Two pair 4.7539%
% Three of a kind 2.1128%
% Straight (excluding royal flush and straight flush) 0.3925%
% Flush (excluding royal and straight) 0.1956%
% Full house 0.1441%
% Four of a kind 0.0240%
% Straight Flush (excluding royal flush) 0.00139%
% Royal Flush = 0.000154%
Found 421681 "One Pair" in 1000000 hands. That is one in every 2 hands.
Percentage of "One Pair" = 42.168100%. Theory says 42.2569%
Found 47652 "Two Pairs" in 1000000 hands. That is one in every 21 hands.
Percentage of "Two Pairs" = 4.765200%. Theory says 4.7539%
Found 21137 "3 of a kind" in 1000000 hands. That is one in every 47 hands.
Percentage of "3 of a kind" = 2.113700%. Theory says 2.1128%
Found 3461 straights in 1000000 hands. That is one in every 289 hands.
Percentage of straights = 0.346100%. Theory says 0.3925%
Found 1877 Flushes (excluding straight and royal) in 1000000 hands. That is one in every 533 hands.
Percentage of Flushes = 0.187700%. Theory says 0.1956%
Found 1428 Full Houses in 1000000 hands. That is one in every 700 hands.
Percentage of Full Houses = 0.142800%. Theory says 0.1441%
Found 210 "4 of a kind" in 1000000 hands. That is one in every 4762 hands.
Percentage of "4 of a kind" = 0.021000%. Theory says 0.0240%
Found 4 straight flushes (excluding royal) in 1000000 hands. That is one in every 250000 hands.
Percentage of straight flushes = 0.000400%. Theory says 0.00139%.
Found 2 Royal Flushes in 1000000 hands. That is one in every 500000 hands.
Percentage of Royal Flushes = 0.000200%. Theory says 0.000154%
댓글 수: 0
추가 답변 (1개)
Torsten
2024년 2월 25일
Hand = randi(13,[1,5])
arrayfun(@(i)nnz(Hand==i),1:13)
댓글 수: 1
Steven Lord
2024년 2월 25일
Alternately you could use histcounts instead of the arrayfun call.
Hand = randi(13,[1,5])
[counts, edges] = histcounts(Hand, 1:14)
Note that the last edge is 14. If it were 13 the last bin would count both 12s and 13s in the data (as it would represent the closed interval [12, 13].) With the last edge being 14 the last bin represents [13, 14] and the next-to-last bin represents [12, 13). Alternately you could specify a BinMethod and BinLimits, though the bin edges aren't as nice (unless you round them.)
[counts2, edges2] = histcounts(Hand, BinMethod="integers", BinLimits = [1 13])
edges2r = round(edges2)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!