1 Write and test a MATLAB program that simulates the experim
1. Write and test a MATLAB program that simulates the experiment of rolling two dice N times and generates estimates of the following statistics:
a. The relative probability that the number of spots is equal to (2, 3, …, 11, 12)
b. The percentage difference between the most frequent and the least frequent of the 36 possible outcomes.
2. Run the program with N = 1,000, 10,000, 100,000 and 1,000,000
3. What do you observe about the behavior of the estimated statistics as N increases?
Solution
Matlab code:
clear all;
clc;
format long;
NN = [1000,10000, 100000, 1000000]
for x = 1:size(NN,2)
N = NN(x);
v1 = randi([1 6],1,N);
v2 = randi([1 6],1,N);
events = zeros(6,6);
for i = 1:N
events(v1(i),v2(i)) = events(v1(i),v2(i)) + 1;
end
v = v1 + v2;
probs = zeros(1,11);
for n = 2:12
ans = 0;
for i = 1:size(v,2)
if(v(i) == n)
ans = ans + 1;
end
end
probs(n) = ans/N;
end
fprintf(\'The relative probability that the number of spots is equal to (2, 3, …, 11, 12)\');
probs\'
p = (max(max(events)) - min(min(events)))/N*100;
fprintf(\'The percentage difference between the most frequent and the least frequent of the 36 possible outcomes: %d\ \', p);
end
