フィルターのクリア

Help needed on a Yahtzee game!

5 ビュー (過去 30 日間)
David Deman
David Deman 2021 年 12 月 7 日
コメント済み: David Deman 2021 年 12 月 8 日
So I have a code that generates the amount of times dice are rolled to get 5 of a kind. How can I write a code that repeats this procedure x amount of times (say 10,000), and stores the result of each roll in a vector, as well as plotted out in a histogram. Thank you!
throwAgain=MCO_throwAgain()
function throwAgain=MCO_throwAgain()
die=1;
%Random number 1-6 of a 1x6 vector
RV=randi([1,6],1,6);
flag=0;
while(1)
for i=1:6
if(sum(RV==i)>=5)
flag=1;
break;
end
end
if(flag==1)
break;
end
die=die+1;
RV=randi([1,6],1,6);
end
throwAgain=die;
end
I have tried to write
diary('C:\Temp\Result.txt');
for i=1:10000
throwAgain()
end
diary('off');
histogram(throwAgain);
But this gives me the same number every time, so ill get say 196 10,000 times.

回答 (1 件)

KSSV
KSSV 2021 年 12 月 7 日
N = 10000 ;
throwAgain = zeros(N,1) ;
for i = 1:N
throwAgain(i)=MCO_throwAgain() ;
end
histogram(throwAgain)
function throwAgain=MCO_throwAgain()
die=1;
%Random number 1-6 of a 1x6 vector
RV=randi([1,6],1,6);
flag=0;
while(1)
for i=1:6
if(sum(RV==i)>=5)
flag=1;
break;
end
end
if(flag==1)
break;
end
die=die+1;
RV=randi([1,6],1,6);
end
throwAgain=die;
end
  3 件のコメント
KSSV
KSSV 2021 年 12 月 7 日
It looks like you have saved the function MCO_throwAgain.m in another folder and trying to call in another folder. See to it that, you are working in the same folder where the function is saved.
David Deman
David Deman 2021 年 12 月 8 日
So I have both of the scripts saved in the same folder on my computer, but when I run this program after running thow again i get the error
Unrecognized function or variable 'MCO_throwAgain'.
Error in Montecarlo (line 4)
throwAgain(i)=MCO_throwAgain();
Do I have to adjust the file names? currently I have them saved as FiveofaKind.m and Montecarlo.m.
Thank you!

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

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by