Help needed on a Yahtzee game!
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!