CHE 492 HW 5 Abdulaziz Alhouti
CHE 492 HW 5 Abdulaziz Alhouti
CHE 492 HW 5 Abdulaziz Alhouti
1. Question 1:
1.1. Objective:
The first problem looks into the probability of a sum of two dice being rolled for multiple
trials to see if a sum of seven is the most likely outcome. A MATLAB script is used to
simulate the dice rolling for a specified number of trials and determine the longest 7 sum
streak. The results obtained are analyzed and visualized using a heatmap.
1.2. Methods:
I developed a 6x6 matrix named results to collect the dice output in order to evaluate the
probability of their outcome. I recorded the longest run of output dice with a sum of seven
with a variable called maxsevens. I then implemented the function randi([1 6]) that
generated random integers between 1 and 6 for each iteration of the used for loop which is
explained in the code snippet in figure 1 below. The total number of outcomes was
determined using the variable totals, and an if statement was used to see if there was a
consecutive sequence of output 7 along with the longest streak of this result. Finally, the total
number of outcomes was divided by the total number of dice rolls to get the probability of
outcomes and the heatmap() function was used to visualize these findings. The full
MATALAB script can be found in Appendix A.
for i = 1:1:rolls %This for loop randomly rolls 2 dice for a specified number of rolls in order to
determine the probability of the outcome sum of these dice
%Roll the dice nb.1 using randi() function
%Roll the dice nb.2 using randi() function
%Assign the result of the two dice to current
%Increment the individual outcome numbers by 1
%Sum the 2 dice outcome
%Calculate the number of times an outcome was obtained and increment it by 1
if sum == 7 %if conditions to check whether sum is or not 7
if streak == "YES" %if conditions to check whether sum is a streak of 7 or not
%Increment the number of times a 7 streak was obtained by 1
end
if streak == "NO" %Check if the outcome wasn’t 7 and thus no 7 streak
%Set streak value to Yes and sevensstreak to 1
end
end
if sum ~= 7 && streak == "YES" %check if dice output sum is equal to 7 and it’s a streak
%Calculate new streak number of outcomes 7
if thissevens > maxsevens %if statement to determine whether this is the maximum streak of 7
%Set the new longest 7 streaks;
end
%Reset thissevens variable to 0 to determine the length of the new streak
end
end
Figure 1. Code snippet of the for loop used to simulate the rolling of 2 dice with their respective outputs
and the maximum streak of sevens
1
1.3. Results:
The results obtained from the MATLAB script are visualized in the heatmap represented in
Figure 2 below. The probability distribution ranged from 0 for the outcome 1 which is an
impossible outcome of 2 dice to a maximum value for the outcome 7 ranging around a
probability of 0.16. The heatmap represented in Figure 2 below seems to be resembling a
normal distribution where there is a dense probability in the center of the heatmap for a sum
of 7 and decreases gradually as the sum decreases or increases towards 0 and 12 respectively.
Finally, I checked for the longest streak of 7 obtained which was equal to 6.
Figure 2. Heat map showing a normal probability distribution for the sum to 2 dice randomly
rolled for 100,000 rolls
Figure 2 clearly represents a normal probability distribution peaking at the sum of 7 and
decreasing as we further go towards the edges towards the right and left edges.
1.4. Discussion
The results obtained from MATLAB are very interesting as the sum of 7 had the most
probable outcome despite the fact that the probability a dice would land on a certain number
is equal for all its sides. Further investigating this outcome, I found out that casinos develop
their games based on probability distributions in order to remain profitable. This is
implemented in card games also based on probability distributions for decks constituting of
52 cards. I also performed several MATLAB runs in order to find how the probability of
landing a 7 changed as the number of rolls increased as can be seen in Table 1 below.
2
Table 1. Probability of landing a 7 as a function of the number of times a dice is rolled
Table 1. above reveals that in order to be profitable in a gambling game, one must choose the number 7
for a dice game and play it for several times to increase his chances of winning.
3
2. Question 2:
2.1. Objective:
This question looks into the path a frog takes to cover a 3 by 3 block by calculating the
average number of its moves. For this purpose, a MATLAB code is created to simulate the
frogs path and obtain the several outputs which are later averaged.
2.2. Methods:
To simulate a frog’s path, I first represented the frog’s position on the 3 by 3 board by a row
position using the variable frogr and a column position using frogc. The frogs
position was randomly assigned using the randi()function. To cover the entire board, if
conditionals are employed in a while loop to drive the frog to cover the vacant board spots.
The frog was moved randomly using the randi()function generating a random moving
variable which was then added to the frog's position. The heatmap() was used to display
the simulation and the total number of moves taken by the frog was finally determined. The
full MATALAB script can be found in Appendix B.
2.3. Results:
After running the MATLAB script for the first trial, the heat map revealed the number of
moves the frog has taken to cover the full board which is equal to 38 as can be seen in Figure
3 below. However, knowing that the frog was assigned a random position with a randomly
oriented sequence with respect to this position and in order to determine the average number
of moves, I added a for loop and ran the MATLAB script for several trials in order to
finally average the obtained results as can be seen in Table 2 below.
In order to determine the average number of moves the results obtained and represented in
Table 2 above are averaged as follows:
38+20+24 +20+ 41+24 +27
Average number of moves= =27.7 moves
7
Therefore, the average number of moves from the 7 performed MATLAB runs is 27.7 moves.
2.4. Discussion:
The results obtained are close to the true average of moves a frog has to take in order
to cover the full board, however; in order to obtain the true average, the number of
runs performed must approach a large number. This is also the case for all
applications in the industry where the average is obtained after numerous amount of
trials, thus increase process efficiency and safety.
5
3. New Question:
Consider there is a baseball game where the thrower has to throw the ball till it reaches
behind the batter. What is the probability it will pass the batter assuming there is a 9x9 block
representing the area a bater can cover? Write a MATLAB code to determine this probability
assuming the bater has a 50% chance to hit the ball back.
6
Appendix A
A.1: Full script used to simulate the rolling of 2 dice with their
respective outputs and the maximum streak of sevens.
A.1: Full script used to simulate the rolling of 2 dice with their respective outputs and the
maximum streak of sevens:
%October 20, 2021
%Rolling Dice
results = zeros(6, 6); %this will feed the heatmap
totals = zeros(1, 12);
rolls = 200000; %1 million
maxsevens = 0; %longest streak of sevens
thissevens = 0; %current streak of sevens;
sevensstreaks = []; %create variable sevenstreaks matrix
streak = "NO"; %set initial streak value to No
for i = 1:1:rolls %This for loop randomly rolls 2 dice for a specified number of rolls in order
to determine the probability of the outcome sum of these dice
die1 = randi([1 6]); %Roll the dice nb.1 using randi() function
die2 = randi([1 6]); %Roll the dice nb.2 using randi() function
current = results(die1, die2); % Assign the result of the two dice to current
results(die1, die2) = current + 1; % Increment the individual outcome numbers by 1
sum = die1 + die2; % Sum the 2 dice outcome
value = totals(1, sum); %Calculate the number of times an outcome was obtained and
totals(1, sum) = value +1; %increment it by 1
if sum == 7 %if conditions to check whether sum is a streak of 7 or not
if streak == "YES" %if condition for a streak of 7
thissevens = thissevens + 1; %Increment the number of times a 7 streak was obtained
%By 1
end
if streak == "NO" %Check if the outcome wasn’t 7 and thus no 7 streak
streak = "YES"; %Set streak value to Yes
thissevens = 1; %and sevensstreak to 1
end
end
if sum ~= 7 && streak == "YES" %check if dice output sum is equal to 7 and a streak
streak = "NO"; %string of 7s is broken
sevensstreaks = [sevensstreaks, thissevens]; %Calculate new streak number of outcomes 7
if thissevens > maxsevens %if statement to determine whether this is the maximum streak
%of 7
maxsevens = thissevens; %Set the new longest 7 streaks;
end
thissevens = 0; %Reset thissevens variable to 0 to determine the length of the new streak
end
end
totals = totals/rolls; %Calculate the probability of the sum obtained from rolling 2 dice several
%times
h = heatmap(totals); %create the heatmap object
h.Colormap = turbo; %return the turbo colormap as a three-column array with the same number of
rows as the colormap for the current figure
h.Title = "Dice Rolls"; %assign a title for the heatmap
h.XLabel = "Sum of Two Dice"; %name of x-axis
1A
Appendix B
B.1: Full script used to determine the average number of moves
taken by frog’s path to cover a 3 by 3 matrix block.
B.1: Full script used to determine the average number of moves taken by frog’s path to cover a 3 by
3 matrix block.:
%Frog Hop on a Board
%October 20. 2021
side = 3; %side length of the block (3 blocks on each side)
board = zeros(side, side); %Create a 3x3 matrix board and fill it with zeroes
%Randomly place the frog on the board
frogr = randi([1 side]); %Create a random row position for the frog
frogc = randi([1 side]); %Create a random column position for the frog
board(frogr, frogc) = 1; %position of frog on board
for i=1:1:100000 %for loop to determine the average number of moves a frog has made
while ismember(0, board) %while loop used to move the frog towards the empty board spaces to cover the full
%board
%determine position of frog and the allowable moves
%allowable moves if not at an edge or corner
rmoves = [-1, -1, 0, 1, 1, 1, 0, -1];
cmoves = [0, -1, -1, -1, 0, 1, 1, 1];
%corners
if frogr == 1 && frogc == 1 %if conditional for frog movement at corners
rmoves = [1, 0, 1];
cmoves = [0, 1, 1];
end
if frogr == 1 && frogc == side %if conditional for frog movement at corners
rmoves = [0, 1, 1];
cmoves = [-1, 0, -1];
end
if frogr == side && frogc == side %if conditional for frog movement at corners
rmoves = [-1, 0, -1];
cmoves = [0, -1, -1];
end
if frogr == side && frogc == 1 %if conditional for frog movement at corners
rmoves = [0, -1, -1];
cmoves = [1, 0, 1];
end
%sides but not corners
if frogr == 1 && frogc ~= 1 && frogc ~= side %if conditional for frog movement at sides but not corners
rmoves = [0, 1, 1, 1, 0];
cmoves = [1, 1, 0, -1, -1];
end
if frogc == 1 && frogr ~= 1 && frogr ~= side %if conditional for frog movement at sides but not corners
rmoves = [1, 1, 0, 1, 1];
cmoves = [0, 1, 1, 1, 0];
end
if frogr == side && frogc ~= 1 && frogc ~= side %if conditional for frog movement at sides but not corners
rmoves = [0, -1, -1, -1, 0];
cmoves = [1, 1, 0, -1, -1];
end
if frogc == side && frogr ~= 1 && frogr ~= side %if conditional for frog movement at sides but not corners
rmoves = [-1, -1, 0, 1, 1];
cmoves = [0, -1, -1, -1, 0];
end
value = numel(rmoves); %how many possibilities are in my moves?
pick = randi([1, value]); %get a random number based on array length
frogr = frogr + rmoves(pick); %move frog row
frogc = frogc + cmoves(pick); %move from column
value = board(frogr, frogc); %get current board value
board(frogr, frogc) = value + 1; %increase board value by 1
heatmap(board); %heatmap
pause(0.1);
end
end
totalmoves = sum(board, 'all') %get total number of moves frog made
1B