MATLAB: Throws to get yatzy

yatzy

I have a schooltask where I'm about to do a function whom calculate how many thows its need to get yatzy with five dices.
I have done it pretty good I think, but with 50000 results I get the average throws to ~12 when it should be 191283/17248=*11.0902*
What have I done bad? I have looked over it alot and cant find anything wrong.
function [nr_throws] = nr_throws()
throw=[1,5]; % Matrix for 5 dices
nr_throws=1; % First throw
most_common=mode(throw); % Most occured dice in the throw
for dice=1:5
throw(dice)=ceil(6*rand); % Randomize the first throw
end
while max(std(throw))~=0 % Controll that the first throw not is yatzy
for dice=1:5
if throw(dice)~=most_common
throw(dice)=ceil(6*rand); % Randomize the throws that don't occur the most times
end
end
nr_throws=nr_throws+1; % New throw done
most_common=mode(throw); % Controlls the most occured throw again
end
Calculate the mean with:
function [mean] = mean()
i=1;
up_to=50000;
value=0;
while i <= up_to
value=value+nr_throws();
i=i+1;
end
mean=value/up_to;

Best Answer

Try this:
function [nr_throws] = nr_throws()
nr_throws=1; % First throw
throw = randi(6,1,5);
most_common=mode(throw);
while max(std(throw))~=0 % Controll that the first throw not is yatzy
nmc = (throw~=most_common);
throw(nmc) = randi(6,1,nnz(nmc));
nr_throws=nr_throws+1; % New throw done
most_common=mode(throw); % Controlls the most occured throw again
end
I get a mean of 11.mumble. Your problem was in the first few lines. throw = [1,5] makes a 1-by-2 vector ([1,5]), which meant that most_common was always coming out as 1. Then you were actually doing the first throw. And, BTW, you can generate multiple random numbers in a single line -- no need for a loop.
The stuff inside the while loop was working, I think. I just cleaned it up with some logical indexing. As I do.