Sie sind auf Seite 1von 3

SCRIPT.

%PROJECT 1 BE_1500
%MICHAEL LUKETICH EY0350
%MICHELLE WESNER FV3681
%ALI ZAHOUI ED8560
%2.1
function [] =YAHTZEE(~)
x=input('please enter the number of throws:'); %allows for a easier entry of
the number of throws.
EXP=input('please enter the number of experiments you''d like to run:');
%allows you to easily input the experiments.
NUM_OF_THROWS=0;
for W=1:EXP %this loop runs the code for the number of experiments that are
inputed.
Random_result=ceil(6*rand(1,x));% m is the resultant vector, so the results of
the throws.
%2.2
[A,B]=size(Random_result); %determines the size of the vector thats input into
the function.
ONE=0;
TWO=0;
THREE=0;
FOUR=0;
FIVE=0;
SIX=0;
for i=1:B %this loop moves through the number of dice and counts the numbers
rolled.
if Random_result(i)==1
ONE=ONE+1; %counts the number of dices roles that come up 1.
end
if Random_result(i)==2
TWO=TWO+1; %counts the number of dices roles that come up 2.
end
if Random_result(i)==3
THREE=THREE+1; %counts the number of dices roles that come up
3.
end
if Random_result(i)==4
FOUR=FOUR+1; %counts the number of dices roles that come
up 4.
end
if Random_result(i)==5
FIVE=FIVE+1; %counts the number of dices roles that
come up 5.
end
if Random_result(i)==6
SIX=SIX+1; %counts the number of dices roles that
come up 6.
end
end
NUM=[ONE,TWO,THREE,FOUR,FIVE,SIX];% this creates a vector named NUM which save
the count of each of the rolls.
%2.3
MOST_COMMON=mode(Random_result); %mode takes the vector and finds the value
which comes up the most.
%2.5 start new function.
Q=0;

T=0;
THROW_COUNTER=1;
[Q]=YAHTZEE_part2 (B,Random_result,x,Q,MOST_COMMON);%calls the function that
checks if a yahtzee was rolled in the first roll.
while Q<5 %this loop runs until all the dice are the same number.
j=0;
Q=0;
while j<length(Random_result)%this loop runs all the numbers in the vector
once, then ends.
j=j+1;%counts which dice the loops on.
if Random_result(j)==MOST_COMMON %checks to see the if that dice is
already equal to the most common number.
T(j)=Random_result(j);%if the number is equal then it save that dice
as that number and moves on.
end
if Random_result(j)~=MOST_COMMON %checks if the number isn't equal to the
most common number.
T(j)=ceil(6*rand(1));%if it's not equal then it has the dice reroll
that dice.
end
Random_result(j)=T(j);%this saves the new dice roll into the old vector.
if Random_result(1:x)==MOST_COMMON %checks to see if all the dice are the
same number.
Q=6;%if they are then makes Q>5 and ends the loop.
end
end
THROW_COUNTER=THROW_COUNTER+1; %this counts the number of throws.
end
NUM_OF_THROWS(W)=THROW_COUNTER; %saves the number of throws for each cycle.
end
h=histogram(NUM_OF_THROWS,'BinWidth',1); %creates a histogram of all the
outcomes of all the throws for the number of experiments.
hold on
%2.7
EXPECTED_VALUE=(1/EXP)*sum(NUM_OF_THROWS)%this calculates the expected 5value
that'll
for i=1:EXP %this loop runs through the results of the experiments.
P(i)=(NUM_OF_THROWS(i)-EXPECTED_VALUE)^2; %this is the first part of the
varience function.
end
VARIANCE=(1/(EXP-1))*sum(P) %this takes the first part of the varience
function and usses it to calculate the varience.
YAHTZEE_part3 (EXP);%this calls to use YAHTZEE_part3.
hold off
end
1st Called Function.
function [Q]=YAHTZEE_part2 (B,Random_result,x,Q,MOST_COMMON)
for Z=1:B %the loop checks the first roles to see if the first roll was a
yahtzee.
if Random_result(1:x)==MOST_COMMON %if the random results all equal the
same number then it changes Q to 6.
Q=6;
end
end

2nd Called Function.


function [STANDARD]=YAHTZEE_part3 (EXP)
e1=[1;0;0;0;0];
e5=[0;0;0;0;1];
E1TRAN=transpose(e1);%this turns the e1 vector.
A=[0 1/6 1/36 1/216 1/1296;0 5/6 10/36 15/216 25/1296; 0 0 25/36 80/216
250/1296;0 0 0 120/216 900/1296; 0 0 0 0 120/1296];
for i=1:EXP %this runs the loop for as many experiments there are.
P(i)=E1TRAN*(A^(i))*e5; %this is the probability density function.
end
STANDARD=plot(P*EXP); %this plots the probability density function.
xlim([0 50]); % this limits to be between 0 and 50.
end

Results.
>> YAHTZEE
please enter the number of throws:5
please enter the number of experiments you'd like to run:10000
EXPECTED_VALUE = 11.1972
VARIANCE = 40.1355

Das könnte Ihnen auch gefallen