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

Matlab question While loop, nested or not? You be the judge. You will develop an

ID: 2082084 • Letter: M

Question

Matlab question While loop, nested or not? You be the judge. You will develop an indefinite loop (while) which will simulate rolling a die until a six is rolled. Use the randi function to return one value between one and six at a time, and repeat this within a white loop With the Conation of the value being less than six. How many runs did it take before the first six was rolled? Next, set up a for loop which rolls a die five consecutive times and sums the value for the rolls. This will produce a number between five and 30, with the probability of the range of numbers being normally distributed. How many Finally, combine the above loops to simulate rolling a die five times per day repeatedly until you roll five consecutive rolls of a six, for a total of 30. How many days does it take until this happens?

Explanation / Answer

While Loop:

clc;
clear all;

syms a; % Let a be the random number i.e. the count on the dice.
n=0; % number of times the dice was rolled

while(a~=6)
a=randi(6)
n=n+1;
end

fprintf('Number of runs before the first six was rolled = %d',n-1);

Sample Output:

a =

4


a =

1


a =

1


a =

2


a =

6

Number of runs before the first six was rolled = 4 .

------------------------------------------

For Loop:

syms sum r; % r is random number on a dice
sum=0;

n=5; % Roll the dice for 5 consecutive times

for n=1:5
r = randi(6)
sum = sum + r;
end

fprintf('Sum of 5 rolls = %d ',sum);

Sample Output:

r =

3


r =

4


r =

1


r =

1


r =

4

Sum of 5 rolls = 13

------------------------------------------------------------------

3)

syms sum r; % sum is total of 5 rolls of a dice on a perticular day. r is a random number that appears on the dice
day=0; % counter for days
sum=0;

while(sum~=30)
day=day+1;
  
for n=1:5
r = randi(6)
sum = sum + r;
end
end
  
fprintf('Sum of 30 obtained on day %d',day);