フィルターのクリア

Need help calling a function

3 ビュー (過去 30 日間)
David Deman
David Deman 2021 年 12 月 8 日
コメント済み: David Hill 2021 年 12 月 9 日
Hello so I am trying to replicate a yahtzee game, and the statistics of how many times do 5 dice have to be rolled to get 5 of the same kind of dice. I am writing a main script that calls multiple functions.
My first function called rolls 5 dice, and displays what numbers are rolled. then my second function determines which number occurs the most in the function, and the third displays which number in the vector needs to be rolled again
%Main Script
% 2.1 Several Throws
numOfDice= 5
V = SeveralThrows(numOfDice);
% generate random numbers from 1 to 6 of vector size numOfDice
%Input amount of dice rolled
% plotting histogram
% 2.2-2.3
die=SeveralThrows(5);
out=numOutcome2(die);
% 2.4
out2=FindtheDicetoThrowAgain([V])
And my called functions are
%2.1 Several Throws
function V = SeveralThrows(numOfDice)
% generate random numbers from 1 to 6 of vector size numOfDice
V = ceil(randi([1,6],1,numOfDice));
%Input amount of dice rolled
% plotting histogram
histogram(V);
xlabel('Face of Die')
ylabel('Number of Die')
end
% 2.2 Count The Number of Each Outcome
function outcomeFreq = numOutcome(outcome)
% assigning zeros of vector size 1 x 6.
outcomeFreq = zeros(1,6);
% counting frequency of numbers in vector outcomeFreq.
for i = 1:length(outcome)
outcomeFreq(outcome(i)) = outcomeFreq(outcome(i)) + 1;
%Counts times a number 1 through 6 occurs in input vector
end
end
% 2.3 Find Out Which Outcome is Most Common
function commonOutcome = numOutcome2(outcome)
%assigning outcomeFreq to zeros of vector 1 x 6
outcomeFreq = zeros(1,6);
% finding frequency of numbers from 1 to 6.
for i = 1:length(outcome)
outcomeFreq(outcome(i)) = outcomeFreq(outcome(i)) + 1;
end
% assigning commonOutcome to 1.
commonOutcome = 1;
% finding common outcome.
for i = 2:6
if(outcomeFreq(i)>outcomeFreq(commonOutcome))
commonOutcome = i;
%Input vector will create an output that displays most common integer
end
end
end
%2.4 Find the Dice to Throw Again
function throwAgain = MCO_throwAgain(RV)
CV = [0, 0, 0, 0, 0, 0];
for i=1:6
for j=1:length(RV)
if RV(j) == i
CV(i) = CV(i) + 1;
end
end
end
MFO = find(CV == max(CV));
MFO = MFO(1);%if there are more than 1 MFO, then select the one which is at index 1
throwAgain = find( RV~=RV(MFO));
%here we are finding the indices in MFO where the value is not equal to MFO
%so these indices need to be thrown again
end %End the function
So what I need help on is to call this function I have written that takens the selected die that need to be rerolled, and rerolls them till i have 5 of a kind, as well as repeating this process for x amount of times and graphing it.
%2.5 Five-of-a-Kind
throwAgain=MCO_throwAgain()
function throwAgain=MCO_throwAgain()
% MCO_throwAgain outputs the number of random throws taken to have at least 5 same
% die points. Optionally it also outputs the final throw
% points.
%First Roll
die=1;
%Random number 1-6 of a 1x6 vector representing 6 dies
% at least 5 same points
RV=randi([1,6],1,6);
flag=0;
% keep trying
while(1)
% check if at least 5 dies have same point
for i=1:6
if(sum(RV==i)>=5)
flag=1;
break;
end
end
% if yes, stop
if(flag==1)
break;
end
% Otherwise, throw again
die=die+1;
RV=randi([1,6],1,6);
end
%Display # of trials
throwAgain=die;
end
Thank you!

回答 (1 件)

David Hill
David Hill 2021 年 12 月 8 日
編集済み: David Hill 2021 年 12 月 8 日
Not exactly sure what you are asking. Do you want to call the last function in your main function? I simple way to look at the occurance of rolling a yantee from five dice is:
x=histc(randi(6,2e6,5),1:6,2);%rolls 5 dice 2 million times
MaxNum=max(x,[],2);%determines the maximum of the same number on each dice
histogram(MaxNum);%plots histogram
nnz(MaxNum==5)*100/length(MaxNum);%percent yantees out of the 2 million rolls
  2 件のコメント
David Deman
David Deman 2021 年 12 月 8 日
Thanks for answering. Im not sure if my 2.5 function is correct, but I want to use it to take the output from the main function, and reroll those dice till there are 5 of a kind. Then I need to repeat this say 10,000 times, and plot these results with a curve fitted histogram. I am struggling to see where I could call function 2.5 in my main.
David Hill
David Hill 2021 年 12 月 9 日
Take a look at the output here.
y=randi(6,2e6,5);%two million initial throws
r=[];
count=1;
while 1
h=histc(y,1:6,2);%bins the dice
[m,idx]=max(h,[],2);%finds max number of same dice and index of the same
Idx=m==5;%checks for yahtzee
r=[r;repmat(count,nnz(Idx),1)];%sets the number of throws for any yahtzee
count=count+1;
y(Idx,:)=[];%eliminates the yahtzee for additional throws
idx(Idx)=[];%eliminates yahtzee from index
ny=randi(6,size(y,1),5);%throws the dice again
if isempty(y)
break;
end
rr=y~=idx;%selects the dice needed to be rethrown
y(rr)=ny(rr);%inserts those dice thrown again into the orginal (reduced by yahtzee) matrix of throws
end
histogram(r);%plots histogram of throws needed to get a yahtzee

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWord games についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by