Please use MatLab to do the following questions Consider an
Please use MatLab to do the following questions:
Consider an experiment where you record the sum of two dice rolls.
(a) Create either a sub-function or an anonymous function that simulates the experiment.
(b) Create a sub-function that returns a vector of t = 10000 trials of the experiment.
(c) Create a primary function that returns the number of trials that give each of the distinct simulated sums of two dice rolls. (Count how many of the 10000 sums are 2, how many are 3,..., and how many are 12. )
Solution
function [ result ] = trials()
experiment = experimentTrial();
result = zeros(12,1);
for diceSum=2:12
result(diceSum) = sum( experiment == diceSum );
end
for diceSum=2:12
fprintf(\'For dice sum value=%d, Trials are %d\ \',diceSum,result(diceSum));
end
end
function sumOfDiceRoll = experimentTrial()
total = 10000; %total experiments : 10000
sumOfDiceRoll = zeros( total , 1 );
for t = 1:total
sumOfDiceRoll(t,1) = experiment();
end
end
function sum = experiment()
sum = 0;
%first dice roll
sum = sum + randi([1,6],1,1);
%second dice roll
sum = sum + randi([1,6],1,1);
end
