In: Computer Science
1. Write a function named ”ThreeDicesAdd” to perform the
following ”experiment” 1000 times: roll 3
six-sided dice and add their values. Put the outcomes in a list
called addDiceList. So addDiceList should
have length 1000, and each of the 1000 elements should be an
integer from 3 to 18. Plot the histogram of
the outcomes.
2. For the same experiment as in question 1, write a function named
”ThreeDicesAdd2” to increase the
experiment times (N) and see how the histogram of the outcomes
changes. For each N, you will have one
histogram plot. Output all plots as a GIF file. (Choose N= 1000 :
10,000 : 1,000,000). matlab question
I have coded the following problem in MATLAB that solves the above problem. I have coded the two functions ThreeDicesAdd and ThreeDicesAdd2 and also attached the required histograms.
ThreeDicesAdd screenshot
ThreeDicesAdd code to copy
function addDiceList = ThreeDicesAdd()
% Initializing an one-dimensional array with size 1000
addDiceList = zeros(1, 1000);
for n = 1:1000
% The first throw of the dice
firstThrow = randi([1, 6]);
% The second throw of the dice
secondThrow = randi([1, 6]);
% The third throw of the dice
thirdThrow = randi([1, 6]);
% The total of all the throws
total = firstThrow + secondThrow + thirdThrow;
% We add that throw to the list.
addDiceList(n) = total;
end
end
ThreeDicesAdd2 screenshot
ThreeDicesAdd2 code to copy
function addDiceList = ThreeDicesAdd2(N)
% Initializing an one-dimensional array with size 1000
addDiceList = zeros(1, N);
for n = 1:N
% The first throw of the dice
firstThrow = randi([1, 6]);
% The second throw of the dice
secondThrow = randi([1, 6]);
% The third throw of the dice
thirdThrow = randi([1, 6]);
% The total of all the throws
total = firstThrow + secondThrow + thirdThrow;
% We add that throw to the list.
addDiceList(n) = total;
end
end
Driver code screenshot
Driver code to copy
res = ThreeDicesAdd(1000);
h = histogram(res);
Histograms: