How to find same values in a randi function

4 ビュー (過去 30 日間)
TheSaint
TheSaint 2024 年 2 月 25 日
コメント済み: Steven Lord 2024 年 2 月 25 日
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.

採用された回答

Image Analyst
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%

その他の回答 (1 件)

Torsten
Torsten 2024 年 2 月 25 日
Hand = randi(13,[1,5])
Hand = 1×5
3 5 1 5 10
arrayfun(@(i)nnz(Hand==i),1:13)
ans = 1×13
1 0 1 0 2 0 0 0 0 1 0 0 0
  1 件のコメント
Steven Lord
Steven Lord 2024 年 2 月 25 日
Alternately you could use histcounts instead of the arrayfun call.
Hand = randi(13,[1,5])
Hand = 1x5
6 7 2 11 10
[counts, edges] = histcounts(Hand, 1:14)
counts = 1x13
0 1 0 0 0 1 1 0 0 1 1 0 0
edges = 1x14
1 2 3 4 5 6 7 8 9 10 11 12 13 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])
counts2 = 1x13
0 1 0 0 0 1 1 0 0 1 1 0 0
edges2 = 1x14
1.0000 1.5000 2.5000 3.5000 4.5000 5.5000 6.5000 7.5000 8.5000 9.5000 10.5000 11.5000 12.5000 13.0000
edges2r = round(edges2)
edges2r = 1x14
1 2 3 4 5 6 7 8 9 10 11 12 13 13

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCard games についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by