Help needed on a Yahtzee game!

5 次查看(过去 30 天)
David Deman
David Deman 2021-12-7
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 CenterFile Exchange 中查找有关 Word games 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by