필터 지우기
필터 지우기

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 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