Choosing balls from the boxes

4 ビュー (過去 30 日間)
Michal Waligora
Michal Waligora 2022 年 5 月 10 日
編集済み: Torsten 2022 年 5 月 10 日
I found the following question to solve: Game with 13 boxes(no repetitions). Inside each box there is a ball of different colour. There are 3 colours (red, blue and green), and there are 4 balls per colour. One box contains a black ball.
Win: when we pick 4 of the same colour - award: red - 10, blue - 20, green - 50
Loose: when we find black - no award
I would like to write a program, to find the probability of picking the black ball before any 4 of the same colour was found.
My first idea was to write a "for" and than many "if" statements but my plan didn't work:
rCount: # of red balls founded
n = 13
for i = 1:n
if rCount == 4
disp('You won the game with red')
else \\'I would like to write here a code like keep going with the loop'
end
  3 件のコメント
Sam Chak
Sam Chak 2022 年 5 月 10 日
Did you get stuck after end?
Michal Waligora
Michal Waligora 2022 年 5 月 10 日
True, Question: write a program, to find the probability of picking the black ball before any 4 of the same colour was found

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

採用された回答

Torsten
Torsten 2022 年 5 月 10 日
編集済み: Torsten 2022 年 5 月 10 日
positions_red = 1:4;
positions_blue = 5:8;
positions_green = 9:12;
position_black = 13;
Ntrials = 1e5;
success = 0;
for i = 1:Ntrials
draw = randperm(13);
draw_black = find(draw==position_black);
draw = draw(1:draw_black-1);
redindraw = numel(intersect(draw,positions_red));
blueindraw = numel(intersect(draw,positions_blue));
greenindraw = numel(intersect(draw,positions_green));
if redindraw < 4 && blueindraw < 4 && greenindraw < 4
success = success + 1;
end
if mod(i,1000)==0
disp(i)
end
end
probability = success/Ntrials
  1 件のコメント
Michal Waligora
Michal Waligora 2022 年 5 月 10 日
Wow, that's great. Thank you

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by