Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MatLab code Write a MATLAB script to simulate rolling dice. First ask the user h

ID: 1766122 • Letter: M

Question

MatLab code Write a MATLAB script to simulate rolling dice. First ask the user how many times (call it n) he/she wants to roll the dice. 1. Roll a dice n times and calculate how many times it lands 6 2. Roll two dice simultaneously n times and calculate how many times they land the same. the summation of the numbers is between 5 and 8 inclusively. the summation of the numbers is divisible by 3 3. Roll two dice simultaneously n times and calculate how many times 4. Roll two dice simultaneously n times and calculate how many times Run your code for n- 1000 and report the results using fprintf

Explanation / Answer

MATLAB code is given below.

clc;
close all;
clear all;

n = 1000;
fid=fopen('ROLL_DICE.dat','w');

% Question 1
% Rolling a dice
Times_Dice_is_6 = length(find( ceil(rand(1,n)*6) == 6))


% Question 2
% Rolling a 2 dice
Times_2Dice_same = length(find( ceil(rand(1,n)*6) == ceil(rand(1,n)*6)))


% Question 3
% ROlling 2 Dice & Sum is between 5 and 8 inclusively
% i.e. the sum is either 5 or 6 or 7 or 8
A = ceil(rand(1,n)*6);% Dice 1 1000 time
B = ceil(rand(1,n)*6);% Dice 2 1000 time

i = 0;
for k = 1:length(A)
if (A(k) + B(k) >= 5 && A(k) + B(k) <= 8)
i = i+1;
end
end
Time_sum_bw_5_and_8 = i

% Question 4
% # times the sum is divisible by 3
m=0;
for k = 1:length(A)
if mod((A(k) + B(k)),3) == 0
m = m+1;
end
end
Times_sum_divisible_by_3 = m

fprintf(fid,'%6.3f %6.3f %6.3f %6.3f',...
Times_Dice_is_6,Times_2Dice_same,i,m);

Result:

Times_Dice_is_6 =

172


Times_2Dice_same =

169


Time_sum_bw_5_and_8 =

534


Times_sum_divisible_by_3 =

364