Clear Filters
Clear Filters

Help needed on a Yahtzee game!

5 views (last 30 days)
David Deman
David Deman on 7 Dec 2021
Commented: David Deman on 8 Dec 2021
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.

Answers (1)

KSSV
KSSV on 7 Dec 2021
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 Comments
KSSV
KSSV on 7 Dec 2021
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 on 8 Dec 2021
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!

Sign in to comment.

Categories

Find more on Word games in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!