Throws to get 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;
채택된 답변
추가 답변 (1개)
Sean de Wolski
2011년 5월 9일
Don't overwrite the builtin function mean!!! And don't overwrite the function mean (that you just created) with a variable mean!!! (maybe one more exclamation point?)
function [the_mean] = getMean()
i=1;
up_to=50000;
value=0;
while i <= up_to
value=value+nr_throws();
i=i+1;
end
the_mean=value/up_to;
I don't know if this is your problem or not, but let me know if it fixes it.
댓글 수: 3
Matt Tearle
2011년 5월 9일
Ditto nr_throws -- don't use a variable with the same name as the function. (repmat('!',1,5))
Also... for is neater than while:
up_to = 50000;
value = 0;
for i=1:up_to
value = value + nr_throws;
end
the_mean = value/up_to;
If you just want the mean, that will do, but it might be more useful right now (ie while debugging) to keep the values themselves:
throwvec = zeros(up_to,1);
for i=1:up_to
throwvec(i) = nr_throws;
end
the_mean = mean(throwvec);
% hist(throwvec), etc goes here for further analysis
Sean de Wolski
2011년 5월 9일
(repmat('!',1,5))
I think you have preemptively won the award for today's best answer.
Matt Tearle
2011년 5월 9일
I'm all about scalability.
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!