In: Electrical Engineering
Simulate the experiment of rolling two dices for 1000 times. Generate the probability mass distributionfunction(PMF) for the sum of the two dices and plot the result using histogramcommand. Use two different methods for your simulations: Using for loops Using vectors and matrices in MATLAB
MATLAB code using for loop:
clc; clear all;
for i=1:1000
dice_1(i)=randi([1,6],1);
dice_2(i)=randi([1,6],1);
end
sum_of_dices = dice_1'+dice_2';
disp('dice_1 dice_2')
disp([dice_1' dice_2'])
disp('sum of dices is')
disp(sum_of_dices)
histogram(sum_of_dices)
xlabel('sum of the value on the faces of two dices')
ylabel('number of times the sum occured')
Output:
MATLAB code using vectors and matrices:
clc; clear all;
dice_1 = randi([1,6],[1000,1]);
dice_2 = randi([1,6],[1000,1]);
sum_of_dices = dice_1+dice_2;
disp('dice_1 dice_2')
disp([dice_1 dice_2])
disp('sum of dices is')
disp(sum_of_dices)
histogram(sum_of_dices)
xlabel('sum of the value on the faces of two dices')
ylabel('number of times the sum occured')