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

Problem 4 Write a MATLAB script to simulate rolling dice. First ask the user how

ID: 3905025 • Letter: P

Question

Problem 4 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 3. Roll two dice simultaneously n times and calculate how many times the summation of the numbers is between 5 and 8 inclusively 4. Roll two dice simultaneously n times and calculate how many times the summation of the numbers is divisible by 3 Run your code for n 1000 and report the resuits using fprint

Explanation / Answer

#dice.m
#The matlab script file that prompts user to enter
#number of times to roll a dice and then
#print the number of times 6 is rolled
#number of times the same number is rolled
#number of times the dices are in between 5 and 8
#number of times the sum is divisible by 3


#Prompt user to enter number of times to roll a dice
n=input('Enter number of times to roll a dice :');

#Number of times the dice rolled 6 number
countOfsix=0;
for i=1:n
value=randi([1 6]);
if value==6
countOfsix=countOfsix+1;
end
end

fprintf('# of times 6 rolled %d : ',countOfsix);
#code to find number of times the the two dices are rolled
#as same
sameNumCount=0;
for i=1:n
dice1=randi([1 6]);
dice2=randi([1 6]);
if dice1==dice2
sameNumCount=sameNumCount+1;
end
end

fprintf('# of times same number rolled %d : ',sameNumCount);

#code to find the number of times two dices
#are between 5 and 8
countOf5and8=0;
for i=1:n
dice1=randi([1 6]);
dice2=randi([1 6]);
if ((dice1>5 && dice1<8 ) && (dice2>5 && dice2<8))
countOf5and8=countOf5and8+1;
end
end
fprintf('# of times rolling between 5 and 8 is %d : ',countOf5and8);

#code to find the number of times the sum of dice rolling
#is divisible by 3
divisibleBy3=0;
for i=1:n
dice1=randi([1 6]);
dice2=randi([1 6]);
if (rem(dice1+dice2,2)==0)
divisibleBy3=divisibleBy3+1;
end
end
fprintf('# of times summation divisible by 3 is %d : ',divisibleBy3);

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

Sample Output:


Enter number of times to roll a dice :1000
# of times 6 rolled 163
# of times same number rolled 168
# of times rolling between 5 and 8 is 32
# of times summation divisible by 3 is 498

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote